Patricio Jerí
Patricio Jerí

Reputation: 526

DOM Parser in Java, with attributes and tag values

    <Document>
    <Folder>
        <Placemark id="kml_1">
            <name>$1.00 / hr 2hr time limit</name>
            <snippet> </snippet>
            <styleUrl>#ParkingMeterStyler_KMLStyler</styleUrl>
            <ExtendedData>
                <SchemaData schemaUrl="#Meter_Rates_and_Time_Limits">
                    <SimpleData name="RATE">$1.00</SimpleData>
                    <SimpleData name="LIMIT">2hr</SimpleData>
                </SchemaData>
            </ExtendedData>
            <LineString>
                <coordinates>-123.100739208611,49.2630169018194,0 -123.100348847572,49.2630078055425,0 </coordinates>
            </LineString>
        </Placemark>
</Folder>
</Document>

I'm having trouble extracting the "$1.00" and the "2hr" in:

<SimpleData name="RATE">$1.00</SimpleData>
<SimpleData name="LIMIT">2hr</SimpleData>

So far, my code looks like this and extracts the "name" and "coordinates" successfully:

public class Debuger {

    public static void main(String argv[]) {

      try {

        File fXmlFile = new File("parkingMeter.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("Placemark");
        System.out.println("-----------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

           Node nNode = nList.item(temp);
           if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;

              System.out.println("Name : " + getTagValue("name", eElement));

              if(eElement.getAttribute("name").equals("RATE")){
                  System.out.println("Rate : " + getTagValue("SimpleData", eElement));
              }else if(eElement.getAttribute("name").equals("LIMIT")) {
                   System.out.println("Limit : " + getTagValue("SimpleData", eElement));
                }

              System.out.println("Coordinates : " + getTagValue("coordinates", eElement));

           }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
  }

  private static String getTagValue(String sTag, Element eElement) {
    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

    return nValue.getNodeValue();
  }

}

I'm really stuck here, help would be much appreciated.

Upvotes: 0

Views: 295

Answers (1)

Yogendra Singh
Yogendra Singh

Reputation: 34367

Here is the problem:

Your <ExtendedData> tag is 4th child node of <Placemark> node and you are only traversing tag children e.g. node and through getTagValue, you are getting first grand child.

   NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

Please correct the traversal by iterating child nodes of node. It should work.

Upvotes: 1

Related Questions