我在解析以下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文件并解析它。我已经尝试了我能想到的每个xpath组合,但无法让它工作:
NSData *encryptData = [self getResourceFileName:filename extension:fileext readFileInZip:@"encryption.xml"]; //this is a function to retrieve files from a zip file. This is working
if(encryptData != nil){
//http://www.w3.org/2001/04/xmlenc#
CXMLDocument* cryptFile = [[CXMLDocument alloc] initWithData:encryptData options:0 error:nil];
NSArray *encryptionItems = [cryptFile nodesForXPath:@"EncryptedData" namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.w3.org/2001/04/xmlenc#" forKey:@""] error:nil];
for (CXMLElement* encEl in encryptionItems) {
NSArray *uuidArray = [encEl nodesForXPath:@"KeyInfo/resource" namespaceMappings:nil error:nil];
NSString *uuid = [[uuidArray objectAtIndex:0] stringValue];
NSArray *fileArray = [encEl nodesForXPath:@"CipherData/CipherReference" namespaceMappings:nil error:nil];
NSString *fileRef = [[fileArray objectAtIndex:0] stringValue];
NSLog(@"File: %@ - UUID: %@",fileRef,uuid);
}
}
答案 0 :(得分:0)
这是一个非常容易使用的XML解析器。
答案 1 :(得分:0)
我正在使用CXMLDocuments和CXMLElements,并且花了一些时间来处理类似的问题(谷歌的KML文件)。您的问题可能是由于命名空间问题。设置命名空间映射时,为命名空间指定一个键,然后在XPath表达式中为选择器添加前缀,后跟冒号(:)。从一个简单的例子开始,假设你的XML是:
<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];
请注意,您需要为每个元素添加前缀,而不仅仅是声明命名空间的元素。这是你正在处理的一个名称空间繁重的XML文档,祝你好运。