使用objective-c进行TBXML解析 [英] TBXML parsing using objective- c

查看:98
本文介绍了使用objective-c进行TBXML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在尝试使用TBXML解析以下xml.我想创建一个包含所有项目标签值的数组.如何遍历"item"标签?

Am trying to parse the below xml using TBXML. I want to create an array which contains all the item tag values. How can I traverse through the "item" tags?

<root>
<item>
<northeast_area>
<item>
<new_england_north xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[8]">
    <item xsi:type="xsd:string">boston s, ma</item>
    <item xsi:type="xsd:string">providence, ri</item>
    <item xsi:type="xsd:string">boston, ma </item>
    <item xsi:type="xsd:string">portland, me</item>
    <item xsi:type="xsd:string">boston, ma </item>
    <item xsi:type="xsd:string">boston central</item>
    <item xsi:type="xsd:string">boston north, ma</item>
    <item xsi:type="xsd:string">boston south, ma</item>
</new_england_north>
</item>

<item>
<upstate_new_york xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="xsd:string[6]">
    <item xsi:type="xsd:string">binghampton, ny</item>
    <item xsi:type="xsd:string">rochester, ny</item>
    <item xsi:type="xsd:string">albany s, ny</item>
    <item xsi:type="xsd:string">syracuse, ny</item>
    <item xsi:type="xsd:string">albany, ny</item>
    <item xsi:type="xsd:string">buffalo, ny</item>
</upstate_new_york>
</item>
</northeast_area>
</item>

推荐答案

下面是一段示例代码,可从文件中读取XML.您可以对其进行调整以使用可用的NSString/NSData对象. 请注意,由于我们不知道您的SOAP调用可以返回的确切格式,因此它并不是很可靠,它只是一种解决方案,适用于您提供的数据.

Here's a piece of sample code that reads the XML from a file. You can tweak it to use an available NSString/NSData object. Note that since we don't know the exact potential formats that your SOAP calls can return, this isn't very robust, it's just a solution that works for the data you've provided.

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        NSString* path = @"/Path/To/Your/File";
        NSData *contents = [[NSFileManager defaultManager] contentsAtPath:path];
        TBXML *xml = [TBXML tbxmlWithXMLData:contents];

        TBXMLElement *rootElement = [xml rootXMLElement];

        TBXMLElement *rootItemElem = [TBXML childElementNamed:@"item" parentElement:rootElement];

        while (rootItemElem != nil) {
            TBXMLElement *areaElement = rootItemElem->firstChild;

            while (areaElement != nil) {
                TBXMLElement *areaItemElement = [TBXML childElementNamed:@"item" parentElement:areaElement];
                while (areaItemElement != nil) {
                    TBXMLElement *localeItem = areaItemElement->firstChild;
                    NSLog(@"Checking locale %s", localeItem->name);
                    TBXMLElement *specificItem = [TBXML childElementNamed:@"item" parentElement:localeItem];
                    while (specificItem != nil) {
                        NSLog(@"Item with value: %@", [TBXML textForElement:specificItem]);
                        specificItem = [TBXML nextSiblingNamed:@"item" searchFromElement:specificItem];
                    }

                    areaItemElement = [TBXML nextSiblingNamed:@"item" searchFromElement:areaItemElement];
                }

                areaElement = areaElement->nextSibling;
            }

            rootItemElem = [TBXML nextSiblingNamed:@"item" searchFromElement:rootItemElem];
        }

    }
    return 0;
}

和输出:

2011-10-19 08:20:40.321 Testing[14768:707] Checking locale new_england_north
2011-10-19 08:20:40.323 Testing[14768:707] Item with value: boston s, ma
2011-10-19 08:20:40.323 Testing[14768:707] Item with value: providence, ri
2011-10-19 08:20:40.324 Testing[14768:707] Item with value: boston, ma
2011-10-19 08:20:40.324 Testing[14768:707] Item with value: portland, me
2011-10-19 08:20:40.325 Testing[14768:707] Item with value: boston, ma
2011-10-19 08:20:40.326 Testing[14768:707] Item with value: boston central
2011-10-19 08:20:40.326 Testing[14768:707] Item with value: boston north, ma
2011-10-19 08:20:40.327 Testing[14768:707] Item with value: boston south, ma
2011-10-19 08:20:40.327 Testing[14768:707] Checking locale upstate_new_york
2011-10-19 08:20:40.328 Testing[14768:707] Item with value: binghampton, ny
2011-10-19 08:20:40.328 Testing[14768:707] Item with value: rochester, ny
2011-10-19 08:20:40.329 Testing[14768:707] Item with value: albany s, ny
2011-10-19 08:20:40.329 Testing[14768:707] Item with value: syracuse, ny
2011-10-19 08:20:40.330 Testing[14768:707] Item with value: albany, ny
2011-10-19 08:20:40.330 Testing[14768:707] Item with value: buffalo, ny

使用它,创建一个NSArray并存储项目,或者为每个语言环境创建一个NSArray并以这种方式分隔项目应该相当简单. (将它们存储在以区域设置为键的NSDictionary中.)

Using this, it should be fairly straightforward to create an NSArray and store the items, or create an NSArray for each locale and separate the items that way. (Storing them in an NSDictionary keyed by the locale.)

这篇关于使用objective-c进行TBXML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