我必须解析以下 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="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
那么我能做什么呢?
最好的问候, 纳扎尔