1

我有一个 XML

<?xml version="1.0" encoding="UTF-8"?>
<drivers>

<driver>
<img><![CDATA[45djx96.jpg]]></img>
<name><![CDATA[Alonso]]></name>
<teamname><![CDATA[farari]]></teamname>
<ref><![CDATA[45djx96]]></ref>
</driver>

<driver>
<img><![CDATA[1236.jpg]]></img>
<name><![CDATA[Alonso2]]></name>
<teamname><![CDATA[farari2]]></teamname>
<ref><![CDATA[1236]]></ref>
</driver>

<driver>
<img><![CDATA[1245FGt.jpg]]></img>
<name><![CDATA[Alonso3]]></name>
<teamname><![CDATA[farari3]]></teamname>
<ref><![CDATA[1245FGt]]></ref>
</driver>

</drivers>

我想显示唯一的细节

在 UILable 上表示 1236,s name & teamname。

我第一次使用网络服务。我成功在控制台上捕获 XML,但无法读取 XML 并在 UILable 上显示名称和团队名称的值。

现在 xml 显示在控制台上,但无法解析。

任何的想法?

提前致谢。

4

3 回答 3

1

使用TouchXML您可以将这些数据解析为数组,也可以将单个记录解析为字符串,我只是发布一些关于它的代码

CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:data options:0 error:nil] autorelease];
NSArray *nodes = [doc nodesForXPath:@"//driver" error:nil];

for (CXMLElement *node in nodes) {      
    // Create Object Of Bean Class. if required

    for(int counter = 0; counter < [node childCount]; counter++) {          
        //Save Data in The Bean Class and Add In the Array.

        if ([[[node childAtIndex:counter] name] isEqualToString:@"teamname"]) {                 
            NSString *string = [[node childAtIndex:counter] stringValue];
                            yourLable.Text = string;
            //                NSLog(@"\n\n Title %@",string);

        }
        else if ([[[node childAtIndex:counter] name] isEqualToString:@"ref"]) {                 
            NSString *string = [[node childAtIndex:counter] stringValue];
           //                NSLog(@"\n\n Ref %@",string);
        } 
}

}

对于 TouchXML,请参见本教程和示例

  1. 触摸XML

  2. iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml

于 2012-11-21T09:46:04.540 回答
1

对于解析,您可以使用需要以下类的 TBXML。TBXML.h、TBXML.m 和 NSDataAdditions.h、NSDataAdditions.m。

我给出了一个示例。您可以根据您的代码进行更改

 NSString *cStr = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SetPatientBPXMLResponse xmlns=\"http://webservice.cruxmed.com/\"><SetPatientBPXMLResult><response><state><![CDATA[45djx96.jpg]]></state></response></SetPatientBPXMLResult></SetPatientBPXMLResponse></soap:Body></soap:Envelope>"];

    TBXML *tbxmlObj = [[TBXML alloc] initWithXMLString:cStr];

    // Obtain root element
    TBXMLElement * root = tbxmlObj.rootXMLElement;
    //-------------------------------------------
    if (root)
    {
        //-------------------------------------------
        // search for the first NewDataSet element within the root element's children
        // instantiate an NewDataSet objectGetDataInXMLFromStoredProcedureResponse
        TBXMLElement * SoapBody = [TBXML childElementNamed:@"soap:Body" parentElement:root];

        TBXMLElement * SoapResponse = [TBXML childElementNamed:@"SetPatientBPXMLResponse" parentElement:SoapBody];

        TBXMLElement * SoapResult = [TBXML childElementNamed:@"SetPatientBPXMLResult" parentElement:SoapResponse];

        //TBXMLElement * NewDataSet1 = [TBXML childElementNamed:@"response" parentElement:SoapResult];

        TBXMLElement * NewDataSet = [TBXML childElementNamed:@"response" parentElement:SoapResult];
        // if an _wspGetChartTemplateByAgent element was found
        while (NewDataSet != nil) {


            //cAccountName,fOrdTotTaxDEx,fOrdTotTax,;

            // instantiate a Order object


            // find the iInvoiceId
            TBXMLElement * AutoIndex = [TBXML childElementNamed:@"state" parentElement:NewDataSet];

            if (AutoIndex != nil)
            {
                NSString* iVal=[TBXML textForElement:AutoIndex] ;
                NSLog(@"iVal:%@",iVal);
            }
            // find the next sibling element named "_wspGetChartTemplateByAgent"
            NewDataSet = [TBXML nextSiblingNamed:@"response" searchFromElement:NewDataSet];
        }
    }
于 2012-11-21T10:02:29.437 回答
0

好吧,我认为 NSXMLParser 类应该敲响警钟。但是,它不是那么方便,您可以找到许多更好的解析器,例如 TBXML(分支之一)。此外,通常建议使用 JSON 而不是 XML,因为它更轻且更易于解析。最常见的 JSON 解析器库是 SBJSON。

于 2012-11-21T10:00:03.403 回答