【问题标题】:TBXML parsing complex xml into arrayTBXML 将复杂的 xml 解析为数组
【发布时间】:2011-03-22 15:16:33
【问题描述】:

我有一个需要设置为数组的 xml 响应。问题是我需要访问每个元素并将其存储在一个数组中,以便我可以将它用于 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 还很陌生,所以它证明是一个挑战。

我需要从 xml 中获取 &lt;destination&gt; , &lt;status&gt;, &lt;datesent&gt;, &lt;message&gt;。最多可以有 25 条记录....

希望有人能帮我解决这个问题!

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios ios4


    【解决方案1】:

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

    <?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 中得到结果,称为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;
    }
    

    此函数将返回一个数组,其中包含代表您的RecordsNSDictionary 对象。 NSDictionary 使用起来很简单,获取你使用[yourDictionary objectForKey:yourXmlNode] 的属性。文档在这里:NSDictionary

    【讨论】:

    • 我已经实现了这个,但我得到的数组只填充了第二级以外的任何东西......
    • 我的示例 XML 中只有两个级别,agencyXmlElement->firstChild 正在获得第二个级别。您必须再次执行此操作才能深入了解关卡。您可以在这里完成一切,只需适应它。
    • 很好的答案!但以防万一 - 对于 n 级深度 - gist.github.com/romainbriche/1922643 返回字典。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 2018-02-09
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多