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);
}
, 나는 다른 질문에 대한 답변에서 그하는 Pathes를 구축하는 식을 제안 :
아래 코드는 당신이 필요로하는 경로를 검색 http://stackoverflow.com/ A/16491186/695343 - 모든 atributes에도 잘 작동합니다. 각 속성에 대한 경로가있는 XPath 표현식 목록을 리턴합니다. –