IPhone SDK : Help me about xml parsing with TBXML

2019-07-24 18:16发布

问题:

i have a connection with a webservice, recieved data is not equals to "nil" and the response SOAP UI show is this;

<soap:Envelope
     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
>
-<soap:Body>
 -<GroupResponse xmlns="http://tempuri.org/">
  -<GroupResult>
   -<GroupClass>
     -<Groupid>48937</Groupid>
     -<GroupName>test</GroupName>
       -<Members>
        -<MemberClass>
            -<memberID>4699860</memberID>
            -<memberNAME>nazan</memberNAME>
            -<memberLASTNAME>torun</memberLASTNAME>
            -<memberPHONE>292930412452</memberPHONE>
            -<memberBIRTH>02.04.1980</memberBIRTH>
            -<memberMARRIAGE>01.11.2009</memberMARRIAGE>
         </MemberClass>
        -<MemberClass>
              ...has lots of member

how can i get theese with TBXML ? this is my code, but it crashes..

NSArray *propertyArray = [NSArray arrayWithObjects:@"Members",@"memberID",@"memberNAME",@"memberLASTNAME",@"memberPhone",@"memberBIRTH",@"memberMARRIAGE",nil];

TBXML * tbxml = [[TBXML alloc] initWithXMLData:GrupRecievedData];
        TBXMLElement *root = tbxml.rootXMLElement;
        if (root) {
            NSLog(@"root: %@",[TBXML elementName:root]);

            TBXMLElement *item = [TBXML childElementNamed:@"soap:Body" parentElement:root];
            TBXMLElement *item1 = [TBXML childElementNamed:@"GroupResponse" parentElement:item];
            TBXMLElement *item2 = [TBXML childElementNamed:@"GroupResult" parentElement:item1];
            TBXMLElement *item3 = [TBXML childElementNamed:@"Groupid" parentElement:item2];
            TBXMLElement *item4 = [TBXML childElementNamed:@"GroupName" parentElement:item3];
            TBXMLElement *item5 = [TBXML childElementNamed:@"Members" parentElement:item4];

i tried to parse with objects in array, but i crashes on "TBXMLElement *item1 = [TBXML childElementNamed:@"GroupResponse" parentElement:item];" i dont have any idea anything about solving this problem..

回答1:

Place a breakpoint on root and in debugger window expand root see if you have childElement soap:Body then expand childElement soap:Body see if you have childElement GroupResponse..From the XML you posted I tried it out it works fine until Groupid , Groupid, GroupName,Members are siblings, MemberClass is firstChild of Member, MemberId is firstChild of memberclass, MemberName, MemberPhone.etc are nextSiblings of MemberId.



回答2:

Have you tried setting a break point and stepping through the code to see what each call is actually returning? Also, the error it crashes with would help. My first guess would be that [TBXML childElementNamed:@"soap:Body" parentElement:root] is returning nil. Then on the next line you are looking for the child of the nil "item" which the TBXML code probably doesn't like.