這取決於你的數據和你的散列圖的結構:什麼是關鍵,如果值。
然而,DOM和XPath做的工作:
String xml= // your xml
DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xml)));
String expression="//header"; // Same for body, ...
XPathExpression expr = xpath.compile(expression) ;
NodeList nodes = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int k = 0; k < nodes.getLength(); k++) {
// Do what you want with that
希望它有助於
您可以使用JAXB將其綁定到一個POJO(我想你將不得不實行'javax.xml。 bind.annotation.adapters.XmlAdapter'並使用'@ XmlJavaTypeAdapter'註釋傳遞它)或使用'javax.xml.xpath.Xpath' – theo