2013-05-19 2 views
3

XML 파일에서 XPath로 파싱하여 모든 매개 변수를 가져온 다음 각 매개 변수의 전체 경로를 가져 오려고하지만 어떤 이유로 든 내 코드가 작동하지 않습니다. 강령 :Java의 XML 파일에서 노드의 전체 경로를 얻는 방법은 무엇입니까?

ArrayList<String> names = new ArrayList<String>(); 
ArrayList<String> paths = new ArrayList<String>(); 
URL oracle = new URL("http://weather.yahooapis.com/forecastrss?w=2502265"); 
InputStream is = oracle.openStream(); 
org.w3c.dom.Document doc = null; 
DocumentBuilderFactory domFactory; 
DocumentBuilder builder; 
try { 
    domFactory = DocumentBuilderFactory.newInstance(); 
    domFactory.setNamespaceAware(true); 
    builder = domFactory.newDocumentBuilder(); 
    doc = builder.parse(is); 
} catch (Exception ex) { 
    System.err.println("unable to load XML: " + ex); 
} 

XPathFactory factory = XPathFactory.newInstance(); 
XPath xpath = factory.newXPath(); 
XPathExpression expr = xpath.compile("//*/@*"); 
Object result = expr.evaluate(doc, XPathConstants.NODESET); 
NodeList nl = (NodeList) result; 
for(int j=0 ; j < nl.getLength() ; j++){ 
    names.add(nl.item(j).getNodeName()); 
    Node node = nl.item(j); 
    ArrayList<String> parents = new ArrayList<String>(); 
    while(node.getParentNode() != null){ // it didn't even gone through this loop 
     parents.add(node.getNodeName()); 
     node = node.getParentNode(); 
    } 
    System.out.println(parents); 
}  
+0

, 나는 다른 질문에 대한 답변에서 그하는 Pathes를 구축하는 식을 제안 :

아래 코드는 당신이 필요로하는 경로를 검색 http://stackoverflow.com/ A/16491186/695343 - 모든 atributes에도 잘 작동합니다. 각 속성에 대한 경로가있는 XPath 표현식 목록을 리턴합니다. –

답변

2

표현은 //*/@* 빈 노드 집합을 반환합니다.

당신이 XPath는 2.0 좋은 경우
import org.w3c.dom.Attr; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 

public class SO { 

    @SuppressWarnings("nls") 
    public static void main(String[] args) throws Exception { 
     List<String> names = new ArrayList<>(); 
     URL oracle = 
     new URL("http://weather.yahooapis.com/forecastrss?w=2502265"); 
     InputStream is = oracle.openStream(); 
     org.w3c.dom.Document doc = null; 
     DocumentBuilderFactory domFactory; 
     DocumentBuilder builder; 
     try { 
     domFactory = DocumentBuilderFactory.newInstance(); 
     domFactory.setNamespaceAware(true); 
     builder = domFactory.newDocumentBuilder(); 
     doc = builder.parse(is); 
     } catch (Exception ex) { 
     System.err.println("unable to load XML: " + ex); 
     } 
     XPathFactory factory = XPathFactory.newInstance(); 
     XPath xpath = factory.newXPath(); 
     XPathExpression expr = xpath.compile("//*:*/@*"); 
     Object result = expr.evaluate(doc, XPathConstants.NODESET); 
     NodeList nl = (NodeList) result; 
     for(int j=0 ; j < nl.getLength() ; j++){ 
     names.add(nl.item(j).getNodeName()); 
     Node node = nl.item(j); 
     String path = "." + node.getNodeName() + " = " + node.getNodeValue(); 
     node = ((Attr)node).getOwnerElement(); 
     while(node != null) { 
      path = node.getNodeName() + '/' + path; 
      node = node.getParentNode(); 
     } 
     System.out.println(path); 
     } 
    } 
} 
+0

오, 미안하지만, 이미 그것을 만들었지 만 j <2는 테스트 일이었고, 나는 나의 질문에있는 코드를 편집했다. –

+0

올바른 XPath 표현식은 // @ *이며 getParent() 대신 getOwnerElement()를 사용합니다. – Aubin

+0

XPath 표현식은 괜찮 았지만 네임 스페이스 ('xmlns : yweather = "http : // xml .weather.yahoo.com/ns/rss/1.0 "xmlns : geo ="http://www.w3.org/2003/01/geo/wgs84_pos# "'), 그는'// * : */@ *'_all_ 네임 스페이스를 쿼리하거나 다른 방법으로 처리합니다. –

관련 문제