iOS中的Nsxml解析器

时间:2013-03-26 09:30:47

标签: iphone nsxmlparser

我是手机编程的新手。从Web服务调用我收到了响应,我必须得到数据。我已尽力但我无法做到这一点......我需要(两个问题,即你童年最好的名字是什么?你的出生地点是什么?)标签中的价值帮助plz ...... ..下面是我必须解析的xml响应....

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:isForgotPasswordResponse xmlns:ns1="http://ws.esb.pmp2.ilantus.com/">
         <ns1:return>
            <ns2:attributes xsi:nil="true" xmlns:ns2="http://vo.common.pmp2.ilantus.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:editAllAttributesBean xsi:nil="true" xmlns:ns2="http://vo.common.pmp2.ilantus.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:loginBean xmlns:ns2="http://vo.common.pmp2.ilantus.com">
               <ns2:changePwdOnNextLogon>false</ns2:changePwdOnNextLogon>
               <ns2:host>localhost</ns2:host>
               <ns2:lock>false</ns2:lock>
               <ns2:loginId>pxpuser1002</ns2:loginId>
               <ns2:loginPassword>Passw0rd</ns2:loginPassword>
               <ns2:newPassword xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:oldPassword xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:role>Help Desk User Role</ns2:role>
               <ns2:seedvalue>5VIQPHRXOBTMGRID</ns2:seedvalue>
               <ns2:user xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            </ns2:loginBean>
            <ns2:policyBeans xsi:nil="true" xmlns:ns2="http://vo.common.pmp2.ilantus.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:profileBean xmlns:ns2="http://vo.common.pmp2.ilantus.com">
               <ns2:attributes xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:email>Not specified</ns2:email>
               <ns2:firstName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:isRegistered xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:lastName xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:loginBean xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
               <ns2:username xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            </ns2:profileBean>
            <ns2:qnABeans xsi:nil="true" xmlns:ns2="http://vo.common.pmp2.ilantus.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:questions xmlns:ns2="http://vo.common.pmp2.ilantus.com">
               <ns2:Attribute>
                  <ns2:attributeName>question1</ns2:attributeName>
                  <ns2:category xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:editable xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:entity_name xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:label xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:operation xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:options xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:required xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:sequence>0</ns2:sequence>
                  <ns2:type xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:value>What was the name of your childhood best fr?</ns2:value>
               </ns2:Attribute>
               <ns2:Attribute>
                  <ns2:attributeName>question2</ns2:attributeName>
                  <ns2:category xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:editable xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:entity_name xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:label xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:operation xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:options xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:required xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:sequence>0</ns2:sequence>
                  <ns2:type xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:value>Which is your place of birth?</ns2:value>
               </ns2:Attribute>
            </ns2:questions>
            <ns2:reportBean xsi:nil="true" xmlns:ns2="http://vo.common.pmp2.ilantus.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
            <ns2:secondFactorBean xmlns:ns2="http://vo.common.pmp2.ilantus.com">
               <ns2:attributes>
                  <ns2:Attribute>
                     <ns2:attributeName>End User Role</ns2:attributeName>
                     <ns2:category xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:editable xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:entity_name xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:label xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:operation xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:options xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:required xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:sequence>0</ns2:sequence>
                     <ns2:type xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:value>Enabled</ns2:value>
                  </ns2:Attribute>
                  <ns2:Attribute>
                     <ns2:attributeName>Help Desk User Role</ns2:attributeName>
                     <ns2:category xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:editable xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:entity_name xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:label xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:operation xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:options xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:required xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:sequence>0</ns2:sequence>
                     <ns2:type xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:value>Enabled</ns2:value>
                  </ns2:Attribute>
                  <ns2:Attribute>
                     <ns2:attributeName>Administrator Role</ns2:attributeName>
                     <ns2:category xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:editable xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:entity_name xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:label xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:operation xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:options xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:required xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:sequence>0</ns2:sequence>
                     <ns2:type xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                     <ns2:value>Disabled</ns2:value>
                  </ns2:Attribute>
               </ns2:attributes>
               <ns2:emailTemplate>Dear {0}, 
 
Please find the back up code for your multi factor authentication. 
Back up code:{1} 
Please configure your code manually, if you delete your entry from Google authenticator accidentally. 
 
Thank you, 
TEAM PXP. 
 
 
N.B. Please do not reply to this mail, as it is not monitored.</ns2:emailTemplate>
               <ns2:secondFactor>true</ns2:secondFactor>
               <ns2:statusBean>
                  <ns2:errorCode xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:errorMessage xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
                  <ns2:result>true</ns2:result>
               </ns2:statusBean>
            </ns2:secondFactorBean>
            <ns2:statusBean xmlns:ns2="http://vo.common.pmp2.ilantus.com">
               <ns2:errorCode>PXPERROR11033</ns2:errorCode>
               <ns2:errorMessage>Either challenge or response or both not found for the user.pxpuser1002</ns2:errorMessage>
               <ns2:result>true</ns2:result>
            </ns2:statusBean>
         </ns1:return>
      </ns1:isForgotPasswordResponse>
   </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:2)

NSMutableString *xmlString;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    xmlString = [[NSMutableString alloc] init];    
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [xmlString appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{    
    if ([elementName isEqualToString:@"ns2:value"])
    {
        if (![xmlString isEqual:@""])
        {
            NSLog(@"%@",xmlString);
            [SomeArray addObject:xmlString];
        }
    }

    //Release string
    [xmlString release];
    xmlString = nil;
}

此处NSMutableString是一个变量,用于跟踪foundCharacters方法中的当前元素,并使用elementName追加字符串和didEndElement方法,我们可以识别该标记的值。< / p>

<强>更新

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    //update your UI here after parsing is finished
}