0

我必须解析以下 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>

我只想选择几个值,它们是50139553Amy637270182637270196

一般来说,我专注于id标签 k="name" v="Amy"nd ref="637270182"

这是我使用的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

那么我能做什么呢?

最好的问候, 纳扎尔

4

2 回答 2

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

于 2015-07-09T11:00:26.983 回答
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应仅打印该值时打印这些值。

于 2015-07-09T10:28:45.653 回答