많은 링크가 있다는 것을 알고 있지만 해결책을 찾을 수 없습니다. 내가 "필드 이름 = 'ID'의 값을 취득 할XML이 특정 속성에서 노드의 값을 얻습니다.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Entity Type="test-folder">
<Fields>
<Field Name="item-version"/>
<Field Name="id">
<Value>5386</Value>
</Field>
<Field Name="ver-stamp">
<Value>2</Value>
</Field>
<Field Name="parent-id">
<Value>5310</Value>
</Field>
<Field Name="system">
<Value>A</Value>
</Field>
<Field Name="last-modified">
<Value>2014-03-03 16:35:24</Value>
</Field>
<Field Name="description">
<Value/>
</Field>
<Field Name="hierarchical-path">
<Value>AAAAAPAADAACADC</Value>
</Field>
<Field Name="view-order">
<Value>0</Value>
</Field>
<Field Name="name">
<Value>TC77</Value>
</Field>
<Field Name="attachment">
<Value/>
</Field>
</Fields>
: 이것은 내 XML 파일입니다
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
Document doc = builder.parse(is);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//Field[@Name=\"id\"]");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
System.out.println(nl.getLength());
for(int i=0;i<nl.getLength();i++){
Node currentItem = nl.item(i);
String key = currentItem.getAttributes().getNamedItem("Name").getNodeValue();
System.out.println(key);
}
: 내 검색을 바탕으로 좀 코드로 올라와있다 "5386입니다. 죄송합니다. 방금 다시 게시하는 중이라면 정말 답을 얻을 수 없었습니다.
이 방법을 사용하면 여러 텍스트 노드를 직접 연결해야합니다. 일반적으로'string (...) '함수를 사용하여 XPath 엔진이이를 수행하도록하는 것이 더 간단합니다. – Karsten