For Programmers: Free Programming Magazines  


Home > Archive > Java Help > April 2004 > Re: XML Parsing with DOMParser









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: XML Parsing with DOMParser
Fahd Shariff

2004-04-29, 8:39 am

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

yang2002_99@yahoo.com (Yang Xiao) wrote in message news:<55c6e631.0404281115.264d51da@posting.google.com>...
> 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

Yang Xiao

2004-04-29, 2:24 pm

Hi,
Thanks for the pointer, I was 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

fahdshariff@yahoo.com (Fahd Shariff) wrote in message news:<9bc0209f.0404290048.278fe62d@posting.google.com>...[color=darkred]
> 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
>
> yang2002_99@yahoo.com (Yang Xiao) wrote in message news:<55c6e631.0404281115.264d51da@posting.google.com>...
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com