如何使用 GDataXML 解析器解析此 xml?

发布于 2025-01-03 03:53:03 字数 1434 浏览 0 评论 0原文

<list>
    <OrderData HASH="1408108039"></OrderData>
    <OrderData HASH="208524692">
        <id>97</id>
        <customer>
            <CustomerData HASH="2128670187"></CustomerData></customer>
        <billingAddress></billingAddress><deliveryAddress></deliveryAddress>
        <orderDetail>
            <list>
                <OrderDetailData HASH="516790072"></OrderDetailData>
                <OrderDetailData HASH="11226247"></OrderDetailData>
                <OrderDetailData HASH="11226247"></OrderDetailData>
            </list>
        </orderDetail>
        <log/>
    </OrderData>
    <OrderData HASH="1502226778"></OrderData>
</list>

我找不到解决方案来查找 OrderDetailData 元素的数量?我还阅读了 http://iphonebyradix.blogspot。 com/2011/03/using-gdata-to-parse-xml-file.html 这个网址。

提前致谢。

编辑:

我再次解释我的要求。在此 xml 中将有多个 OrderData 元素。现在我必须计算特定 OrderData 元素中 OrderDetailData 元素的数量。假设,根据我的 xml,当前解析的 xml 有一个 OrderData 元素,名为 id,其值为 97。现在,我必须计算有多少 OrderDetailData 元素包含在 OrderData中(其id` 为 97)。

<list>
    <OrderData HASH="1408108039"></OrderData>
    <OrderData HASH="208524692">
        <id>97</id>
        <customer>
            <CustomerData HASH="2128670187"></CustomerData></customer>
        <billingAddress></billingAddress><deliveryAddress></deliveryAddress>
        <orderDetail>
            <list>
                <OrderDetailData HASH="516790072"></OrderDetailData>
                <OrderDetailData HASH="11226247"></OrderDetailData>
                <OrderDetailData HASH="11226247"></OrderDetailData>
            </list>
        </orderDetail>
        <log/>
    </OrderData>
    <OrderData HASH="1502226778"></OrderData>
</list>

I cannot find a solution to find the number of OrderDetailData elements? I also read http://iphonebyradix.blogspot.com/2011/03/using-gdata-to-parse-xml-file.html this url.

Thanks in advance.

Edit:

I am explaining my requirement again. In this xml there will be multiple OrderData element. Now I have to count the number of OrderDetailData elemnts from a particular OrderData element. Suppose that, according to my xml, the current parsed xml has one OrderData element, named id and its value is 97. Now, I have to count how many OrderDetailData elements are contained in the OrderData(whichid` is 97).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

油饼 2025-01-10 03:53:03

这是一个如何检索一些数据的简单示例。这个例子非常简单并且没有使用XPath表达式。我建议你先了解它是如何工作的,然后再使用XPath表达式。在我看来,如果您无法理解解析器的工作原理,那么使用 XPath 表达式是没有用的。

NSString* path = [[NSBundle mainBundle] pathForResource:@"test2" ofType:@"xml"];

NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData 
                                                       options:0 error:&error];
//NSLog(@"%@", doc.rootElement); // print the whole xml

NSArray *orderDataArray = [doc.rootElement elementsForName:@"OrderData"];

for (GDataXMLElement *orderDataElement in orderDataArray) {

    if([orderDataElement childCount] > 0)
    {            
        NSString *attributeForOrderDataElement = [(GDataXMLElement *) [orderDataElement attributeForName:@"HASH"] stringValue];   

        NSLog(@"attributeForOrderDataElement has value %@", attributeForOrderDataElement);

        GDataXMLElement* idElement = (GDataXMLElement*)[[orderDataElement elementsForName:@"id"] objectAtIndex:0];

        NSLog(@"id has value %@", idElement.stringValue);

        GDataXMLElement* orderDetailElement = (GDataXMLElement*)[[orderDataElement elementsForName:@"orderDetail"] objectAtIndex:0];

        GDataXMLElement* listElement = (GDataXMLElement*)[[orderDetailElement elementsForName:@"list"] objectAtIndex:0];            

        NSArray* orderDetailDataArray = [listElement elementsForName:@"OrderDetailData"];

        int count = 0;
        for (GDataXMLElement *orderDetailDataElement in orderDetailDataArray) {

            NSString *attributeForOrderDetailDataElement = [(GDataXMLElement *) [orderDetailDataElement attributeForName:@"HASH"] stringValue];

            NSLog(@"attributeForOrderDetailDataElement has value %@", attributeForOrderDetailDataElement);

            count++;
        }

        NSLog(@"%d", count);
    }
}

[doc release];
[xmlData release]; 

这是输出控制台:

attributeForOrderDataElement has value 208524692 <-- HASH value
id has value 97 <-- id value
attributeForOrderDetailDataElement has value 516790072 <-- HASH value
attributeForOrderDetailDataElement has value 11226247
attributeForOrderDetailDataElement has value 11226247
3 <-- the count

希望有帮助。

编辑

