title: Reading contents of the XML using java
body: <p>I'm trying to read an <a href="http://www.cse.lk/listedcompanies/overview.htm?d-16544-e=3&amp;6578706f7274=1" rel="nofollow">XML file</a> 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. </p>
  
  <p>Since the column tags are not unique, I have no idea how to read them. Can someone help me.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code> 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&amp;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 &lt; 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 &lt; 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();
    }
   }
  }
  </code></pre>