0

How can I get this <firstName> and <lastName>. I have tried a lot but I have messed up.

<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>

Till now I have done this.

 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);
        }
 }

Cordially appreciate for any help or suggestion.

Yamen Emon
  • 139
  • 3
  • 12
  • possible duplicate of [How can I parse this xml using GDataXML parser?](http://stackoverflow.com/questions/9156302/how-can-i-parse-this-xml-using-gdataxml-parser) – Midhun MP Jan 31 '13 at 06:25

1 Answers1

2

I would try something like this. I would loop through the XML document, and store it in an array.

Notice the NSLog() which will print to the console and will help you see what is going on.

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);
}

Then I would loop through the array, and do something like this when I wanted to retrieve a result such as the customer's first name.

for (int i = 0; i < [outputArray count]; i++) {
   NSString *firstName = [[[[[[[[outputArray objectAtIndex:i]elementsForName:@"customer"] objectAtIndex:0] elementsForName:@"CustomerData"] objectAtIndex:0] childAtIndex:2]stringValue];
}
Vikings
  • 2,527
  • 32
  • 45
  • If you post a full section of the XML document, that would help – Vikings Jan 29 '12 at 15:14
  • What is the NSLog statement printing? If Nothing, then you doing something wrong before parsing the file – Vikings Jan 30 '12 at 18:29
  • It would also be nice to see more of the XML files, and i'm sure we can get it to work – Vikings Jan 30 '12 at 18:30
  • Okay I get errors when I try to parse the file. For one check out there is no , that is not a proper XML structure. You also need to see what your NSLog prints out in that for loop, if nothing prints out then you have an error before parsing. It's hard to help you when I don't know the XML file structure or what errors you are getting. – Vikings Feb 01 '12 at 01:38
  • Check out this tutorial, maybe that will help you out - http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml – Vikings Feb 01 '12 at 01:40
  • i have seen that tutorial but i did not get any help or may be i did not understand their solution.So if you help me then it will so glad to you – Yamen Emon Feb 01 '12 at 03:35
  • i did not allocate the `outputArray`,that's why it did not work.Now it is working... – Yamen Emon Feb 01 '12 at 03:46