test2.xml 包含您的文件,但您可以将其作为字符串传递。您还可以将参数作为字符串传递,如下所示:

NSString* xmlString = @"<list>"
    "<OrderData HASH=\"1408108039\"></OrderData>"
    "<OrderData HASH=\"208524692\">"
    "<id>97</id>"
    "<customer>"
    "<CustomerData HASH=\"2128670187\"></CustomerData>"
    "</customer>"
    "<billingAddress></billingAddress>"
    "<deliveryAddress></deliveryAddress>"
    "<orderDetail>"
    "<list>"
    "<OrderDetailData HASH=\"516790072\"></OrderDetailData>"
    "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
    "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
    "</list>"
    "</orderDetail>"
    "<log/>"
    "</OrderData>"
    "<OrderData HASH=\"1502226778\"></OrderData>"
    "</list>";    

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:&error];  

This is a simple example how to retrieve some data. This example is very simple and not use XPath expression. I suggest you first understand how it works and then use XPath expression. In my opinion it is not useful to use XPath expression if you cannot understand how the parser works.

NSString* path = [[NSBundle mainBundle] pathForResource:@"test2" ofType:@"xml"];

NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData 
                                                       options:0 error:&error];
//NSLog(@"%@", doc.rootElement); // print the whole xml

NSArray *orderDataArray = [doc.rootElement elementsForName:@"OrderData"];

for (GDataXMLElement *orderDataElement in orderDataArray) {

    if([orderDataElement childCount] > 0)
    {            
        NSString *attributeForOrderDataElement = [(GDataXMLElement *) [orderDataElement attributeForName:@"HASH"] stringValue];   

        NSLog(@"attributeForOrderDataElement has value %@", attributeForOrderDataElement);

        GDataXMLElement* idElement = (GDataXMLElement*)[[orderDataElement elementsForName:@"id"] objectAtIndex:0];

        NSLog(@"id has value %@", idElement.stringValue);

        GDataXMLElement* orderDetailElement = (GDataXMLElement*)[[orderDataElement elementsForName:@"orderDetail"] objectAtIndex:0];

        GDataXMLElement* listElement = (GDataXMLElement*)[[orderDetailElement elementsForName:@"list"] objectAtIndex:0];            

        NSArray* orderDetailDataArray = [listElement elementsForName:@"OrderDetailData"];

        int count = 0;
        for (GDataXMLElement *orderDetailDataElement in orderDetailDataArray) {

            NSString *attributeForOrderDetailDataElement = [(GDataXMLElement *) [orderDetailDataElement attributeForName:@"HASH"] stringValue];

            NSLog(@"attributeForOrderDetailDataElement has value %@", attributeForOrderDetailDataElement);

            count++;
        }

        NSLog(@"%d", count);
    }
}

[doc release];
[xmlData release]; 

This is the output console:

attributeForOrderDataElement has value 208524692 <-- HASH value
id has value 97 <-- id value
attributeForOrderDetailDataElement has value 516790072 <-- HASH value
attributeForOrderDetailDataElement has value 11226247
attributeForOrderDetailDataElement has value 11226247
3 <-- the count

Hope it helps.

Edit

test2.xml contains your file but you could pass it as a string. You can also pass as parameters as string like the following:

NSString* xmlString = @"<list>"
    "<OrderData HASH=\"1408108039\"></OrderData>"
    "<OrderData HASH=\"208524692\">"
    "<id>97</id>"
    "<customer>"
    "<CustomerData HASH=\"2128670187\"></CustomerData>"
    "</customer>"
    "<billingAddress></billingAddress>"
    "<deliveryAddress></deliveryAddress>"
    "<orderDetail>"
    "<list>"
    "<OrderDetailData HASH=\"516790072\"></OrderDetailData>"
    "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
    "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
    "</list>"
    "</orderDetail>"
    "<log/>"
    "</OrderData>"
    "<OrderData HASH=\"1502226778\"></OrderData>"
    "</list>";    

GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:&error];  
冷血 2025-01-10 03:53:03

我发现 TouchXML 很容易解析,您可以直接从 xml 访问所需的数据

。 首先下载 TouchXML 并将 libxml2.dylib 框架添加到您的项目中。
更改“标题搜索路径”的构建设置并添加“/usr/include/libxml2”
将 TouchXML.h 导入到您的文件

//Access document 
CXMLDocument *parserDoc = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:&err];

//Access root element and access children in heirarchy
CXMLElement *root = [parserDoc rootElement];
NSArray *places = [[[root children] objectAtIndex:0] children];

Else

