2012-10-15 3 views
1

내 XML에서 특정 이름을 검색하고 해당 값을 검색하려고합니다.xml 이름 - 값 필드 구문 분석

<n0:field> 
    <n0:name n4:type="n3:string" xmlns:n3="http://www.w3.org/2001/XMLSchema" xmlns:n4="http://www.w3.org/2001/XMLSchema-instance">LifePolicyID</n0:name> 
    <n0:value n6:type="n5:string" xmlns:n5="http://www.w3.org/2001/XMLSchema" xmlns:n6="http://www.w3.org/2001/XMLSchema-instance">1</n0:value> 
</n0:field> 

내가 LifePolicyID 이름의 값을 얻을려고 :

예를 들어 내가이 분야가있다.

programatticly 할 방법이 있습니까?

는 지금은이 같은 XPath는을저기서 오전 :

이름은이 경우에
XPathExpression xpe = xpath.compile("//*[name/text()='" + name +"']/value"); 

이 LifePolicyID입니다. 그러나 그것은 작동하지 않습니다.

아이디어가 있으십니까?

답변

0

코드는

String xml = 
    "<n0:field xmlns:n0='http://test/uri'>" + 
    " <n0:name n4:type='n3:string' xmlns:n3='http://www.w3.org/2001/XMLSchema' xmlns:n4='http://www.w3.org/2001/XMLSchema-instance'>LifePolicyID</n0:name>" + 
    " <n0:value n6:type='n5:string' xmlns:n5='http://www.w3.org/2001/XMLSchema' xmlns:n6='http://www.w3.org/2001/XMLSchema-instance'>1</n0:value>" + 
    "</n0:field>"; 
String name = "LifePolicyID"; 
XPath xpath = XPathFactory.newInstance().newXPath(); 
XPathExpression xpe = xpath.compile("//*[name/text()='" + name +"']/value"); 
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(new StringReader(xml))); 
System.out.println(xpe.evaluate(doc)); 
나를 위해 작동하는 것 같다