parsing XML with DOMParser

Discussion in 'XML' started by Yang Xiao, Apr 29, 2004.

  1. Yang Xiao

    Yang Xiao Guest

    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");
    }
    }
    Yang Xiao, Apr 29, 2004
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybGl0b3M=?=

    Java's DOMParser and .NET's XMLDocument

    =?Utf-8?B?Q2FybGl0b3M=?=, Mar 2, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    Joerg Jooss
    Mar 5, 2005
  2. Replies:
    0
    Views:
    366
  3. John Smith
    Replies:
    0
    Views:
    872
    John Smith
    Sep 1, 2004
  4. abwabe
    Replies:
    0
    Views:
    487
    abwabe
    Sep 2, 2004
  5. vunet
    Replies:
    7
    Views:
    245
    Thomas 'PointedEars' Lahn
    Dec 17, 2007
Loading...

Share This Page