//Access node by node
NSString *location =[[[[[parserDoc nodesForXPath:@"/xml_api_reply/weather/forecast_information/city" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];

I found TouchXML easy in parsing and you can directly access data needed from xml

First of all Download TouchXML and add libxml2.dylib framework to your project.
change buildsetting for "Header Search Path" and add "/usr/include/libxml2"
Import TouchXML.h to your file

//Access document 
CXMLDocument *parserDoc = [[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:&err];

//Access root element and access children in heirarchy
CXMLElement *root = [parserDoc rootElement];
NSArray *places = [[[root children] objectAtIndex:0] children];

Else

//Access node by node
NSString *location =[[[[[parserDoc nodesForXPath:@"/xml_api_reply/weather/forecast_information/city" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];
青衫儰鉨ミ守葔 2025-01-10 03:53:03

如果 OrderDetailData 始终存在于数组的第二个对象中,您可以使用下面的方法,

NSArray* arr = [[XMLelement elementForName:@"list"] elementsForName:@"OrderData"];    
NSXMLElement* listElement = [[[arr objectAtIndex:1] elementForName:@"orderDetail"] elementForName:@"list"];
NSArray* orderDetailArray* = [listElement elementsForName:@"OrderDetailData"];

在这个中您可以获取数组中的 OrderDetailData 元素,并且您可以在循环中解析数据。获取HASH值使用。

NSString* hash = [[orderDetailArray objectAtIndex:0] stringValueForAttribute:@"HASH"];

If the OrderDetailData is present in 2nd object of array all the time, u can use the below

NSArray* arr = [[XMLelement elementForName:@"list"] elementsForName:@"OrderData"];    
NSXMLElement* listElement = [[[arr objectAtIndex:1] elementForName:@"orderDetail"] elementForName:@"list"];
NSArray* orderDetailArray* = [listElement elementsForName:@"OrderDetailData"];

In this u can get the OrderDetailData elements in the array, and u can parse the data in loop. to get HASH value use.

NSString* hash = [[orderDetailArray objectAtIndex:0] stringValueForAttribute:@"HASH"];
神经暖 2025-01-10 03:53:03

这是下面的代码,我已经尝试过了。我得到了正确的输出..检查一下

NSString* xmlString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                       "<list>"
                       "<OrderData HASH=\"1408108039\"></OrderData>"
                       "<OrderData HASH=\"208524692\">"
                       "<id>97</id>"
                       "<customer>"
                       "<CustomerData HASH=\"2128670187\"></CustomerData></customer>"
                       "<billingAddress></billingAddress><deliveryAddress></deliveryAddress>"
                       "<orderDetail>"
                       "<list>"
                       "<OrderDetailData HASH=\"516790072\"></OrderDetailData>"
                       "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
                       "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
                       "</list>"
                       "</orderDetail>"
                       "<log/></OrderData>"
                       "<OrderData HASH=\"1502226778\"></OrderData>"
                       "</list>";

NSXMLDocument*  doc = [[NSXMLDocument alloc] initWithData:[xmlString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSXMLElement* rootElement = [doc rootElement];

NSArray* arr = [rootElement elementsForName:@"OrderData"];
NSXMLElement* orderDataElement;

for (int i = 0; i < [arr count]; i++)
{
    int Id = 0;
    Id = [[arr objectAtIndex:i] integerValueForNode:@"id"];
    if (Id != 0)
    {
        orderDataElement = [arr objectAtIndex:i];
        break;
    }
}

NSArray* orderDetailArray = [[[orderDataElement elementForName:@"orderDetail"] 
                              elementForName:@"list"] 
                             elementsForName:@"OrderDetailData"];

for (int j = 0; j < [orderDetailArray count]; j++)
{
    NSLog(@"%@", [[orderDetailArray objectAtIndex:j] stringValueForAttribute:@"HASH"]);
}

This is the below code, i have tried. I got the right output.. check out

NSString* xmlString = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                       "<list>"
                       "<OrderData HASH=\"1408108039\"></OrderData>"
                       "<OrderData HASH=\"208524692\">"
                       "<id>97</id>"
                       "<customer>"
                       "<CustomerData HASH=\"2128670187\"></CustomerData></customer>"
                       "<billingAddress></billingAddress><deliveryAddress></deliveryAddress>"
                       "<orderDetail>"
                       "<list>"
                       "<OrderDetailData HASH=\"516790072\"></OrderDetailData>"
                       "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
                       "<OrderDetailData HASH=\"11226247\"></OrderDetailData>"
                       "</list>"
                       "</orderDetail>"
                       "<log/></OrderData>"
                       "<OrderData HASH=\"1502226778\"></OrderData>"
                       "</list>";

NSXMLDocument*  doc = [[NSXMLDocument alloc] initWithData:[xmlString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
NSXMLElement* rootElement = [doc rootElement];

NSArray* arr = [rootElement elementsForName:@"OrderData"];
NSXMLElement* orderDataElement;

for (int i = 0; i < [arr count]; i++)
{
    int Id = 0;
    Id = [[arr objectAtIndex:i] integerValueForNode:@"id"];
    if (Id != 0)
    {
        orderDataElement = [arr objectAtIndex:i];
        break;
    }
}

NSArray* orderDetailArray = [[[orderDataElement elementForName:@"orderDetail"] 
                              elementForName:@"list"] 
                             elementsForName:@"OrderDetailData"];

for (int j = 0; j < [orderDetailArray count]; j++)
{
    NSLog(@"%@", [[orderDetailArray objectAtIndex:j] stringValueForAttribute:@"HASH"]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文