如何在 Objective-C 中使用 TBXML 解析此 xml?
我知道我的 xml 看起来很奇怪但没什么可做的。现在我的问题是如何使用 TBXML 解析这个 xml 属性及其值?
−<order>
<id>100</id>
<order_date>October 13th, 2011 at 2:43 PM</order_date>
−<customer>
<id>45</id>
<name>John Kramer</name>
−<address>
−<billing>167 Yarra Street, South Yarra, Melbourne, Australia</billing>
−<shipping>35 Victoria, Street, North Yarra, Melbourne, Australia</shipping>
<email> [email protected]</email>
<phone> +6546325478 </phone>
</address>
</customer>
−<products>
−<product>
<id>12</id>
<name>Asus F5RL</name>
−<description>Color: Blue. Size: Square.</description>
<qty>2</qty>
<unit_price>50 AUD</unit_price>
<total_price>100 AUD</total_price>
</product>
−<product>
<id>12</id>
<name>Acer F4</name>
<description>Color: Red</description>
<qty>3</qty>
<unit_price>40 AUD</unit_price>
<total_price>120 AUD</total_price>
</product>
</products>
-<price_details>
<subtotal>220 AUD</subtotal>
<discount>20 AUD</discount>
<tax> 10 AUD </tax>
<shipment> 5 AUD </shipment>
<grand_total> 235 AUD </grand_total>
</price_details>
<order_status>Pending </order_status>
</order>
现在我仍然做到了这一点,但它正在崩溃。
- (void)traverseElement:(TBXMLElement *)element {
do {
NSLog(@"%@",[TBXML elementName:element]);
if (element->firstChild)
[self traverseElement:element->firstChild];
TBXMLAttribute * attribute = element->firstAttribute;
// if attribute is valid
while (attribute) {
// Display name and value of attribute to the log window
NSLog(@"%@->%@ = %@",
[TBXML elementName:element],
[TBXML attributeName:attribute],
[TBXML attributeValue:attribute]);
// Obtain the next attribute
attribute = attribute->next;
}
if ([[TBXML elementName:element] isEqualToString:@"order"]) {
NSLog(@"xml element checking");
TBXMLElement *order_id = [TBXML childElementNamed:@"id" parentElement:element];
TBXMLElement *order_date = [TBXML childElementNamed:@"order_date" parentElement:element];
TBXMLElement *customer = [TBXML childElementNamed:@"customer" parentElement:element];
TBXMLElement *customer_id = [TBXML childElementNamed:@"id" parentElement:customer];
TBXMLElement *customer_name = [TBXML childElementNamed:@"name" parentElement:customer];
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:customer_id],
[TBXML textForElement:customer_name],
[TBXML textForElement:order_id],
[TBXML textForElement:order_date],nil ] ];
NSLog(@"customer id: %@",customer_id);
NSLog(@"customer name: %@",customer_name);
}
I know my xml looking very much odd but nothing to do. Now my problem is how can I parse this xml attribute and its value using TBXML?
−<order>
<id>100</id>
<order_date>October 13th, 2011 at 2:43 PM</order_date>
−<customer>
<id>45</id>
<name>John Kramer</name>
−<address>
−<billing>167 Yarra Street, South Yarra, Melbourne, Australia</billing>
−<shipping>35 Victoria, Street, North Yarra, Melbourne, Australia</shipping>
<email> [email protected]</email>
<phone> +6546325478 </phone>
</address>
</customer>
−<products>
−<product>
<id>12</id>
<name>Asus F5RL</name>
−<description>Color: Blue. Size: Square.</description>
<qty>2</qty>
<unit_price>50 AUD</unit_price>
<total_price>100 AUD</total_price>
</product>
−<product>
<id>12</id>
<name>Acer F4</name>
<description>Color: Red</description>
<qty>3</qty>
<unit_price>40 AUD</unit_price>
<total_price>120 AUD</total_price>
</product>
</products>
-<price_details>
<subtotal>220 AUD</subtotal>
<discount>20 AUD</discount>
<tax> 10 AUD </tax>
<shipment> 5 AUD </shipment>
<grand_total> 235 AUD </grand_total>
</price_details>
<order_status>Pending </order_status>
</order>
still now I have done this but it is crashing.
- (void)traverseElement:(TBXMLElement *)element {
do {
NSLog(@"%@",[TBXML elementName:element]);
if (element->firstChild)
[self traverseElement:element->firstChild];
TBXMLAttribute * attribute = element->firstAttribute;
// if attribute is valid
while (attribute) {
// Display name and value of attribute to the log window
NSLog(@"%@->%@ = %@",
[TBXML elementName:element],
[TBXML attributeName:attribute],
[TBXML attributeValue:attribute]);
// Obtain the next attribute
attribute = attribute->next;
}
if ([[TBXML elementName:element] isEqualToString:@"order"]) {
NSLog(@"xml element checking");
TBXMLElement *order_id = [TBXML childElementNamed:@"id" parentElement:element];
TBXMLElement *order_date = [TBXML childElementNamed:@"order_date" parentElement:element];
TBXMLElement *customer = [TBXML childElementNamed:@"customer" parentElement:element];
TBXMLElement *customer_id = [TBXML childElementNamed:@"id" parentElement:customer];
TBXMLElement *customer_name = [TBXML childElementNamed:@"name" parentElement:customer];
[records addObject:[NSArray arrayWithObjects:
[TBXML textForElement:customer_id],
[TBXML textForElement:customer_name],
[TBXML textForElement:order_id],
[TBXML textForElement:order_date],nil ] ];
NSLog(@"customer id: %@",customer_id);
NSLog(@"customer name: %@",customer_name);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经解决了这个问题可能会对某人有帮助......
I have solve this problem may be it will helpful for someone...