Derek,
 
Go to xml.apache.org, this is where the xerces parser project is located. I use this parser. Here is  a bit of code to parse a document...it uses sun's javax extensions for "pluggable" parser, so it will work with any parser. If you download the xerces stuff and put it in the orion directory, it should work. I don't use the xerces jar that comes with orion, its a little old.
 
The SaxParseException is thrown when the xml isn't complient with the dtd, or when it is not well formed xml. Notice that there is a statement:
 

            docBuilderFactory.setValidating(true);
 
... this is the bit that makes the parser a validating parser. You also might think of only using a sax parser (this example is dom), if you only want a few bits of the xml file. This will save a little on memory.
 
Regards,
 
Lawrence
 
import java.util.*;
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.io.IOException;
import java.net.URL;
 
public class Test
{
 
 
    //
    // Reading and writing an XML document stored in a file.
    //
    public static void main (String argv [])
    {
 Document doc;
 
 if (argv.length != 1) {
     System.err.println ("Usage: cmd filename");
     System.exit (1);
 }
 
 try {
 
          
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setValidating(true);
            Test t = new Test();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            URL url = t.getClass().getResource(argv[0]);
     doc = docBuilder.parse (url.toString());
 
 
 

           
  } catch (SAXParseException err) {
     System.out.println ("** Parsing error"
  + ", line " + err.getLineNumber ()
  + ", uri " + err.getSystemId ());
     System.out.println("   " + err.getMessage ());
     // print stack trace as below
        
 } catch (SAXException e) {
     Exception x = e.getException ();
 
     ((x == null) ? e : x).printStackTrace ();
 
 } catch (Throwable t) {
     t.printStackTrace ();
 }
                 
               
 
 System.exit (0);
    }
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Derek Akers
Sent: Monday, September 18, 2000 3:39 PM
To: Orion-Interest
Subject: xml validation

 
    I am building an application that received xml from various sources and does various things with it...  I am currently trying to figure out how to validate an xml string/source against a given dtd (ie - does xml "x" conform to dtd "y")...  does anyone have any suggestions as to how I can go about incorporating this functionality into the receiving bean (ejb)?  I realize that there are several parsers and validators out there, but what I am looking for is some code for using them manually....
 
Derek Akers
 
Internet Application Developer
Eldan Software, Toronto
(416) 341-0070
www.eldan.com

Reply via email to