473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

parsing XML with DOMParser

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");
}
}
Jul 20 '05 #1
0 3054

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
2
by: DKM | last post by:
I recently found out that the below code works in FireFox 1.0.4. str = "<b>Hello world!!!</b>"; var doc = new DOMParser().parseFromString(str, "text/xml");...
1
by: Carlitos | last post by:
Hi there, I'm a beginner on XML. At this moment I am working on a conversion to C# of a (pretty large) program originally coded in Java. It's not much what the JLCA does when converting XML...
0
by: june | last post by:
Hi, I have a big problem with parsing HTML into a XHTML using Cberneko to validate the html. First I tried to work with a HTML-File. This solutions works fine: String aHTMLFile =...
0
by: Divya Prakash | last post by:
Hi But I am unable to parse all the nodes of the tree .....especially the subtree of the main tree It displays only the sibling not the subtree Regards Divya
6
by: jackwootton | last post by:
Hello everyone, I understand that XML can be parsed using JavaScript using the XML Document object. However, it is possible to parse XHTML using JavaScript? I currently listen for DOMMutation...
5
by: vunet | last post by:
I used DOMParser to convert a response string to XML object: DOMParser.parseFromString() In browsers where DOMParser is not supported (Safari 1-2 for my case) I cannot use DOMParser. What...
3
gagandeepgupta16
by: gagandeepgupta16 | last post by:
I am having problem in parsing a string containing HTML Tags. The situation is somewhat similar to the following as quoted in some other forum : if (typeof DOMParser == "undefined") { ...
1
by: Sidhartha | last post by:
Hi, I am facing a problem while parsing local language characters using sax parser. We use DOM to parse and SAX to read the source. But when our application parses strings with local language...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.