亲爱的朋友,我想要使用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的?
答案 0 :(得分:2)
使用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];
}
}
答案 1 :(得分:0)
您可以使用SMXMLDocument。