Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

XML Parsing with DOMParser

Thread view: 
Yang Xiao - 28 Apr 2004 20:15 GMT
Hi all,
I'm having some problems with parsing XML with DOMParser.
What I want is to filter out particular elements in the XML, the
sample XML looks like this.

Thanks in advance.

Yang

<CATALOG>
           <CD>
                       <TITLE>Empire Burlesque</TITLE>
                       <ARTIST>Bob Dylan</ARTIST>
                       <COUNTRY>USA</COUNTRY>
                       <COMPANY>Columbia</COMPANY>
                       <PRICE>10.90</PRICE>
                       <YEAR>1985</YEAR>
           </CD>
           <CD>
                       <TITLE>Hide your heart</TITLE>
                       <ARTIST>Bonnie Tyler</ARTIST>
                       <COUNTRY>UK</COUNTRY>
                       <COMPANY>CBS Records</COMPANY>
                       <PRICE>9.90</PRICE>
                       <YEAR>1988</YEAR>
           </CD>
           <CD>
                       <TITLE>Greatest Hits</TITLE>
                       <ARTIST>Dolly Parton</ARTIST>
                       <COUNTRY>USA</COUNTRY>
                       <COMPANY>RCA</COMPANY>
                       <PRICE>9.90</PRICE>
                       <YEAR>1982</YEAR>
           </CD>
</CATALOG>

and my code(which doesn't work J)
import java.io.IOException;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


//  A Simple DOM Application
public class BasicDOM2 {
   
   // Constructor
   public BasicDOM2(String xmlFile) {
       
       //  Create a Xerces DOM Parser
       DOMParser parser = new DOMParser();
       
       //  Parse the Document
       //  and traverse the DOM
       try {
           parser.parse(xmlFile);
           Document document = parser.getDocument();
           traverse(document);
       } catch (SAXException e) {
           System.err.println(e);
       } catch (IOException e) {
           System.err.println(e);
       }
   }
   
   //  Traverse DOM Tree.  Print out Element Names
   private void traverse(Node node) {
       
       int type = node.getNodeType();
       if (type == Node.ELEMENT_NODE &&
node.getNodeName().equals("ARTIST")){
           System.out.print(node.getNodeName() + ": ");
           System.out.print(node.getNodeValue());
       } else {
       }
       
      /*
       if (type == Node.TEXT_NODE){// &&
node.getNodeName().equals("ARTIST"))
           System.out.println(node.getNodeValue());
           // System.out.println(node.getLocalName());
       }else{
       }
       */
       
       NodeList children = node.getChildNodes();
       if (children != null) {
           for (int i=0; i< children.getLength(); i++)
               traverse(children.item(i));
       }
       
   }
   
   // Main Method
   public static void main(String[] args) {
       BasicDOM2 basicDOM = new BasicDOM2("c:/temp/cd2.xml");
   }
 
}
Fahd Shariff - 29 Apr 2004 09:48 GMT
I suggest you read up on what DOM documents look like. In your XML
file, for example, the AUTHOR tag is stored as an ELEMENT_NODE. The
value of this tag (Bob Dylan) will be stored as a TEXT_NODE as a child
of the author node.

So during parsing, if you encounter an author element node, you have
to look at its children to get the value of the author.

A quick naive approach is shown below:

A similar approach could be used for other tags.

//////
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.* ;

//  A Simple DOM Application
public class BasicDOM2 {
   
   // Constructor
   public BasicDOM2(String xmlFile)
   {
       DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
    try
       {
        DocumentBuilder parser = factory.newDocumentBuilder();
        Document doc = parser.parse(xmlFile) ;
        traverse(doc) ;
             }
    catch (Exception e) {System.out.println(e) ;}
       
   }
   
   //  Traverse DOM Tree.  Print out Element Names
   private void traverse(Node node) {  
    int type = node.getNodeType();
       if (type == Node.ELEMENT_NODE &&
        node.getNodeName().equals("ARTIST")){
           System.out.print(node.getNodeName() + ": ");
       
       //now get the values which are the children of this node
           NodeList children = node.getChildNodes();
           if (children != null) {
              for (int i=0; i< children.getLength(); i++)
                   System.out.println(children.item(i).getNodeValue()) ;
           }
       } else {
       }
       NodeList children = node.getChildNodes();
       if (children != null) {
           for (int i=0; i< children.getLength(); i++)
               traverse(children.item(i));
       }
       
   }
    // Main Method
   public static void main(String[] args) {
       BasicDOM2 basicDOM = new BasicDOM2("test.xml");
   }  
}
////

Fahd
http://www.fahdshariff.cjb.net

> Hi all,
> I'm having some problems with parsing XML with DOMParser.
[quoted text clipped - 4 lines]
>  
> Yang
Yang Xiao - 29 Apr 2004 18:15 GMT
Hi,
Thanks for the pointer, I was confused about the tree like structure
DOM has from the beginning, I saw the TEXT node as an Attribute rather
than a child node of the element. Thanks for clarifying the point,
thanks.
Yang

> I suggest you read up on what DOM documents look like. In your XML
> file, for example, the AUTHOR tag is stored as an ELEMENT_NODE. The
[quoted text clipped - 72 lines]
> >  
> > Yang


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.