You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by Alain De <al...@yahoo.com> on 2001/01/12 18:21:05 UTC

DOM and XML validation

Hi there,

I am pretty new in the XML universe and this is
probably an old one ...

Using Xerces 1.2.3, I would like to use a validating
DOM Parser to 
(in)validate
a sample pizza.xml file. The problem is that the
parser doesn't 
complain about the
fact that the xml is not DTD compliant. Hereafter the
Java file and the 
xml file.
I would expect the parser to tell me that there is a
problem in the 
attribute
extracheese of the xml file.

Thanks in advance for any hint.

Alain

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE pizza
[
<!ELEMENT pizza (topping,price,size)>
<!ELEMENT topping (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT size (#PCDATA)>
<!ATTLIST topping extracheese (yes|no) "no">
]>

<pizza>
<topping extracheese="yesa"> Pepperoni </topping>
<price> 10 </price>
<size> large </size>
</pizza>


...
public ParseTest() {
 try {
 
domParser.setFeature("http://xml.org/sax/features/validation",
true);
 } catch (Exception e) {
  e.printStackTrace();
 }
 InputSource source = null;
 source = new InputSource("/pizza.xml");
 try {
  domParser.parse(source);
 } catch (Exception e) {
  e.printStackTrace();
 }
 domParser.setErrorHandler(this);

 Document document=domParser.getDocument();
 Element 
element=(Element)document.getElementsByTagName("price").item(0);
 String price=element.getFirstChild().getNodeValue();
 System.out.println(price);
}
/**
 * Insert the method's description here.
 * Creation date: (1/11/01 9:35:53 PM)
 */
public void error(SAXParseException ex) {
  System.out.println("[Error] "+
         //getLocationString(ex)+": "+
         ex.getMessage());
 }
/**
 * Insert the method's description here.
 * Creation date: (1/11/01 9:35:53 PM)
 */
public void fatalError(SAXParseException ex) throws
SAXException {
  System.out.println("[Fatal Error] "+
         //getLocationString(ex)+": "+
         ex.getMessage());
  throw ex;
 }
/**
 * Insert the method's description here.
 * Creation date: (1/11/01 9:15:41 PM)
 * @param args java.lang.String[]
 */
public static void main(String[] args) {
 ParseTest test=new ParseTest();
}
/**
 * Insert the method's description here.
 * Creation date: (1/11/01 9:35:53 PM)
 */
public void warning(SAXParseException ex) {
  System.out.println("[Warning] "+
         //getLocationString(ex)+": "+
         ex.getMessage());
 }
}

__________________________________________________
Do You Yahoo!?
Yahoo! Photos - Share your holiday photos online!
http://photos.yahoo.com/

Re: DOM and XML validation

Posted by Khaled Noaman <kn...@ca.ibm.com>.
By looking at the code, I can see that you are setting the error handler
after performing the parse action. You need to set the error handler
before parsing the document.

Also, since you are using the Java version of the parser, you need to
post your messages to the xercesj dev list.

Regards,
Khaled Noaman
XML Parsers, IBM Toronto Lab
knoaman@ca.ibm.com

Alain De wrote:

> Hi there,
>
> I am pretty new in the XML universe and this is
> probably an old one ...
>
> Using Xerces 1.2.3, I would like to use a validating
> DOM Parser to
> (in)validate
> a sample pizza.xml file. The problem is that the
> parser doesn't
> complain about the
> fact that the xml is not DTD compliant. Hereafter the
> Java file and the
> xml file.
> I would expect the parser to tell me that there is a
> problem in the
> attribute
> extracheese of the xml file.
>
> Thanks in advance for any hint.
>
> Alain
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <!DOCTYPE pizza
> [
> <!ELEMENT pizza (topping,price,size)>
> <!ELEMENT topping (#PCDATA)>
> <!ELEMENT price (#PCDATA)>
> <!ELEMENT size (#PCDATA)>
> <!ATTLIST topping extracheese (yes|no) "no">
> ]>
>
> <pizza>
> <topping extracheese="yesa"> Pepperoni </topping>
> <price> 10 </price>
> <size> large </size>
> </pizza>
>
> ...
> public ParseTest() {
>  try {
>
> domParser.setFeature("http://xml.org/sax/features/validation",
> true);
>  } catch (Exception e) {
>   e.printStackTrace();
>  }
>  InputSource source = null;
>  source = new InputSource("/pizza.xml");
>  try {
>   domParser.parse(source);
>  } catch (Exception e) {
>   e.printStackTrace();
>  }
>  domParser.setErrorHandler(this);
>
>  Document document=domParser.getDocument();
>  Element
> element=(Element)document.getElementsByTagName("price").item(0);
>  String price=element.getFirstChild().getNodeValue();
>  System.out.println(price);
> }
> /**
>  * Insert the method's description here.
>  * Creation date: (1/11/01 9:35:53 PM)
>  */
> public void error(SAXParseException ex) {
>   System.out.println("[Error] "+
>          //getLocationString(ex)+": "+
>          ex.getMessage());
>  }
> /**
>  * Insert the method's description here.
>  * Creation date: (1/11/01 9:35:53 PM)
>  */
> public void fatalError(SAXParseException ex) throws
> SAXException {
>   System.out.println("[Fatal Error] "+
>          //getLocationString(ex)+": "+
>          ex.getMessage());
>   throw ex;
>  }
> /**
>  * Insert the method's description here.
>  * Creation date: (1/11/01 9:15:41 PM)
>  * @param args java.lang.String[]
>  */
> public static void main(String[] args) {
>  ParseTest test=new ParseTest();
> }
> /**
>  * Insert the method's description here.
>  * Creation date: (1/11/01 9:35:53 PM)
>  */
> public void warning(SAXParseException ex) {
>   System.out.println("[Warning] "+
>          //getLocationString(ex)+": "+
>          ex.getMessage());
>  }
> }
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Photos - Share your holiday photos online!
> http://photos.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-cvs-help@xml.apache.org

Re: DOM and XML validation

Posted by Ian Roberts <ir...@decisionsoft.com>.
On Fri, 12 Jan 2001, Alain De wrote:

> public ParseTest() {
>  try {
>  
> domParser.setFeature("http://xml.org/sax/features/validation",
> true);
>  } catch (Exception e) {
>   e.printStackTrace();
>  }
>  InputSource source = null;
>  source = new InputSource("/pizza.xml");
>  try {
>   domParser.parse(source);
>  } catch (Exception e) {
>   e.printStackTrace();
>  }
>  domParser.setErrorHandler(this);

You have to call setErrorHandler *before* you call parse.

Ian

-- 
Ian Roberts                     | irr@decisionsoft.com
DecisionSoft Ltd.               | http://www.decisionsoft.com/