Results 1 to 3 of 3
  1. #1
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default DOM Parser for xml file in java

    I am trying to parse an xml file using dom in java

    Here is the format of the xml file
    Java Code:
    <?xml version="1.0"?>
    <NAME_XML>
    <Created>2008_11_07 11:41::53</Created>
    <datastructure>
    <LabelType name="edge-type" id="3">
          <LabelValueList>
            <LabelValue name="agent" id="22" parent_idref="19" />
          </LabelValueList>
    </LabelType>
    </datastructure>
    </NAME_XML>
    I got the root node name.Here is the code for that

    import java.util.ArrayList;
    import java.util.List;

    Java Code:
    import javax.xml.parsers.*;
    
    import org.omg.CORBA.Any;
    import org.w3c.dom.*;
    public class DOMSAXParsing {
    	public static void main(String args[])
    	{
    		List childlist=new ArrayList();
    	try
    	  {
    		//load and parse the document
    		DocumentBuilder builder;
    		// Create a factory
    		DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
    		// Use the factory to create a builder
    		builder=factory.newDocumentBuilder();
    		Document document=builder.parse("hello.xml");
    
    		//retrieve and display the root node		
    		Element node = document.getDocumentElement();
            String root = node.getNodeName();
            System.out.println("Root Node: " + root);
    	  }
    	catch(Exception e)
    		{
    		}
    	}
    }
    How do i get the child elements such as created and datastructure?
    Java Code:
    Node Child=node.getFirstChild();
            String childname=Child.getNodeName();
            System.out.println(childname);
    trying the above code is not working for me.I get this output

    #text

    I believe that LabelType is the child element of the node datastructure,is it right?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The first child of the root node is a text node that holds the text immediately after the root node which will be a String that has nothing but a line feed in it. To see the complexity of your Document, recurse through it. Try something like so:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    
    public class DomParsing {
      private static final String FILE_NAME_STRING = "hello.xml";
    
      public static void main(String args[]) {
        List childlist = new ArrayList();
        try {
          // load and parse the document
          DocumentBuilder builder;
          // Create a factory
          DocumentBuilderFactory factory = DocumentBuilderFactory
              .newInstance();
          // Use the factory to create a builder
          builder = factory.newDocumentBuilder();
          Document document = builder.parse(FILE_NAME_STRING);
    
          // retrieve and display the root node
          Element node = document.getDocumentElement();
          recurseThroughDoc(node);
    
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
      private static void recurseThroughDoc(Node node) {
        System.out.println("Name: " + node.getNodeName());
        System.out.println("Value: \"" + node.getNodeValue() + "\"");
        
        int type = node.getNodeType();
        if (type == Node.ELEMENT_NODE) {
          System.out.println("Type: Element Node");
        } else if (type == Node.TEXT_NODE) {
          System.out.println("Type: Text Node");
        } else {
          System.out.println("Type: Other, type number: " + type);
        }
        NodeList nodeList = node.getChildNodes();
        System.out.println("child count: " + nodeList.getLength());
        System.out.println();
        if (nodeList.getLength() > 0) {
          for (int i = 0; i < nodeList.getLength(); i++) {
            recurseThroughDoc(nodeList.item(i));
          }
        }
      }
    }
    Note that this code doesn't even look for attributes, but that could easily be added.

  3. #3
    Ms.Ranjan is offline Member
    Join Date
    Jun 2008
    Posts
    85
    Rep Power
    0

    Default

    Thanks a lot.I learnt how to get the attributes too.I have a question from the output i got i understand that the end tag is considered as a text node is that right?

    for eg:
    <created>some data </created>

    the <created> tag is treated as element node but the some data and
    </created> tag is treated as text node.How do i avoid recursing through the end tag bcoz it does not have value or the child count anyway.any suggestion plz?

Similar Threads

  1. Developing HTML Parser in JAVA
    By shinojkk in forum Advanced Java
    Replies: 1
    Last Post: 01-18-2008, 09:07 PM
  2. Java Mozilla Html Parser 0.1.7
    By levent in forum Java Software
    Replies: 0
    Last Post: 07-30-2007, 05:30 PM
  3. Newbie help with a file parser
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 07-24-2007, 05:31 AM
  4. DNS name parser 1.2.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 09:21 PM
  5. MultipartRequest File Upload parser 2.0B9
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-12-2007, 01:24 PM

Posting Permissions

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