473,836 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>Columb ia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonni e Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Greate st 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.IOExcep tion;
import org.apache.xerc es.parsers.DOMP arser;
import org.w3c.dom.Doc ument;
import org.w3c.dom.Nod e;
import org.w3c.dom.Nod eList;
import org.xml.sax.SAX Exception;
// A Simple DOM Application
public class BasicDOM2 {

// Constructor
public BasicDOM2(Strin g xmlFile) {

// Create a Xerces DOM Parser
DOMParser parser = new DOMParser();

// Parse the Document
// and traverse the DOM
try {
parser.parse(xm lFile);
Document document = parser.getDocum ent();
traverse(docume nt);
} catch (SAXException e) {
System.err.prin tln(e);
} catch (IOException e) {
System.err.prin tln(e);
}
}

// Traverse DOM Tree. Print out Element Names
private void traverse(Node node) {

int type = node.getNodeTyp e();
if (type == Node.ELEMENT_NO DE &&
node.getNodeNam e().equals("ART IST")){
System.out.prin t(node.getNodeN ame() + ": ");
System.out.prin t(node.getNodeV alue());
} else {
}

/*
if (type == Node.TEXT_NODE) {// &&
node.getNodeNam e().equals("ART IST"))
System.out.prin tln(node.getNod eValue());
// System.out.prin tln(node.getLoc alName());
}else{
}
*/

NodeList children = node.getChildNo des();
if (children != null) {
for (int i=0; i< children.getLen gth(); i++)
traverse(childr en.item(i));
}

}

// Main Method
public static void main(String[] args) {
BasicDOM2 basicDOM = new BasicDOM2("c:/temp/cd2.xml");
}
}
Jul 20 '05 #1
0 3077

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

Similar topics

2
3964
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 Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
2
3788
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"); document.getElementById("div1").appendChild(doc.documentElement); I knew how to manipulate document object. Now, I see that there is this DOMParser object that one can use in the browser in Javascript. What other objects are accessible in FireFox?
1
2461
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 classes from Java to C# for several reasons, and basically leaves the programmer with the decision on how they will be converted (Reference: http://msdn.microsoft.com/asp.net/using/migrating/jspmig/phase1/xml/) At this point I am about to convert...
0
1999
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 = "file:\\C:/work/Eclipse3.1.1/html-file.html"; org.xml.sax.InputSource pSource = new InputSource(aHTMLFile);
0
1060
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
5945
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 events, when the events occur I access the node which was inserted or removed (event.target). There is only ever about 5 lines of XHTML nested in the node, however it would be silly for me to parse it manually using methods like hasChildNodes...
5
8374
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 method is normally used to convert a string to XML object. For Firefox I use E4X. For IE I use ActiveXObject loadXML() method. Thanks.
3
2497
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") { DOMParser = function () {} DOMParser.prototype.parseFromString = function (str, contentType) { if (typeof ActiveXObject != "undefined") { var d =...
1
3505
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 especially czech,polish,turkish in place of local language character some other word is comming. Eg: Input string :ahoj, jak se máš Output string :ahoj, jak se m&aacute;š
0
10818
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10565
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3094
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.