Suhail Gupta
Suhail Gupta

Reputation: 23256

getting data in the object form when reading the xml file

Following code is written to fetch the data from the xml file. I think it does fetch but in the object form. Why is that ?

package newpackage;

import java.io.File;
import java.util.Iterator;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.util.LinkedList;

public class xmlParser {


private DocumentBuilder db;
private DocumentBuilderFactory dbf;
private Document dom;
private LinkedList list = new LinkedList();

public static void main(String args[]) {
    xmlParser o = new xmlParser();
    o.parseXML();
    o.parseDocument();
    o.print();
}

public void parseXML() { // getting a document builder
    try {
        dbf = DocumentBuilderFactory.newInstance();
        db = dbf.newDocumentBuilder();
        dom = db.parse(new File("Details.xml"));
    } catch(Exception exc) {
        exc.printStackTrace();
    }
}

public void parseDocument() { // get a list of student elements
    Element docEle = dom.getDocumentElement();
    NodeList nl = docEle.getElementsByTagName("Details");
    if(nl != null && nl.getLength() > 0) {
        for(int i=0;i<nl.getLength();i++) {
            Element el = (Element)nl.item(i);
           Details details = new Details();
           details = details.getDetail(el);
           list.add(details);
        }
    }
}

public void print() {
 //        list.iterator();
 //        while(!list.isEmpty()) {
 //            System.out.println(list.pop().toString());
 //        }

      Iterator it = list.iterator();
      while(it.hasNext()) {
          System.out.println(it.next().toString());
      }

  }    
}

Output :

newpackage.Details@1ac3c08
newpackage.Details@9971ad
newpackage.Details@1f630dc
newpackage.Details@1c5c1

Why do i get the output in the object form even after applying toString ?

Upvotes: 0

Views: 48

Answers (2)

Polygnome
Polygnome

Reputation: 7795

When you want readable output, you have to override the toString() method in your newpackage.Details class ;)

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240908

Why do i get the output in the object form even after applying toString ?

Because you didn't override toString() method for that Details class

Upvotes: 0

Related Questions