Jaya Mayu
Jaya Mayu

Reputation: 17257

Reading contents of the XML using java

I'm trying to read an XML file using java. I can sucessfully read the file but the problem is, I don't know how to read the values inside the column tag.

Since the column tags are not unique, I have no idea how to read them. Can someone help me.

Thanks in advance.

 import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader {

 public static void main(String argv[]) {

  try {
      //new code
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(new URL("http://www.cse.lk/listedcompanies/overview.htm?d-16544-e=3&6578706f7274=1").openStream());

      doc.getDocumentElement().normalize();
      System.out.println("Root element " + doc.getDocumentElement().getNodeName());
      NodeList nodeLst = doc.getElementsByTagName("row");
      System.out.println("Information of all Stocks");

      for (int s = 0; s < nodeLst.getLength(); s++) {

        Node fstNode = nodeLst.item(s);

        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

          Element fstElmnt = (Element) fstNode;
          //NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("column");
          //Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
          //NodeList fstNm = fstNmElmnt.getChildNodes();
          //System.out.println("First Tag : "  + ((Node) fstNm.item(0)).getNodeValue());
          NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("column");
         // Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

          for (int columnIndex = 0; columnIndex < lstNmElmntLst.getLength(); columnIndex++) {
              Element lstNmElmnt = (Element) lstNmElmntLst.item(columnIndex);
              NodeList lstNm = lstNmElmnt.getChildNodes();
              System.out.println("Last Tag : " + ((Node) lstNm.item(0)).getNodeValue());
              }

        }

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

Upvotes: 0

Views: 1505

Answers (2)

extraneon
extraneon

Reputation: 23970

You now get a NPE on:

<column/>

and you should check your list size before getting element 0:

 NodeList lstNm = lstNmElmnt.getChildNodes();
 if (lstNm.getLength() > 0) {
    System.out.println("Last Tag : " + ((Node)lstNm.item(0)).getNodeValue());
 } else {
     System.out.println("No content");
 }

And as you're processing text content in nodes, have a look at the answer to this SO question. Text nodes are irriting as:

<foo>
   a
   b
   c
 </foo>

can be or are more than one child node of foo, and getTextContent() can ease the pain a bit.

Upvotes: 1

Nicolas Bousquet
Nicolas Bousquet

Reputation: 4000

This code :

NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("column");

Return a List of column nodes, why not just use a for loop to iterate over them all instead of just reading the first one ?

for (int columnIndex = 0; columnIndex < fstNmElmntLst.getLength(); columnIndex++) {
Element fstNmElmnt = (Element) fstNmElmntLst.item(columnIndex);
...
}

Upvotes: 2

Related Questions