aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to turn on Xerces validation? Help!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to turn on Xerces validation? Help!!!" Watch "How to turn on Xerces validation? Help!!!" New topic
Author

How to turn on Xerces validation? Help!!!

Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
I am using Xerces DOM parser to parse a xml file. The parser just doesn't validate the file.
I tried to add ErrorHandler as follows:
org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.setErrorHandler( new HandlerBase() {
public void error (SAXParseException e) throws SAXParseException {
throw e; }} );
I tried use schema and dtd, the parser just doesn't seem to do the validation. It knows about non-well-formed xml, but it doesn't validate xml against dtd or schema.
Any help is appreciated.
Anil Vupputuri
Ranch Hand

Joined: Oct 31, 2000
Posts: 527
Originally posted by Caroline Iux:
I am using Xerces DOM parser to parse a xml file. The parser just doesn't validate the file.
I tried to add ErrorHandler as follows:
org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
parser.setErrorHandler( new HandlerBase() {
public void error (SAXParseException e) throws SAXParseException {
throw e; }} );
I tried use schema and dtd, the parser just doesn't seem to do the validation. It knows about non-well-formed xml, but it doesn't validate xml against dtd or schema.
Any help is appreciated.


Try this link
http://www.javaranch.com/ubb/Forum31/HTML/000923.html
Anil

SCJP 1.5, SCEA, ICED (287,484,486)
Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
I checked the article, but didn't find answers. I have a simple example downloaded from ibm web-site which shows how to parse and print xml using DOM.
I didn't include the print method here.
*************************************************************
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.ibm.xml.parsers.*;
/**
* A sample DOM writer. This sample program illustrates how to
* traverse a DOM tree.
*/
public class domOne
{
public void parse(String uri)
{
Document doc = null;
try
{
DOMParser parser = new DOMParser();
parser.parse(uri);
doc = parser.getDocument();
}
catch (Exception e)
{
System.err.println("Sorry, an error occurred: " + e);
}
}
}
**************************************************************
The following is a sample xml file and dtd file downloaded from IBM website with the above example.
**************************
<?xml version="1.0"?>
<!DOCTYPE sonnet SYSTEM "sonnet.dtd">
<sonnet type="Shakespearean">
<author>
<last-name>Shakespeare</last-name>
<first-name>William</first-name>
<nationality>British</nationality>
<year-of-birth>1564</year-of-birth>
<year-of-death>1616</year-of-death>
</author>
<title>Sonnet 130</title>
<lines>
<line>My mistress' eyes are nothing like the sun,</line>
<line>Coral is far more red than her lips red.</line>
<line>If snow be white, why then her breasts are dun,</line>
<line>If hairs be wires, black wires grow on her head.</line>
<line>I have seen roses damasked, red and white,</line>
<line>But no such roses see I in her cheeks.</line>
<line>And in some perfumes is there more delight</line>
<line>Than in the breath that from my mistress reeks.</line>
<line>I love to hear her speak, yet well I know</line>
<line>That music hath a far more pleasing sound.</line>
<line>I grant I never saw a goddess go,</line>
<line>My mistress when she walks, treads on the ground.</line>
<line>And yet, by Heaven, I think my love as rare</line>
<line>As any she belied with false compare.</line>
</lines>
</sonnet>
****************************************************
<!-- sonnet.dtd -->
<!-- sonnet is the root of the document -->
<!ELEMENT sonnet (author,title?,lines)>
<!-- the default sonnet type is "Shakespearean" -->
<!ATTLIST sonnet type (Shakespearean | Petrarchan)
"Shakespearean">
<!-- author contains information about the author -->
<!ELEMENT author (last-name,first-name,nationality,
year-of-birth?,year-of-death?)>
<!-- last-name, first-name, nationality, year-of-birth,
and year-of-death are all elements inside author. -->
<!ELEMENT last-name (#PCDATA)>
<!ELEMENT first-name (#PCDATA)>
<!ELEMENT nationality (#PCDATA)>
<!ELEMENT year-of-birth (#PCDATA)>
<!ELEMENT year-of-death (#PCDATA)>
<!-- The title of the sonnet -->
<!ELEMENT title (#PCDATA)>
<!-- The lines element contains the 14 lines of the
sonnet. -->
<!ELEMENT lines (line,line,line,line,
line,line,line,line,
line,line,line,line,
line,line)>
<!ELEMENT line (#PCDATA)>
****************************************************
According to the dtd, I'd think that with no first name or last name in the xml file, the parser should throw an exception. But it didn't. This is really frustrating. I have been looking for answers.
Any help is greatly appreciated!!!


[This message has been edited by Caroline Iux (edited June 22, 2001).]
Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
Anyone who can help?
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Use

parser.setFeature( "http://xml.org/sax/features/validation",
true );

Be sure to enclose the URL in double quotes.
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Caroline Iux
Ranch Hand

Joined: May 14, 2001
Posts: 103
Thanks, Ajith.
I got the answer from Internet too. In case anyone else has the same question, I posted the code here. The following code should be true for both SAX and DOM parser.
org.apache.xerces.framework.XMLParser parser = new org.apache.xerces.parsers.SAXParser();
parser.setErrorHandler(new org.xml.sax.helpers.DefaultHandler() {public void error (org.xml.sax.SAXParseException e) throws org.xml.sax.SAXParseException {
throw e;}});
parser.setFeature("http://xml.org/sax/features/validation", true);
 
wood burning stoves
 
subject: How to turn on Xerces validation? Help!!!