?
Solved

Xerces Parser Help needed URGENT

Posted on 2004-11-06
10
Medium Priority
?
279 Views
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 :
<RootNode>
 <List name = "something">
     <Item>item a</Item>
     <Description>Something</Description>
     <Prices>
        <Price>10</Price>
        <Price>11</Price>
     </Prices>
 </List>
</RootNode>

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 java.sun.com especially the rome library.

How do I create feed that can be displayed as :

<List name = "something">
     <Item>item a</Item>
     <Description>Something</Description>
     <Price>something</Price>
</List>


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)




0
Comment
Question by:lordiano
7 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 12512540
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++) {
      //process
}

Step-by-step on writing feeds

http://wiki.java.net/bin/view/Javawsxml/Rome04TutorialFeedWriter

0
 
LVL 20

Expert Comment

by:Venabili
ID: 12512593
lordiano,

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

Thanks
Venabili
Java Page Editor
0
 
LVL 14

Assisted Solution

by:sudhakar_koundinya
sudhakar_koundinya earned 500 total points
ID: 12512642
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.io.*;




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)
      {

      node=name;
      if(node.equals("List"))
            {
                  list=new AList();
                  list.name = atts.getName(0);

            }

  }

  /**
   * handler method, this is invoked in ending of every element while reading the XML document
   */
    public void endElement(String name)
    {
              if(node.equals("List"))
            {
                        lists.add(list);
            }
    } //

  /**
   * 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);

            if(node.equals("Item"))
         {
                  list.item=str;
         }
         if(node.equals("Description"))
         {
               list.desc=str;
         }
                  if(node.equals("Price"))
         {
                    list.prices.add(str);
         }

  }

 

  public static void main(String s[]) throws Exception
      {
                                 long startTime=System.currentTimeMillis() ;
             
        SAXParser parser = new SAXParser();
        SAXParserXercesTest1 handler = new SAXParserXercesTest1();
              parser.setDocumentHandler(handler);
        parser.parse("c:/a111.xml");
       
            


             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;
};
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 21

Assisted Solution

by:MogalManic
MogalManic earned 500 total points
ID: 12513156
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
   list.setItem(parElement.getElementsByTagName("Item").item(0).getFirstChild().getNodeValue());
   list.setDescription(parElement.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
      ...extract the price node just like you did list-name


}


Also check out this thread:
http://www.experts-exchange.com/Programming/Q_21194046.html
0
 

Author Comment

by:lordiano
ID: 12513401
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");
entry.setLink("http://wiki.java.net/bin/view/Javawsxml/Rome01");
description = new SyndContentImpl();
description.setType("application/xml");
description.setValue("Initial release of Rome");
entry.setDescription(description);
entries.add(entry);
...

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

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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12514039
Check the dtd or schema for the xml used - that will tell you what you can and can't use
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 12515783
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 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