You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Krishna Vemuri <kr...@electroneconomy.com> on 2001/01/04 03:55:26 UTC

parsing a simple XML file

Hey Guys,

I am using Xerces 1.2.3 with JAXP VER 1.0 & I am using the SAXParser to
parse simple XML files, every single time I am getting the following
exception:

Enter the URI 
/home/kvemuri/test/xml/TestSAXXML.xml
Parsing the XML file from URI : /home/kvemuri/test/xml/TestSAXXML.xml
Parse the given XMLMessage
Document processing started...
The markup in the document preceding the root element must be well-formed.
        at java.lang.Throwable.fillInStackTrace(Native Method)
        at java.lang.Throwable.fillInStackTrace(Compiled Code)
         at java.lang.Throwable.<init>(Compiled Code)
        at java.lang.Exception.<init>(Compiled Code)
        at org.xml.sax.SAXException.<init>(SAXException.java)
        at org.xml.sax.SAXParseException.<init>(SAXParseException.java)
        at
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1056)
        at
org.apache.xerces.framework.XMLDocumentScanner.reportFatalXMLError(XMLDocume
ntScanner.java:626)
        at
org.apache.xerces.framework.XMLDocumentScanner$XMLDeclDispatcher.dispatch(XM
LDocumentScanner.java:805)
        at org.apache.xerces.framework.XMLDocumentScanner.parseSome(Compiled
Code)
        at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:948)
        at
electroneconomy.common.xmlservice.XMLParsingService.parseMessage(XMLParsingS
ervice.java:50)
        at
electroneconomy.common.xmlservice.XMLParsingService.parseXMLMessage(XMLParsi
ngService.java:39)
        at
electroneconomy.common.xmlservice.XMLParsingService.main(XMLParsingService.j
ava:82)


I can open the XML file with any of the browsers or with any XML Editor &
none of them give me the above error, I have tried from the most complex to
the most simple XML's & I am coming back with this exception every time. The
following is the most simplest XML that I tried parsing & the actual  source
code:

<?xml version="1.0" ?>
<PETSTORE STORE="111">
   <PURCHASES CUSTOMERID="222">
      <CREATURE>
          <CREATURETYPE>LLAMA</CREATURETYPE>
          <SPECIES>VICUNA</SPECIES>
      </CREATURE>
      <FEED>
          <DAILYFEED> RUMINANT GRAIN FEED </DAILYFEED>
          <DAILYFEEDQUANTITY>2</DAILYFEEDQUANTITY>
      </FEED>
   </PURCHASES>
</PETSTORE>

									
import org.w3c.dom.*;
import java.io.*;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.apache.xerces.parsers.*;

public class XMLParsingService extends DefaultHandler {
    
    public static String SAX_PARSER = "org.apache.xerces.parsers.SAXParser";
    public static String DOM_PARSER = "org.apache.xerces.parsers.DOMParser";


    private XMLReader reader = null;
    private Document document = null;

    public XMLParsingService() {
    }

    public void parseXMLMessage(String XMLMessage) {
       System.out.println("Parse the given XMLMessage");
       this.parseMessage(XMLMessage);
    }

    public void parseMessage(String XMLMessage) {
       try {
          SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser parser = factory.newSAXParser();
          reader = parser.getXMLReader();
          reader.setContentHandler(this);
          reader.setErrorHandler(this);
          InputSource isource = new InputSource(new
StringReader(XMLMessage));
          reader.parse(isource);
       }  
       catch(SAXException saxe) {
          saxe.printStackTrace();
       }
       catch(ParserConfigurationException pce) {
          pce.printStackTrace();
       }
       catch(IOException ioe) {
          ioe.printStackTrace();
       }
    }

    public void startDocument() throws SAXException {
       System.out.println("Document processing started...");
    }

    public void endDocument() throws SAXException {
       System.out.println("Document processing ended");
    }

    public void error(SAXParseException e){
       System.out.println("ERROR during parsing:"+e.toString());
    }

    public void fatalError(SAXParseException e){
       System.out.println("FATAL ERROR during parsing:"+e.toString());
    }

    public void warning(SAXParseException e){
       System.out.println("WARNING during parsing:"+e.toString());
	
    }


    public static void main(String [] args) throws IOException {
	try {
           XMLParsingService parsingService = new XMLParsingService();
           BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
       
           System.out.println("Enter the URI ");
           String uri = reader.readLine();

           System.out.println("Parsing the XML file from URI : " + uri);

           parsingService.parseXMLMessage(uri);
	}
        catch(Exception exception) {
           exception.printStackTrace();
	}
    }
}


and here's the run script that I am using to actually plug SAXParser in :

java -classpath $TPCLASSPATH:$RUNPATH $1
-Djavax.xml.parsers.SAXParserFactory=org.apache.xerces.jaxp.SAXParserFactory
Impl electroneconomy.common.xmlservice.XMLParsingService

Am I doing something wrong or is there an issue with xerces while used with
JAXP??? If someone can clear this doubt of mine & point me in the right
direction, I would really appreciate that. Thanks so much in advance.

Krishna