Java - XML Parser(DOM-Parser)

时间:2015-07-09 10:15:48

标签: 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"/>
   <tag k="railway" v="platform"/>
   <tag k="shelter" v="yes"/>
   <tag k="tactile_paving" v="yes"/>
   <tag k="wheelchair" v="yes"/>
</way>

我只想选择一些 50139553 Amy 637270182 637270196 的值。

一般情况下,我会关注 id 标记k =&#34; name&#34; v =&#34; Amy&#34; nd ref =&#34; 637270182&#34;

这是我使用的java代码:

Way _way = new Way();
    for (int zl = 0; zl < wayList.getLength(); zl++) {                

        Node way = wayList.item(zl);

        if (way.hasAttributes()) {
            String id = way.getAttributes().getNamedItem("id").getNodeValue();
            _way.id = id;
        }

        for(int k = 0; k  < way.getChildNodes().getLength();++k) {

            Node childNode = way.getChildNodes().item(k);

            if (childNode.getNodeType() == Node.ELEMENT_NODE) {

                Element fin = (Element) childNode;                      

                if(fin.getAttribute("k").contains("name")) {
                    _way.name = fin.getAttribute("v");
                } 


                if(_way.name == null) {             
                    waysOhneNamenIDList.add(_way.id);
                } 


                System.out.println("Way-Id: " + _way.id + " Way-Name: " + _way.name + " Way-Ref: " + _way.ref);

                _way.ref = fin.getAttribute("ref");



            } 
        }
    }

控制台显示以下输出:

Way-Id: 50139553 Way-Name: null Way-Ref: null
Way-Id: 50139553 Way-Name: null Way-Ref: 637270182
Way-Id: 50139553 Way-Name: Amy Way-Ref: 637270196
Way-Id: 50139553 Way-Name: Amy Way-Ref: 
Way-Id: 50139553 Way-Name: Amy Way-Ref: 
Way-Id: 50139553 Way-Name: Amy Way-Ref: 
Way-Id: 50139553 Way-Name: Amy Way-Ref: 
Way-Id: 50139553 Way-Name: Amy Way-Ref: 
Way-Id: 50139553 Way-Name: Amy Way-Ref: 
Way-Id: 50139553 Way-Name: Amy Way-Ref: 

但我希望它有:

Way-Id: 50139553 Way-Name: Amy Way-Ref: 637270182
Way-Id: 50139553 Way-Name: Amy Way-Ref: 637270196

那我该怎么办?

祝你好运, 纳扎尔

2 个答案:

答案 0 :(得分:0)

在如下打印时检查条件(参考方式对象的参考号和名称)可以帮助您!

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时,才能打印值。

答案 1 :(得分:0)

搜索和遍历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();
}

输出:

Way-Id:50139553 Way-Name:Amy Way-Ref:637270182 Way-Ref:637270196

相关问题