objective-c
  • xml
  • xcode
  • xpath
  • gdataxml
  • 2012-04-13 18 views 1 likes 
    1

    xpathとGDataXMLを使用してXMLを解析するのに問題があります。xpathテスターを使用すると、文字列がうまくいくように見えますが、名前空間を追加すると干渉しているようです。GDataXML、名前空間、およびxpath

    XML:次のようにそれを解析する

    <?xml version="1.0" encoding="utf-8"?> 
    
    <Autodiscover xmlns='http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006'> 
          <Response xmlns='http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a'> 
         <Account> 
         <Protocol> 
           <Type>EXPR</Type> 
           <EwsUrl>https://server.my.dom/EWS/Exchange.asmx</EwsUrl> 
         </Protocol> 
         </Account> 
         </Response> 
    </Autodiscover> 
    

    私の試みは次のとおりです。

    NSDictionary *namespaceMappings = [NSDictionary dictionaryWithObjectsAndKeys:@"http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a", @"response", nil]; 
    
        NSArray *idList = [doc nodesForXPath:@"//response:Protocol[Type='EXPR']/EwsUrl" namespaces:namespaceMappings error:nil]; 
    

    私はまた、次のように名前空間を除外しようとしました:

    NSArray *idList = [doc nodesForXPath:@"//Protocol[Type='EXPR']/EwsUrl" error:nil]; 
    

    でどちらのシナリオでも、idListのメンバーは0人です。あたかも何かがついているかどうかを知るために壁に物を投げているように感じる。誰かが私に方法を示すことができますか?

    ありがとうございます!

    答えて

    2

    XPathについては、//response:Protocol[response:Type='EXPR']/response:EwsUrlが必要です。私は客観的なものを手伝ってはいけません。

    +0

    ああ、それでした!ありがとう! – coopermj

    3

    マーティンの答えは正しいですが、私は最初の読書(私のような)でそれを逃した人々のためになぜ私はちょうど綴りと思った。

    名前空間のマッピングを設定するときは、名前空間にキーを付け、XPath式のセレクタの前にそのキーとコロン(:)を付けます。このソリューションでは、CXMLDocumentsとCXMLElementsをニースのTouchXML libraryから使用しています。簡単な例から始めること

    は、あなたの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]; 
    

    注意だけでなく、名前空間が宣言されている場所。

    関連する問題