0

I have to parse the following xml file.

<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>

I only want to pick few values which are 50139553,Amy, 637270182 and 637270196 .

In general I focus on id, tag k="name" v="Amy" and nd ref="637270182".

This is the java code I used:

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");



            } 
        }
    }

The console shows me following output:

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: 

But I want it to have like:

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

So what can I do?

Best regards, Nazar

Nazar Medeiros
  • 437
  • 4
  • 10

2 Answers2

0

Checking for conditions (on ref & name of value of way object) while printing like below should help you!

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);
}

Alternatively you can print the values only when the node name is nd should print only that.

sakthisundar
  • 3,278
  • 3
  • 16
  • 29
0

The best way to search and traverse XML DOM documents is Xpath.
Here is the piece of code that extracts all you required. I added comments as much as I thought needed

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();
}

Output:

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

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47