iphone - 如何使用 GDataXML 解析器解析嵌套的 xml?

标签 iphone ios xml xml-parsing gdataxml

我怎样才能得到这个 <firstName><lastName> 。我已经尝试了很多,但都搞砸了。

<list>
<OrderData HASH="1843180441">
 <id>26</id><customer>
  <CustomerData HASH="882063912"> 
   <id>1</id> 
   <sex>Male</sex>
   <firstName>admin</firstName>
   <lastName>admin</lastName>
   <companyName>Individual</companyName>
   <userName>admin</userName>
   <password>21232f297a57a5a743894a0e4a801fc3</password>    
   <lastLogin>2012-01-26T12:11:38</lastLogin>
   <status>A</status>
   <created>2011-09-28T07:01:47</created>
   <modified>2012-01-26T12:11:38</modified>
   <canChangePassword>Y</canChangePassword>
   <isDeleted>false</isDeleted>
   <abn/>
   <abnBranch/>
 <storeCredit>50000.0</storeCredit>
 <billingAddress>
 <list>
 <AddressData HASH="768553743">
  <id>2</id>
  <firstName>admin</firstName>
  <lastName>admin</lastName>
  <addressLine1>suite: 1307, 9 Yara Street</addressLine1> 
  <addressLine2>dgdgdf</addressLine2>
  <postCode>3000</postCode>
  <suburb>dfgdfg</suburb>
  <city>sadf</city>
  <phone>4456</phone>
  <mobile/>
  <fax/>
  <active>true</active>
  <type>C</type>
  <email>afzal.bitmascot@gmail.com</email>
  <log/>
 </AddressData>
 </deliveryAddress>
 <purchaseDate>2011-10-03T03:23:48</purchaseDate>

</OrderData>
<OrderData HASH="1569451006"></OrderData>
<OrderData HASH="1449383081"></OrderData>
<OrderData HASH="1438157618"></OrderData>
<OrderData HASH="269308788"></OrderData>
</list>

到目前为止,我已经做到了。

 NSLog(@"Enering in the xml file");
 NSArray *getNumberOfOrder = [[xmlDocument rootElement] elementsForName:@"OrderData"];

 for(GDataXMLElement *e in getNumberOfOrder){
   for(int i =0;i<[[e elementsForName:@"customer"]count];i++){
        // All orderd customer name
        NSString  *orderByFirstString = [[[[e elementsForName:@"customer"] objectAtIndex:0] childAtIndex:2] stringValue];
        NSString  *orderByLastString = [[[[e elementsForName:@"customer"] objectAtIndex:0] childAtIndex:3]stringValue];
        orderByString = [NSString stringWithFormat:@"%@ %@",orderByFirstString,orderByLastString];
        NSLog(@"order By String: %@",orderByString);
        }
 }

衷心感谢任何帮助或建议。

最佳答案

我会尝试这样的事情。我将遍历 XML 文档,并将其存储在一个数组中。

注意 NSLog() ,它将打印到控制台并帮助您了解正在发生的事情。

xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];

NSArray *temp = [xmlDocument.rootElement elementsForName:@"OrderData"];

for(GDataXMLElement *e in temp) {
    [outputArray addObject:e];
    NSLog(@"%@", e);
}

然后我会遍历数组,并在我想检索结果(例如客户的名字)时执行类似的操作。

for (int i = 0; i < [outputArray count]; i++) {
   NSString *firstName = [[[[[[[[outputArray objectAtIndex:i]elementsForName:@"customer"] objectAtIndex:0] elementsForName:@"CustomerData"] objectAtIndex:0] childAtIndex:2]stringValue];
}

关于iphone - 如何使用 GDataXML 解析器解析嵌套的 xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9052571/

相关文章:

ios - 在 Xcode 11 中添加对本地 swift 包的依赖

iphone - 使用简单音频引擎的声音循环之间的轻微停顿

iphone - App Store 更新后,iOS 应用程序是否会继续在后台运行?

iphone - UIWebView 加载崩溃 [__NSCFDictionary _isChargeEnabled]

ios - SwiftUI:当应用程序在后台时如何添加带有应用程序 Logo 的模糊 View ?

javascript - 使用 XPATH Javascript 获取 XML 片段

iphone - 与住在其他地方的艺术家合作开发 iOS 游戏/在 iOS 应用程序中修改文件而不重建

ios - iO 的 RDoc 是什么?

安卓.view.InflateException : Binary XML file line #8

java - Apache Camel 简单 http 示例