Objective c Objective C-使用path解析具有名称空间的xml

Objective c Objective C-使用path解析具有名称空间的xml,objective-c,xml,xpath,namespaces,Objective C,Xml,Xpath,Namespaces,解析以下xml数据时遇到一些问题: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container"> <EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#"> <EncryptionMethod Algorithm="

解析以下xml数据时遇到一些问题:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<encryption xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<resource xmlns="http://ns.nuts-for-africa.com/epubdrm">urn:uuid:7297037a-6a5e-4bb1-bfa3-9a683288adb5</resource>
</KeyInfo>
<CipherData>
<CipherReference URI="OPS/epubbooksinfo.html"/>
</CipherData>
</EncryptedData>
<EncryptedData xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc"/>
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<resource xmlns="http://ns.nuts-for-africa.com/epubdrm">urn:uuid:7297037a-6a5e-4bb1-bfa3-9a683288adb5</resource>
</KeyInfo>
<CipherData>
<CipherReference URI="OPS/chapter-008.html"/>
</CipherData>
</EncryptedData>

这是一个非常容易使用的XML解析器


我正在使用CXMLDocuments和CXMLElements,刚刚花了一些时间处理一个类似的问题(Google的KML文件)。您的问题可能是由于名称空间问题。设置名称空间映射时,为名称空间指定一个键,然后在XPath表达式中的选择器前面加上该键和冒号(:)。从一个简单的例子开始,假设您的XML是:

<books xmlns="http://what.com/ever">
  <book>
    <title>Life of Pi</title>
  </book>
  <book>
    <title>Crime and Punishment</book>
  </book
</books>

请注意,您需要为每个元素添加前缀,而不仅仅是声明名称空间的元素。您正在处理的是一个相当重于名称空间的XML文档,祝您好运。

非常抱歉,但我有义务使用path和CXMLDocuments以及CXMLElements
<books xmlns="http://what.com/ever">
  <book>
    <title>Life of Pi</title>
  </book>
  <book>
    <title>Crime and Punishment</book>
  </book
</books>
// So, assuming you've already got the data for the XML document
CXMLDocument* xmldoc = [[CXMLDocument alloc] initWithData:xmlDocData options:0 error:nil];
NSDictionary *namespaceMappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://what.com/ever", @"nskey", nil];
NSError *error = nil;
NSArray *bookElements = [xmldoc nodesForXPath:@"/nskey:books/nskey:book" namespaceMappings:mappings error:&error];