A newbie question using XML Parser
Hi there,
I am very new to XML parsing using Java, here is a simple question.
I have the following segment of codes that call a XmlParser to parse a XML input:
//...
import org.xml.sax.*;
import org.xml.sax.helpers.*;
//...
RequestXmlParser parser =new RequestXmlParser();
// Create a SAX 2 parser
XMLReader xr =XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
// Set the ContentHandler...
xr.setContentHandler(parser);
xr.parse( requestXmlDoc );
and below is the code for my XmlParser:
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.util.*;
import java.text.ParseException;
publicclass RequestXmlParserextends DefaultHandler{
private Request request =new Request();
// Buffer for collecting data from
// the "characters" SAX event.
private CharArrayWriter contents =new CharArrayWriter();
publicvoid startElement(String namespaceURI, String localName, String qName, Attributes attr)throws SAXException{
System.out.println("startElement");
contents.reset();
}
publicvoid endElement(String namespaceURI, String localName, String qName)throws SAXException{
System.out.println("endElement");
try{
if ( localName.equals("reqid") ){
request.setRequestID(contents.toString());
}
if ( localName.equals("reqsts") ){
request.setStatus(contents.toString());
}
}catch (Exception p){
System.out.println("Here" + p.toString());
}
}
publicvoid characters(char[] ch,int start,int length)
throws SAXException{
System.out.println("characters");
contents.write(ch, start, length);
}
public Request getRequest(){
System.out.println("Return getRequest");
return request;
}
}
My problem is the parser said it cannot find the XML document, as when I printed out the XML document using:
System.out.println(requestXmlDoc);
I received:
<? xml version="1.0" ?><REQUEST><REQID>12345</REQID><REQSTS>P</REQSTS></REQUEST>
Whats wrong? Please help.
Thanks
Neo Gigs

