简体   繁体   中英

How to find attribute of root node in touch xml?

NSString *xml = @"<?xml version="1.0" encoding="ISO-8859-15"?>
                  <ServerDateTime DateRequested="" DateSent="20141013_114855">
                     <DateTime>20141013_114857</DateTime>
                  </ServerDateTime>";

In above xml, how to find the attribute value of 'DataSent'?

I have tried by following, but i didn't get the value.

CXMLDocument *documentParser = [[CXMLDocument alloc]initWithData:[xml dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
            NSArray *arrayResult = [documentParser nodesForXPath:@"//ServerDateTime" error:nil];
            for(CXMLElement *element in arrayResult){
                NSString *value = [element name];
                if ([value isEqualToString:@"ServerDateTime"]) {
                    NSString *newLastSyncDate = [[element attributeForName:@"DataSent"] stringValue];    //it gives nil..
                }
            }

You may want to use XPath-queries to search for elements inside a XML. You have to look up if CXML supports this. Maybe also take a look at this question .

There someone is searching for a given attribute with an XPath-query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM