Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > parsing XML with DOMParser

Reply
Thread Tools

parsing XML with DOMParser

 
 
Yang Xiao
Guest
Posts: n/a
 
      04-29-2004
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");
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert String to XML Object Where DOMParser Is Not Supported vunet Javascript 7 12-17-2007 11:33 PM
Java's DOMParser and .NET's XMLDocument =?Utf-8?B?Q2FybGl0b3M=?= ASP .Net 1 03-05-2005 10:52 AM
Unwanted HTML tags inserted when using DOMParser (Java) badgerduke@gmail.com Java 0 01-14-2005 10:56 PM
java.lang.VerifyError : DOMParser & WSAD abwabe XML 0 09-02-2004 10:35 AM
Xerces DOMParser and CDATA sections John Smith XML 0 09-01-2004 04:39 PM



Advertisments