Java-XML解析器(DOM解析器)

Java-XML解析器(DOM解析器),java,xml-parsing,domparser,xml-attribute,child-nodes,Java,Xml Parsing,Domparser,Xml Attribute,Child Nodes,我必须解析以下xml文件 <way id="50139553"> <nd ref="637270182"/> <nd ref="637270196"/> <tag k="name" v="Amy"/> <tag k="bench" v="yes"/> <tag k="bin" v="yes"/> <tag k="public_transport" v="platform"/>

我必须解析以下xml文件

<way id="50139553">
   <nd ref="637270182"/>
   <nd ref="637270196"/>
   <tag k="name" v="Amy"/>
   <tag k="bench" v="yes"/>
   <tag k="bin" v="yes"/>
   <tag k="public_transport" v="platform"/>
   <tag k="railway" v="platform"/>
   <tag k="shelter" v="yes"/>
   <tag k="tactile_paving" v="yes"/>
   <tag k="wheelchair" v="yes"/>
</way>
但我希望它有如下特点:

Way-Id: 50139553 Way-Name: Amy Way-Ref: 637270182
Way-Id: 50139553 Way-Name: Amy Way-Ref: 637270196
那我该怎么办

致以最良好的祝愿, Nazar

在如下打印时检查条件(在way对象的值的ref和name上),应该会对您有所帮助

if (_way.name != null && !_way.name.trim().isEmpty() && (_way.ref != null && !_way.ref.trim().isEmpty()) {
   System.out.println("Way-Id: " + _way.id + " Way-Name: " + _way.name + " Way-Ref: " + _way.ref);
}

或者,您也可以仅在节点名为nd时打印值,但应仅打印该值。

搜索和遍历XML DOM文档的最佳方法是Xpath。
下面是一段代码,它提取了您需要的所有内容。我添加了我认为需要的评论

import javax.xml.parsers.*;
import org.w3c.dom.*;

String wayNodePattern = "//way";                // search all way tags in the document
String wayIdPattern = "./@id";                  // retrieve id attribute of current node
String wayNamePattern = "./tag[@k='name']/@v";  // retrieve v attribute of tag node under current node where k attribute is "name"
String wayRefPattern = "./nd/@ref";             // retrieve ref attribute of nd node under current node

XPath xPath =  XPathFactory.newInstance().newXPath();
Document doc = ...
NodeList wayList = (NodeList)xPath.compile(wayNodePattern)
        .evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < wayList.getLength() ; i++) {
    Node way = wayList.item(i);
    String wayId = (String)xPath.compile(wayIdPattern).evaluate(way, XPathConstants.STRING);
    String wayName = (String)xPath.compile(wayNamePattern).evaluate(way, XPathConstants.STRING);
    NodeList wayRefList = (NodeList)xPath.compile(wayRefPattern).evaluate(way, XPathConstants.NODESET);
    System.out.print("Way-Id:" + wayId + " Way-Name:" + wayName);
    for (int j = 0; j < wayRefList.getLength() ; j++) {
        Node wayRef = wayRefList.item(j);
        System.out.print(" Way-Ref:" + wayRef.getNodeValue());
    }
    System.out.println();
}
import javax.xml.parsers.*;
导入org.w3c.dom.*;
字符串wayNodePattern=“//way”//搜索文档中的所有路径标记
字符串wayIdPattern=“../@id”//检索当前节点的id属性
String wayNamePattern=“./tag[@k='name']/@v”;//检索当前节点下标记节点的v属性,其中k属性为“name”
字符串wayRefPattern=“/nd/@ref”;//检索当前节点下nd节点的ref属性
XPath=XPathFactory.newInstance().newXPath();
文档文档=。。。
NodeList wayList=(NodeList)xPath.compile(wayNodePattern)
.evaluate(doc,XPathConstants.NODESET);
对于(int i=0;i
输出:

道路Id:50139553道路名称:艾米道路参考号:637270182道路参考号:637270196

if (_way.name != null && !_way.name.trim().isEmpty() && (_way.ref != null && !_way.ref.trim().isEmpty()) {
   System.out.println("Way-Id: " + _way.id + " Way-Name: " + _way.name + " Way-Ref: " + _way.ref);
}
import javax.xml.parsers.*;
import org.w3c.dom.*;

String wayNodePattern = "//way";                // search all way tags in the document
String wayIdPattern = "./@id";                  // retrieve id attribute of current node
String wayNamePattern = "./tag[@k='name']/@v";  // retrieve v attribute of tag node under current node where k attribute is "name"
String wayRefPattern = "./nd/@ref";             // retrieve ref attribute of nd node under current node

XPath xPath =  XPathFactory.newInstance().newXPath();
Document doc = ...
NodeList wayList = (NodeList)xPath.compile(wayNodePattern)
        .evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < wayList.getLength() ; i++) {
    Node way = wayList.item(i);
    String wayId = (String)xPath.compile(wayIdPattern).evaluate(way, XPathConstants.STRING);
    String wayName = (String)xPath.compile(wayNamePattern).evaluate(way, XPathConstants.STRING);
    NodeList wayRefList = (NodeList)xPath.compile(wayRefPattern).evaluate(way, XPathConstants.NODESET);
    System.out.print("Way-Id:" + wayId + " Way-Name:" + wayName);
    for (int j = 0; j < wayRefList.getLength() ; j++) {
        Node wayRef = wayRefList.item(j);
        System.out.print(" Way-Ref:" + wayRef.getNodeValue());
    }
    System.out.println();
}