Parsing

    XML4J is an XML Parsing implementation provided from HTTP://alphaworks.ibm.com.  This parser is a 100% pure Java solution, and is available for both Java1 and Java2 VM’s.   The following example is a simple example of parsing an XML Request Document into a usable DOM object.  Some assumptions:

  • The XML Document has already been read from the connection and as an InputStream (is)
  • The sample is for Java 1.1.x

     Example:

     try{

     // instantiate a parser of type com.ibm.xml.parser.Parser

     Parser parser = new Parser(argv[0]);

     // org.w3c.dom.Document is part of XML4J distribution

     Document doc = parser.readStream(is);

     // Did the document contain any errors?

     if (parser.getNumberOfErrors() > 0) {

            System.out.println(“Errors have been found with the Document!”)

      }

      } catch (Exception e) {

       e.printStackTrace();

      }

 

     The Document object has a group of methods for retrieving and manipulating the data elements of the XML.  Refer to the API documentation from alphaworks for more information on XML4J.  Source is available with this documentation for parsing xml documents.

     Sun Microsystems provides an XML parser labeled JAXP.  This implementation is a validating parser conforming to a 100% pure Java1.2 architecture.  Provided is a small slice of the JAXP API:

    Example:

    import javax.xml.parsers.DocumentBuilderFactory; 
    import javax.xml.parsers.FactoryConfigurationError; 
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.DocumentBuilder;
    import org.xml.sax.SAXException; 
    import org.xml.sax.SAXParseException;
    import java.io.File;
    import java.io.IOException;
    import org.w3c.dom.Document;
    import org.w3c.dom.DOMException;

    /*DOMExceptions are only thrown when traversing or manipulating a DOM. Errors
      that occur during parsing are reporting using a different mechanism that is
      covered below.
    */

    public static void main (String argv [])
    {
        if (argv.length != 1) {
            ...
        }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {

    DocumentBuilder builder = factory.newDocumentBuilder();
    document = builder.parse( new File(argv[0]) );

        } catch (SAXParseException spe) {
           // Error generated by the parser
           System.out.println ("\n** Parsing error" + ", line " + spe.getLineNumber () + ", uri " + spe.getSystemId ());
           System.out.println("   " + spe.getMessage() );
           // Use the contained exception, if any
           Exception  x = spe;
           if (spe.getException() != null)
               x = spe.getException();
           x.printStackTrace();<br>
        } catch (SAXException sxe) {
           // Error generated by this application
           // (or a parser-initialization error)
           Exception  x = sxe;
           if (sxe.getException() != null)
               x = sxe.getException();
           x.printStackTrace();

        } catch (ParserConfigurationException pce) {
           // Parser with specified options can't be built
           pce.printStackTrace();<br>
        } catch (IOException ioe) {
           // I/O error
           ioe.printStackTrace();
        }

    }// main

     For more detailed examples and explanations visit Sun Microsystems java.sun.com.  A distribution of the JAXP and examples of Project X are available from Sun Microsystems.

[Welcome] [Register] [Licensing] [Building] [Request] [Response] [References]