iPhone TBXML 循环和解析数据 [英] iPhone TBXML Looping And Parsing Data

查看:13
本文介绍了iPhone TBXML 循环和解析数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个返回的 XML 响应和一个字符串,我需要遍历 xml 并将所有信息存储在一个数组中.这是xml

Basically I have an XML response that is returned and a string, and i need to loops through the xml and store all the information in an array. here is the xml

<?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>

我需要将这 2 个 <records> 和所有数据存储在一个数组中.所以....

I need those 2 <records> and all there data to be stored in an array. so....

一个记录数组 -> 记录数组 -> 每个记录的数组,数据......

an records array -> array of records -> array of each records, data....

我一直坐在这里尝试使用 TBXML 来解决这个问题,这很容易抓住单个节点....但我不能这样做:(

I have been sitting here trying to work this out using TBXML which is easy enough to grab a single node.... but I can't do this :(

推荐答案

好的,第一步是创建一个解析数据的类.例如,将其称为 RecordParser.我们现在需要在头文件中添加几个方法,以及一个 NSMutableArray.

Alright, your first step would be to make a class that will parse the data. Call it RecordParser, for example. We now need to add a couple methods in the header, as well as a NSMutableArray.

@interface RecordParser : NSObject {
    NSMutableArray *records;    
}
@property(nonatomic,retain)NSMutableArray *records;

-(void)loadRecords:(NSString *)records;
-(void)traverseElement:(TBXMLElement *)element;

@end

现在,继续进行实施.我们现在需要实现这两种方法来做我们希望它们做的事情.

Now, go ahead and charge into your implementation. We now need to implement those two methods to do what we want them to do.

- (void)loadRecords:(NSString *)records {
    NSString *someXML = @"http://www.something.com/somexml.xml";
    TBXML *tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:someXML]] retain];

    records = [NSMutableArray array];
    [records retain];

    if (tbxml.rootXMLElement)
        [self traverseElement:tbxml.rootXMLElement];
    [tbxml release];
}

基本上,该方法将获取相关的 XML 文件并开始解析过程.此外,您正在初始化数组并保留它.现在我们来看看奶酪.

Basically that method will grab the XML file in question and begin the parsing process. Also, you're initializing your array and retaining it. Now we come to the cheese.

- (void) traverseElement:(TBXMLElement *)element {
    do {
        if (element->firstChild) 
            [self traverseElement:element->firstChild];

        if ([[TBXML elementName:element] isEqualToString:@"Record"]) {
            TBXMLElement *destination = [TBXML childElementNamed:@"Destination" parentElement:element];
            TBXMLElement *status = [TBXML childElementNamed:@"Status" parentElement:element];
            TBXMLElement *guid = [TBXML childElementNamed:@"GUID" parentElement:element];
            TBXMLElement *dateSub = [TBXML childElementNamed:@"DateSubmitted" parentElement:element];
            TBXMLElement *dateToSend = [TBXML childElementNamed:@"DateToSend" parentElement:element];
            TBXMLElement *dateSent = [TBXML childElementNamed:@"DateSent" parentElement:element];
            TBXMLElement *dateReceived = [TBXML childElementNamed:@"DateReceived" parentElement:element];
            TBXMLElement *message = [TBXML childElementNamed:@"Message" parentElement:element];
            TBXMLElement *id = [TBXML childElementNamed:@"ID" parentElement:element];

            [records addObject:[NSArray arrayWithObjects:
                                  [TBXML textForElement:destination],
                                  [TBXML textForElement:status],
                                  [TBXML textForElement:guid],
                                  [TBXML textForElement:dateSub],
                                  [TBXML textForElement:dateToSend],
                                  [TBXML textForElement:dateSent],
                                  [TBXML textForElement:dateReceived],
                                  [TBXML textForElement:message],
                                  [TBXML textForElement:id],nil]];  
        }
    } while ((element = element->nextSibling));  
}

基本上,该方法的作用是遍历 XML 文件以查找具有您要查找的名称的元素,然后从子节点中获取数据.此外,数据被添加到 records 数组中.所以基本上,当它完成后,它应该在 records 数组中包含您想要的数据,您可以随意操作这些数据.

Basically what the method does is transverse the XML file looking for an element with the name you're looking for, then it grabs the data from the child nodes. Additionally, the data is added to the records array. So basically, when it's done it should have the data you're wanting in your records array, which you can manipulate all you want.

这是完全未经测试的.如果它炸毁了您的计算机并杀死了您的猫,请不要怪我.我通常不会花费所有的工作来编写这样一个完整的方法,但我碰巧喜欢 TBXML.请让我知道它是否有效.我真的希望知道.

This is completely untested. Don't blame me if it blows up your computer and kills your cat. I normally wouldn't take all the work to write a complete method like this, but I happen to like TBXML. Please let me know if it works. I really would appreciate knowing.

这篇关于iPhone TBXML 循环和解析数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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