Results 1 to 2 of 2
  1. #1
    roybean is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default Help reading XML file using DOM Parser


    I'm trying to learn to read XML data to my java program, its the first time i read XML files, i'm using to parse the XML the DOM parser.

    so in a file XML like this :

    Java Code:
    I'm aware this code could be much better, but i'm learning the optimization is after the learning processes to me.

    my code is the following:
    Java Code:
    try {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    Element dE = doc.getDocumentElement();
    System.out.println("Root element " + dE.getNodeName());
    NodeList nodeUserID = dE.getElementsByTagName("Version");
    //int userIdCt = nodeUserID.getLength(); // não necessito de contar só há um de cada tipo
    //System.out.println("userCt " +userCt);
    Element elem_uID = (Element) nodeUserID.item( 0 );
    System.out.println("UserID: " + elem_uID.getFirstChild().getNodeValue());
    NodeList nodeLUser = dE.getElementsByTagName("User");
    Node nodeUser = nodeLUser.item(0);
    Element elmnt_nodeUser = (Element) nodeUser;
    NodeList nodeLoginname = elmnt_nodeUser.getElementsByTagName("HasSupporter");
    Element elem_loginName = (Element) nodeLoginname.item(0);
    NodeList node_getchild = null;
    node_getchild = elem_loginName.getChildNodes();
    System.out.println("Loginname : " + ((Node) node_getchild.item(0)).getNodeValue());
    NodeList nodeLLanguage = elmnt_nodeUser.getElementsByTagName("Language");
    Node nodeLanguage = nodeLLanguage.item(0);
    Element elem_Language = (Element) nodeLanguage;
    NodeList nodeLanguage_ID = elem_Language.getElementsByTagName("Language_ID");
    Element elem_Language_ID = (Element) nodeLanguage_ID.item(0);
    node_getchild = elem_Language_ID.getChildNodes();
    System.out.println("Language_ID : " + ((Node) node_getchild.item(0)).getNodeValue());
    } catch (Exception e) {
    this is producing the following output:

    Root element HattrickData
    Version: 1.9
    HasSupporter: False

    How can i read the language childnodes??

    Thanks for the help
    Last edited by roybean; 06-11-2011 at 05:17 PM.

  2. #2
    KarlNorway is offline Member
    Join Date
    Dec 2011
    Rep Power

    Default Re: Help reading XML file using DOM Parser

    I'm not too good with the DOM (as I'm struggeling a bit my self) but I think the error comes when you try to look at the
    Java Code:

    try to comment out the section and try again.

Similar Threads

  1. 3GP File Parser
    By brainstech in forum Jobs Offered
    Replies: 1
    Last Post: 01-17-2010, 05:17 PM
  2. DOM Parser for xml file in java
    By Ms.Ranjan in forum XML
    Replies: 2
    Last Post: 09-18-2009, 05:21 PM
  3. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  4. Newbie help with a file parser
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 07-24-2007, 05:31 AM
  5. MultipartRequest File Upload parser 2.0B9
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-12-2007, 01:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts