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 の名前とチーム名を意味します。

私は webservice frist time を使用しています。コンソールで XML をキャッチすることはできますが、UILable で XML を読み取って名前とチーム名の値を表示することはできません。

現在、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. TouchXML

  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。

サンプル例を1つ挙げました。コードに応じて変更を加えることができます。

 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 (ブランチの 1 つ) など、より優れたパーサーが多数あります。さらに、XML の代わりに JSON を使用することをお勧めします。最も一般的な JSON パーサー ライブラリは SBJSON です。

于 2012-11-21T10:00:03.403 に答える