Objective C - Parsing xml with namespaces using path

I am having some trouble parsing the following xml data:

<?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>

I am using the following code the read the xml file and parse it. I have tried every xpath combination I could think off, but couldn't get it to work:

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);

    }

} 

Answers


I'm using CXMLDocuments and CXMLElements and have just spent some time dealing with a similar issue (Google's KML files). Your problem is possibly due to the namespacing issue. When you set the namespace mappings, give the namespace a key, then prefix your selectors in your XPath expressions with that key followed by a colon (:). To start with a simple example, say your XML was:

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

You would select all the books with:

// 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];

Note that you need to prefix every element, not just the one where the namespace is declared. That's quite a namespace heavy XML doc you're dealing with, good luck.


This is a really easy XML parser to use.

http://www.tbxml.co.uk/TBXML/TBXML_Free.html


Need Your Help

Python Pathlib avoid Permission Errors when using Iterdir()

python pathlib

I was starting to build a "Directory Explorer" GUI for a small program I wrote and was using Pathlib since I hadn't used it before. Unfortunately I got stuck pretty much right off the bat iterating...

Is it possible to pass arguments to a python made exe at runtime?

python

I'm experimenting with file I/O. I have a small practice program that creates a text file when run. I packaged it with pyinstaller so that double clicking on the exe creates a new folder and places a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.