2013-05-15 2 views
0

안녕하세요 모두 내가 XPath는XPath는이 표현을 수락하지

/ABCD/nsanity/component_details [@ 구성 요소 = "UCS"]/command_details [< * configScope inHierarchical = "true"를 쿠키에 대한 질문에 미칠 = " {COOKIE} "DN ="조직 루트 "* />]/collected_data

내가 XPath는 문 위의 문자열을 검색 할하지만 난이 XPath를 제공하고 때이를 던지고 평가 해에 대한 표현을 XPATH하기 예외 :

발생 원인 : javax.xml.transform.TransformerException : 위치 경로가 필요했지만 다음 토큰이 발생했습니다. < configScope

+1

데이터의 예를 표시하고 코드의 형식을 올바르게 지정하십시오 (쿼리에 포함 된 별인지 여부). 게시물의 형식을 지정하는 방법에 대한 [FAQ]를 읽어보십시오. –

+0

내 검색어에만 포함되지 않는 별이 없습니다 < and /> 기호가 예외를 던지고 있습니다 – Gopal

+0

아래 답변이 예상과 맞지 않으면 이전 메모에 게시 한 내용을 다시 생각해보십시오. 그렇듯이 질문은 모호하다. "내 검색어에 포함되지 않는 별이 없다"는 것은 무엇을 의미합니까? –

답변

1

XPath 표현식의 굵은 부분은 유효한 술어 표현식이 아닙니다. 나는 단지 당신이 무엇을 달성하기를 원하는지 짐작할 수 있습니다. 당신이 inHierarchical="true", cookie="{COOKIE}"dn="org-root"로 설정 속성을 가진 <configScope/> 자식 요소가 단지 <command_details/> 요소를 원하는 경우 XPath 표현식은 다음과 같아야합니다

<abcd> 
    <nsanity> 
    <component_details component="ucs"> 
     <command_details> 
     <configScope inHierarchical="true" cookie="{COOKIE}" dn="org-root" /> 
     <collected_data>Yes</collected_data> 
     </command_details> 
     <command_details> 
     <configScope inHierarchical="true" cookie="{COOKIE}" dn="XXX"/> 
     <collected_data>No</collected_data> 
     </command_details> 
    </component_details> 
    </nsanity> 
</abcd> 

: 여기

/abcd/nsanity/component_details[@component='ucs']/command_details[configScope[@inHierarchical='true' and @cookie='{COOKIE}' and @dn='org-root']]/collected_data 

이 예제 XML이다 다음 Java 프로그램은 XML 파일 test.xml을 읽고 XPath 표현식을 평가합니다 (그리고 요소 <collected_data/>의 텍스트 노드를 인쇄합니다.)

import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.Element; 
import org.w3c.dom.NodeList; 


public class Test { 

    public static void main(String[] args) throws Exception { 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    Document document = dbf.newDocumentBuilder().parse("test.xml"); 

    XPath xpath = XPathFactory.newInstance().newXPath() ; 

    NodeList nl = (NodeList) xpath.evaluate("/abcd/nsanity/component_details[@component='ucs']/command_details[configScope[@inHierarchical='true' and @cookie='{COOKIE}' and @dn='org-root']]/collected_data", document, XPathConstants.NODESET); 
    for(int i = 0; i < nl.getLength(); i++) { 
     Element el = (Element) nl.item(i); 
     System.out.println(el.getTextContent()); 
    } 
    } 
}