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);
}
|
- xml validation Derek Akers
- Re: xml validation Lawrence Fry
- Re: xml validation Ishpal