I need to parse an xml file called students, every student has rollno, firstname, lastname as attributes, but not all student has a club. The xml file looks like this:
<?xml version="1.0"?> <class> <student rollno="393" firstname="Dinkar" lastname="Kad"> <club name="Asian-Caucus" /> </student> <student rollno="493" firstname="Vaneet" lastname="Gupta"/> <student rollno="593" firstname="jasvir" lastname="jazz"> <club name="Students-for-Corporate-Citizenship"/> </student> <student rollno="693" firstname="Joseph" lastname="Patterson"/> </class> I would like to retrieve information about each student, get their rollno, first name, lastname and club name if exist. My code can get information about required attributes, but club name is always empty(even when there should be a club associated with this student): My code is like following:
import java.io.File; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class TestParser { public void parseXML() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = factory.newDocumentBuilder(); File file = new File("c:\\My Documents\\students.xml"); Document doc = docBuilder.parse(file); NodeList list = doc.getElementsByTagName("student"); for(int i=0; i<list.getLength(); i++){ int rollno = 0; String firstname = ""; String lastname = ""; String clubname = ""; Element cur = (Element)list.item(i); NamedNodeMap curAttr = cur.getAttributes(); for(int j=0; j<curAttr.getLength(); j++){ Node attr = curAttr.item(j); if(attr.getNodeName().equals("rollno")) rollno = Integer.parseInt(attr.getNodeValue()); if(attr.getNodeName().equals("firstname")) firstname = attr.getNodeValue(); if(attr.getNodeName().equals("club name")) clubname = attr.getNodeValue(); } // end for each attribute System.out.print("rollno: " + rollno); System.out.print(" firstname: " + firstname); System.out.print(" lastname: " + lastname); System.out.println(" club name: " + clubname); }// end for each element } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub TestParser tp = new TestParser(); tp.parseXML(); } } Any idea to fix this problem, so that student has a club can print out the correct club name? I'm very new to xml parsing, any suggestion is appreciated. Thanks a lot!
No comments:
Post a Comment