【问题标题】:how can i parse the element using attribute in xml parse in iphone?如何在 iphone 中使用 xml 解析中的属性解析元素?
【发布时间】:2012-11-19 08:06:37
【问题描述】:

亲爱的朋友,我想使用 xml 解析器中的属性来解析元素,这是我的 xml 格式

<LOB>Cars</LOB>
<PPL>Indica</PPL>
<REGION>West2</REGION>
<RETAIL>30</RETAIL>
<LOB>Cars</LOB>
<PPL>Indica</PPL>
<REGION>West1</REGION>
<RETAIL>175</RETAIL>
<LOB>Cars</LOB>
<PPL>Indica Vista</PPL>
<REGION>West2</REGION>
<RETAIL>267</RETAIL>
<LOB>Cars</LOB>
<PPL>Indica Vista</PPL>
<REGION>West1</REGION>
<RETAIL>1212</RETAIL>

 more.....


<LOB>PCV - Venture</LOB>
<PPL>Venture</PPL>
<REGION>West2</REGION>
<RETAIL>2</RETAIL>
<LOB>PCV - Venture</LOB>
<PPL>Venture</PPL>
<REGION>West1</REGION>
<RETAIL>12</RETAIL>

 more....

现在我只想要汽车零售计数,但这里 PCV-Venture 属性也在同一个 xml 文件中,所以我只能为属性做些什么是汽车然后解析零售元素而不是 PCV-Venture 的?

【问题讨论】:

  • 你解决了thankq @Rajneesh071

标签: iphone attributes xml-parsing element


【解决方案1】:

使用TBXML解析解析xml格式数据。

链接TBXML

数据应该是这种格式

<info>
<User>
<CountryId>13</CountryId>
<CountryName>Austrlia</CountryName>
</User>
<User>
<CountryId>12</CountryId>
<CountryName>India</CountryName>
</User>
<User>
<CountryId>17</CountryId>
<CountryName>test1</CountryName>
</User>
<User>
<CountryId>16</CountryId>
<CountryName>UK</CountryName>
</User>
</info>  

并解析为

TBXML * tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:url]];

    TBXMLElement *rootXMLElement = tbxml.rootXMLElement;

    if (rootXMLElement)
    {
        TBXMLElement * user = [TBXML childElementNamed:@"user" parentElement:rootXMLElement];


        while (user != nil)
        {

            TBXMLElement *CountryId = [TBXML childElementNamed:@"CountryId" parentElement:user];
            if (CountryId != nil)
            {
                NSLog(@"CountryId :: %@", [TBXML textForElement:CountryId]);
            }

            TBXMLElement *CountryName = [TBXML childElementNamed:@"CountryName" parentElement:user];
            if (CountryName != nil)
            {
                NSLog(@"CountryName :: %@", [TBXML textForElement:CountryName]);
            }

            user = [TBXML nextSiblingNamed:@"user" searchFromElement:user];
        }

    }

【讨论】:

  • 使用父子类型格式,这样你就可以根据汽车获取数据..意味着将汽车作为根,然后其他信息是这个的父
  • 谢谢,但我是初学者,所以请提供一些父子类型格式的示例
【解决方案2】:

您可以使用 SMXMLDocument。

It is an example

【讨论】:

    猜你喜欢
    • 2011-01-11
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 2012-02-26
    相关资源
    最近更新 更多