<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
元素,名为id
,其值为97.现在,我必须计算包含多少OrderDetailData
个元素OrderData (which
id`是97)。
答案 0 :(得分: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];
答案 1 :(得分: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];
答案 2 :(得分: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"];
答案 3 :(得分: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"]);
}