objective-c
  • xml
  • xcode
  • xpath
  • gdataxml
  • 2012-04-13 3 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> 
    

    다음대로 구문 분석하는 나의 시도 :

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

    에서 :

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

    나는 또한 다음과 네임 스페이스를 떠나 시도했습니다 두 시나리오 모두 idList에는 0 명의 구성원이 있습니다. 나는 벽에 물건을 던지 듯이 붙어있는 것처럼 느껴진다. 누군가 내 길을 보여줄 수 있습니까?

    감사합니다.

    답변

    2

    XPath의 경우 //response:Protocol[response:Type='EXPR']/response:EwsUrl이 필요합니다. 나는 객관적으로 도움을 줄 수 없다.

    +0

    아, 그게 다야! 감사! – coopermj

    3

    마틴의 대답은 정확하지만 나는 왜 처음 읽지 못했던 지 (나 같은) 이유를 설명 할 수있을 것이라고 생각했습니다.

    네임 스페이스 매핑을 설정할 때 네임 스페이스에 키를 부여한 다음 XPath 식의 선택기 앞에 해당 키와 콜론 (:)을 붙입니다. 이 솔루션은 깔끔한 TouchXML library의 CXMLDocuments와 CXMLElements를 사용합니다.

    간단한 예제로 시작하려면 , 당신의 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]; 
    

    참고뿐 아니라 네임 스페이스가 선언 된 곳.

    관련 문제