3

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

    }

} 
4

2 に答える 2

0

これは非常に使いやすい XML パーサーです。

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

于 2012-04-04T17:12:19.010 に答える
0

私は 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>

次のすべての本を選択します。

// 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 ドキュメントです。頑張ってください。

于 2013-02-14T11:17:32.557 に答える