【问题标题】:Dynamic Xml parsing in iOSiOS 中的动态 Xml 解析
【发布时间】:2014-09-05 10:46:45
【问题描述】:

我正在处理所有用户数据都依赖于 Web 服务的项目。 Web 服务开发人员发送 xml 作为我的肥皂请求的响应。

在某些情况下,Web 服务开发人员从不同的类为同一 Web 服务生成 xml。 在这种情况下,xml 是使用不同的元素标签生成的。这里 Login 是我的 Web 服务,在不同的情况下会得到两种不同的响应

//============Response 1====================Success========================================
    <soap:Envelope xmlns:soap="http://xyzs.com xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <soap:Body>
          <LoginUserResponse xmlns="http://xyz.co/">
             <LoginUserResult>   —————————————————————>StartElement
                <ArrayOfUserLoginComplexType xmlns="">
                   <UserLoginComplexType>      ——————————————————————>RowElement              
                      <UserId>1</UserId>
                      <Role>User</Role>
                      <UserName>abc</UserName>
                      <Email>user@xyz.com</Email>
                      <ZipCode>0</ZipCode>
                      <IsRead>false</IsRead>
                      <IsWrite>false</IsWrite>
                      <IsDelete>false</IsDelete>
                      <NoOfUser>0</NoOfUser>
                      <isPediaPurchased>false</isPediaPurchased>
                      <isPaymentDone>false</isPaymentDone>
                   </UserLoginComplexType>
                </ArrayOfUserLoginComplexType>
             </LoginUserResult>
          </LoginUserResponse>
       </soap:Body>
    </soap:Envelope>

//============Response 2======================Failuer=======================================
    <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>
          <LoginUserResponse xmlns="http://tempuri.org/">
             <LoginUserResult>   —————————————————————>StartElement
                <ctMessageMst xmlns=“”>————————————————————  > RowElement
                   <MessageID>13</MessageID>
                   <TitleName>Xyz</TitleName>
                   <ProductID>0</ProductID>
                   <MessageText>Hi there</MessageText>
                   <MessageType>E</MessageType>
                   <Date>2014-07-15</Date>
                </ctMessageMst>
             </LoginUserResult>
          </LoginUserResponse>
       </soap:Body>
    </soap:Envelope>

//--------XML Parser Code-----------------------------------------------------------------
      - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
        attributes:(NSDictionary *)attributeDict
        {
        if([elementName isEqualToString:@"GetAllMessageListResult"]) {
             row_elementName=@"ctMessageMst";
             [self CreateDictionary];       
        }   
        else if([elementName isEqualToString:@"LoginUserResult"]) {        
            row_elementName=@"UserLoginComplexType";  //Hardcode condition that i have implemented
            [self CreateDictionary];
        }.....

在我的项目中,我为 Rowelement 完成了硬编码,但我希望它是动态的,我怎么能做这个动态的实施?

我参考了苹果示例代码,但在该代码中,他们也实现了硬编码条件。

  • 案例 1。在响应 1 中,我得到 ----(UserLoginComplexType) 作为 行元素

  • 案例 2。在响应 2 中,我得到 ----(ctMessageMst xmlns="") 作为 行元素

    在我的 Xml 解析器代码中,我静态实现了行元素名称,这在案例 2 中不起作用。 我不知道该怎么做。

【问题讨论】:

  • 这不是一个正常的XML数据,它的soap service需要以不同的方式处理。试着用soap找ios,有很多客户端可用。我知道的一个是sudzc.com,它使用你的soap服务生成一个xcode项目,并进行所有的解析和处理。

标签: ios objective-c ios7 xml-parsing


【解决方案1】:

使用xmlreader解析任何复杂的xml。这会将复杂的xml转换成字典。

【讨论】:

    猜你喜欢
    • 2013-02-08
    • 1970-01-01
    • 2022-04-04
    • 2013-06-28
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2014-05-13
    • 2012-08-11
    相关资源
    最近更新 更多