Xerces Parser Help needed URGENT

Posted on 2004-11-06
Last Modified: 2013-11-23
Hi, I am trying to write a parser that can parse a XML file, and then use RSS to make a feed of my own XML schemas.
I need help in the Xerces XML parser part. Does anyone have sample code for using Xerces XML parser in parsing a XML file, by tagnames ..etc , or maybe tell me how I can parse XML file such as :
 <List name = "something">
     <Item>item a</Item>

Where i will have multiple List,
I want to parse the List-Name, and the Item, description, prices(and all the individual Price)

The xml file can be located on disk or on web.

I read up on the RSS feed creation on especially the rome library.

How do I create feed that can be displayed as :

<List name = "something">
     <Item>item a</Item>

Thanks for your help,
if you have sourcecode and would rather like to email please email to me at
<e-mail removed from Venabili>
(If you do email please let me know you emailed)

Question by:lordiano
    LVL 86

    Accepted Solution

    You could use XPath to get your List nodes:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new File(args[0]));
    XPath xpath = XPathFactory.newInstance().newXPath();
    final String findList = "//List";
    NodeList nodeList = (NodeList) xpath.evaluate(findList, document, XPathConstants.NODESET);
    for(int i = 0;i < nodeList.getLength();i++) {

    Step-by-step on writing feeds

    LVL 20

    Expert Comment


    Please do not post your e-mail in questions -- all the communication should be lead on the site. I removed it from here.

    Java Page Editor
    LVL 14

    Assisted Solution

    Although I haven't tested

    this might help you

    import org.xml.sax.AttributeList;
    import org.xml.sax.HandlerBase;
    import org.xml.sax.Parser;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;
    import org.xml.sax.helpers.ParserFactory;
    import org.xml.sax.XMLReader;
    import org.apache.xerces.parsers.SAXParser;

    import java.util.*;

    public class SAXParserXercesTest1
         extends HandlerBase { //implements org.xml.sax.ContentHandler

       * handler method, this is invoked in starting of every element while reading the XML document

                 public void startElement(String name, AttributeList attrs)

                      list=new AList();
             = atts.getName(0);



       * handler method, this is invoked in ending of every element while reading the XML document
        public void endElement(String name)
        } //

       * handler method, this is invoked in after startElement while reading the XML document
       public void characters(char ch[], int start, int len){

        String str = new String(ch, start, len);




      public static void main(String s[]) throws Exception
                                     long startTime=System.currentTimeMillis() ;
            SAXParser parser = new SAXParser();
            SAXParserXercesTest1 handler = new SAXParserXercesTest1();

                 long endTime=System.currentTimeMillis() ;
                 System.out.println("Xerces Sax Parser  : Time taken to parse the document for "+s[0]+" times is :"+((endTime-startTime)/1000));


          private Vector getITemLists()
                return lists

    String node;
    AList list=null;
    Vector lists=new Vector();

    class AList
          private String name;
          private String item;
          private String desc;
          private Vector prices;
    LVL 21

    Assisted Solution

    I would recommend using JAXP (Part of JDK 1.4 and downloadable from SUN).  The code will be something like this:

    DocumentBuilder build=DocumentBuilderFactory.newDocumentBuilder();
    Document doc=build.parse(xmlFileName);
    NodeList listNodes=doc.getElementsByTagName("List-Name ");
    for(i=0;i<listNodes.getLength();i++) {
       Element parElement=(Element) listNodes.item(i);
       AList list=new AList();

       //Get the  Item, description, prices nodes
          ...extract the price node just like you did list-name


    Also check out this thread:

    Author Comment

    When I create Feed using ROME library, How can i create it so that it will have custome tag names?
    I am creating it like :

    entry = new SyndEntryImpl();
    entry.setTitle("Rome v0.1");
    description = new SyndContentImpl();
    description.setValue("Initial release of Rome");

    but this would be displayed as
        <title> .... </title>
        <link>.... </link>
        <summary type = ............../>

    Is there a way I can rename the tag "entry" to something else?
    and is there a way I can create custom tag other than title, link, entry..???

    I will split points accordingly THANKS a lot for the help
    LVL 86

    Expert Comment

    Check the dtd or schema for the xml used - that will tell you what you can and can't use
    LVL 92

    Assisted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Title # Comments Views Activity
    sameEnds challenge 25 55
    java constructor error 8 63
    hasOne  challenge 59 63
    Java Message handling in Service Layer 3 20
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now