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

查看:102
本文介绍了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 数组。所以基本上,当它完成它应该有你想要的数据在你的记录数组,你可以操纵你想要的。

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天全站免登陆