2
<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元素。现在我必须计算OrderDetailData来自特定OrderData元素的元素数量。假设,根据我的xml,当前解析的xml有一个OrderData元素,命名为97。现在,我要数一下OrderData id`中id有多少OrderDetailData元素是97)。(which

4

4 回答 4

10

这是一个如何检索一些数据的简单示例。这个例子很简单,没有使用 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];  
于 2012-02-06T12:09:08.897 回答
1

我发现 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];

别的

//Access node by node
NSString *location =[[[[[parserDoc nodesForXPath:@"/xml_api_reply/weather/forecast_information/city" error:nil] objectAtIndex:0] attributeForName:@"data"] stringValue] retain];
于 2012-02-06T07:16:04.717 回答
0

如果 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"];
于 2012-02-06T06:45:10.870 回答
0

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

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"]);
}
于 2012-02-06T11:38:27.807 回答