TBXML将复杂的xml解析为数组 [英] TBXML parsing complex xml into array

查看:108
本文介绍了TBXML将复杂的xml解析为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要设置为数组的xml响应。问题是我需要访问每个元素并将其存储在一个数组中,以便我可以将它用于tableview。

I have an xml response that i need to set into an array. The problem is i need to access each element and store it in an array so that i can use it for a tableview.

<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.2sms.com/2.0/schema/0310_ResponseReportStandard.xsd" Version="1.0">
    <Error>
        <ErrorCode>00</ErrorCode>
        <ErrorReason>OK</ErrorReason>
    </Error>
    <ResponseData>
        <Identification>
            <UserID>jonathan.pink@2sms.com</UserID>
        </Identification>
        <Result>2 records were returned</Result>
        <Detail>
            <ReportTitle>Message Summary: Today</ReportTitle>
            <Record>
                <Destination>447790686158</Destination>
                <Status>WithNetwork</Status>
                <GUID><![CDATA[2011-03-22T10:54:22.097Z]]></GUID>
                <DateSubmitted>2011-03-22T10:54:22.097</DateSubmitted>
                <DateToSend></DateToSend>
                <DateSent>2011-03-22T10:54:22.533</DateSent>
                <DateReceived></DateReceived>
                <Message><![CDATA[Yet again another test]]></Message>
                <ID>2011-03-22 10:54:22.250HIHIIOJTFVETW85TS</ID>
            </Record>
            <Record>
                <Destination>447790686158</Destination>
                <Status>SUCCESS</Status>
                <GUID><![CDATA[2011-03-22T10:50:40.064Z]]></GUID>
                <DateSubmitted>2011-03-22T10:50:40.063</DateSubmitted>
                <DateToSend></DateToSend>
                <DateSent>2011-03-22T10:50:42.473</DateSent>
                <DateReceived>2011-03-22T10:50:54.570</DateReceived>
                <Message><![CDATA[This is a test]]></Message>
                <ID>2011-03-22 10:50:40.210DRUDVMCEZGETW85TS</ID>
            </Record>
            <ReportPage ReportID="775797" ItemsPerPage="25" Page="1" TotalItems="2" />
        </Detail>
    </ResponseData>
</Response>

我已初始化TBXML对象,但无法弄清楚如何获取任何重复信息,我知道它需要某种循环,但我对objective-c很新,所以它证明了一个挑战。

I have the TBXML object initialised but can't work out how to get any of the recurring information, I know it will need some sort of loop but i'm pretty new to objective-c so its proving a challenge.

我需要得到<目的地> ,来自xml的< status>,< datesent>,< message> 。最多可以有25条记录....

I need to get the <destination> , <status>, <datesent>, <message> from the xml. There can be up to 25 records....

希望有人可以帮助我一整天都在努力!

Hope some one can help this has been doing my head in all day!

推荐答案

我最近做了类似的事情,你可以快速适应,我的XML是:

I did something similar recently that you can adapt quickly, my XML was:

<?xml version="1.0" encoding="ISO-8859-1"?>
<agencies>
    <agency>
        <name>Agency 1 name</name>
        <addressFirstLine>Immeuble XYZ<addressFirstLine>
        <addressSecondLine>rue de la republique</addressSecondLine>
        <addressThirdLine>69007 Lyon</addressThirdLine>
        <telNumber>01 23 45 67 89</telNumber>
    </agency>
    <agency>
        <name>Agency 2 name</name>
        <addressFirstLine>Immeuble ABC<addressFirstLine>
        <addressSecondLine>rue de la republique</addressSecondLine>
        <addressThirdLine>69007 Lyon</addressThirdLine>
        <telNumber>01 23 45 67 89</telNumber>
    </agency>
</agencies>

我用来解析的代码(在我的 NSArray中得到结果名为代理商):

The code I used to parse this (get the result in my NSArray called agencies):

    TBXML *tbxml = [[TBXML tbxmlWithXMLFile:yourXmlFile retain];
    TBXMLElement *rootXMLElement = tbxml.rootXMLElement;

    if (rootXMLElement) {
        self.agencies = [self traverseElement:rootXMLElement];
        [delegate managerDidReceiveData];
    }

    // release resources
    [tbxml release];

我要转换为数组:

- (NSArray *)traverseElement:(TBXMLElement *)element {

    NSMutableArray *tmpAgencies = [[NSMutableArray alloc] init];

    TBXMLElement *agenciesXmlElement = element->firstChild;
    TBXMLElement *agencyXmlElement;

    do {

        // if the element has child elements, process them
        if ((agencyXmlElement = agenciesXmlElement->firstChild)) {

            NSMutableDictionary *tmpAgency = [[NSMutableDictionary alloc] init];

            do {
                [tmpAgency setValue:[TBXML textForElement:agencyXmlElement] forKey:[TBXML elementName:agencyXmlElement]];

            // Obtain next sibling element
            } while ((agencyXmlElement = agencyXmlElement->nextSibling));

            [tmpAgencies addObject:tmpAgency];
            [tmpAgency release];
        }

    // Obtain next sibling element
    } while ((agenciesXmlElement = agenciesXmlElement->nextSibling));

    return tmpAgencies;
}

此函数将返回一个包含 NSDictionary的数组表示记录的对象。
NSDictionary很容易使用,获取你使用的属性 [yourDictionary objectForKey:yourXmlNode] 。该文档位于: NSDictionary

This function will return you an array which contains NSDictionary objects representing your Records. NSDictionary is simple to use, to get a property you use [yourDictionary objectForKey:yourXmlNode]. The doc is here: NSDictionary.

这篇关于TBXML将复杂的xml解析为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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