0

Below is the XML I'm trying to parse with DOM, my code and my output. I need to get the information out of "Simple data" but I'm failing to do so.

XML:

<kml>
  <Document>
    <Folder id="kml_ft_Meter_Rates_and_Time_Limits">
      <name>Meter_Rates_and_Time_Limits</name>
      <Placemark id="kml_1">
        <name>$1.00 / hr 2hr time limit</name>
        <snippet> </snippet>
        <description><![CDATA[<center><table><tr><th colspan='2' align='center'><em>Attributes</em></th></tr><tr bgcolor="#E3E3F3">
          <th>RATE</th>
          <td>$1.00</td>
          </tr><tr bgcolor="">
          <th>LIMIT</th>
          <td>2hr</td>
          </tr></table></center>]]>
        </description>
        <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>
</kml>

Code filled with sysouts for debugging purposes:

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        System.out.println("Root 1st child :" + doc.getDocumentElement().getChildNodes().item(1).getNodeName());
        System.out.println("Document 1st child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(1).getNodeName());
        System.out.println("Document 2nd child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(2).getNodeName());
        System.out.println("Document 3rd child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(3).getNodeName());
        System.out.println("Document 4th child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(4).getNodeName());
        System.out.println("Document 5th child :" + doc.getDocumentElement().getChildNodes().item(1).getChildNodes().item(5).getNodeName());
        System.out.println("-----------------------");


        NodeList nList = doc.getElementsByTagName("Placemark");
        nList = nList.item(1).getChildNodes();
        System.out.println("Placemark list, 1st placemark 1st child :" + nList.item(1).getNodeName());
        System.out.println("Placemark list, 1st placemark 2nd child :" + nList.item(2).getNodeName());
        System.out.println("Placemark list, 1st placemark 3rd child :" + nList.item(3).getNodeName());
        System.out.println("Placemark list, 1st placemark 4th child :" + nList.item(4).getNodeName());
        System.out.println("-----------------------");
        System.out.println("Placemark list, 1st placemark 9th child :" + nList.item(9).getNodeName());
        System.out.println("-----------------------");
        nList = nList.item(9).getChildNodes();
        System.out.println("Extended data, 1st child :" + nList.item(1).getNodeName());
        System.out.println("-----------------------");
        System.out.println("Schema data, 1st child :" + nList.item(1).getChildNodes().item(1).getNodeName());
        System.out.println("Simple data :" + nList.item(1).getChildNodes().item(4).getNodeName());
        System.out.println("-----------------------");
        System.out.println("Schema data, 2nd child :" + nList.item(1).getChildNodes().item(3).getNodeName());
        System.out.println("Simple data :" + nList.item(1).getChildNodes().item(4).getNodeName());

Console output:

Root element :kml
Root 1st child :Document
Document 1st child :name
Document 2nd child :#text
Document 3rd child :visibility
Document 4th child :#text
Document 5th child :Style
-----------------------
Placemark list, 1st placemark 1st child :name
Placemark list, 1st placemark 2nd child :#text
Placemark list, 1st placemark 3rd child :snippet
Placemark list, 1st placemark 4th child :#text
-----------------------
Placemark list, 1st placemark 9th child :ExtendedData
-----------------------
Extended data, 1st child :SchemaData
-----------------------
Schema data, 1st child :SimpleData
Simple data :#text
-----------------------
Schema data, 2nd child :SimpleData
Simple data :#text
4
  • your node 'name' is a child of 'Folder' node, how can you treat it as a child of 'Document' node. Same is the issue with other nodes, thatswhy you see #text instead everywhere. Commented Oct 20, 2012 at 21:01
  • There's 2 nodes called name, one is the first child of document, the other one is the first child of placemark, but I didn't want to post the complete xml cause it's very long. Actually make that 3 types of node called name. 1) 1st child of document 2) 1st child of folder and 3) 1st child of placemark Commented Oct 20, 2012 at 21:06
  • item indexes start from 0 and not from 1 Commented Oct 20, 2012 at 21:09
  • Makes no difference when traversing through. Commented Oct 20, 2012 at 21:14

2 Answers 2

1

nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(1).getTextContent() --> prints $1.00 and

nList.item(0).getChildNodes().item(9).getChildNodes().item(1).getChildNodes().item(3).getTextContent() --> prints the 2hr.

Here nList is used after this line NodeList nList = doc.getElementsByTagName("Placemark");. Please fix your traversal accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again Yogendra ;), getTextContent() was my main issue.
0

I am not certain of what you exactly want. Maybe elaborate a bit more.

org.w3c.Node has a method getTextContent(). In general these w3c classes use casts of item(i) to for instance Element.

To skip whitespace Text (node name #text`), or simply more directly access specific elements, one uses XPath.

1 Comment

I need to get $1.00 and 2hr in the form of a string out of: <SimpleData name="RATE">$1.00</SimpleData> <SimpleData name="LIMIT">2hr</SimpleData>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.