Question

I need to get the name of the first element in an XML document, e.g. the string "TitleName" of the XML snippet (which is the first line of an XML document):

<TitleName Major="1" Minor="0" Revision="1">

What XPath command (XPathRootName in the code snippet below) would I need to specify for this NSXMLDocument in order to get the string "TitleName"?

NSData *xmlData = [xmlStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSXMLDocument *xmlDocument = [[NSXMLDocument alloc]initWithData:xmlData 
      options:0 error:&error];

NSXMLElement *rootNameXMLElement = [[xmlDocument 
      nodesForXPath:XPathRootName error:&error] lastObject];;
NSString *rootName = rootNameXMLElement.stringValue;
Was it helpful?

Solution

The XPath expression /* would match the document element, but it would be simpler just to use the rootElement property of xmlDocument to get this element directly without using XPath.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top