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

[4249 byte] By [yyvoo] at [2007-9-27 22:44:20]
# 1
tell me one thing: what is the data type of requestXmlDoc?if it is a String, then a little hint: check the XMLReader API...
ddossot at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Yes, you are right, it is a String, but whats wrong? I went to the API but I dunno wats wrong? Can you please tell me straight?
yyvoo at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

XMLReader has two parse() methods. Either you provide an InputSource or a SystemId. Both, however, are only kind of references to the actual XML, SystemId being like a URL and InputSource being constructed from an InputStream, a Reader, or a SystemId again. You can't simply put some XML into a String and pass that to the XMLReader.

For testing, here's a quick hack that should work:

xr.parse(new InputSource(new StringReader(requestXmlDoc)));

jheckler at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
it seems that the importance of taking a quick glance to the API have never been stressed enough... thanks for your patience jheckler!
ddossot at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5
Thanks alot guys! That is the fault of the book's code while I am still learning on these XML things.
yyvoo at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6
Wohoo, my first Duke Dollar! :-)David: It was hard enough to be faster than you this once ;) Anyway I find it much easier to be patient when people ask sophisticated questions instead of those "I want to write a three-tier J2EE app. Just downloaded J2SE SDK. What next? Plz help"
jheckler at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
you are definitely right - the thing is, for this kind of question, it is good to try to create the habit of looking at the API... it can save "newbies" a lot of problems.in the other hand, it's easy dukes - and i won't despise it ;-)david
ddossot at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
Hm, true. Didn't think of pedagogical aspects... I'll try to keep that in mind in future. And hey, it's not like I'm after those Duke Dollars (with my limited expertise, I wouldn't get too far anyway), I just saw that golden thing there next to my reply and couldn't help cheering :)
jheckler at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9
caution, you quickly get addicted with these gold stuffs!
ddossot at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10
Looking at your record, I'd think so. Still, you won't see me interfere e. g. in that "xml pack" thread. That mess, Dukes or not, is all yours ;-D
jheckler at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11
er, you noticed this one uh?my onw personal viet-nam...
ddossot at 2007-7-7 13:42:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...