You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Orlando Ribeiro <or...@inescporto.pt> on 2001/05/04 13:07:11 UTC

Validation using Xerces DOMParser

Hi,
      
      I am parsing an xml file using the Xerces 1_2_3 DOMParser, I
implement the code like this:
 
      public org.w3c.dom.Document get_xerces_document (String
Xml_FileName)
       {      
DOMParser domParser = new DOMParser();          
       
              try
              {      
domParser.parse( new InputSource( new FileInputStream( new
File(xml_FileName) ) ) );
 
              }catch(IOException ex) {
                     ex.printStackTrace();
              }catch(org.xml.sax.SAXException ex) {
                     ex.printStackTrace();
              }
              
              return domParser.getDocument();
       }
 
 
      I want to perform the document validation using an xsd file
(schema file), as showed below:
 
orlando.xsd:
 
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
       <xsd:element name="orlando" type="Dados"/>
       <xsd:complexType name="Dados">
             <xsd:sequence>
                    <xsd:element name="name" type="xsd:string"/>
                    <xsd:element name=" surname" type="xsd:string"/>
                    <xsd:element name="age" type="xsd:integer"/>
             </xsd:sequence>
       </xsd:complexType>
</xsd:schema>
 
 
myExample.xml:
 
<?xml version="1.0" encoding="UTF-8"?>
<orlando xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\TEMP\orlando.xsd">
       <name>Orlando</name>
       < surname >Ribeiro</ surname >
       <age>24</age>
</orlando>
 
 
Is there some way of doing the document validation using the xsd file in
the parsing process (DOMParser)?
Could anybody tell me what should I do to make this work?
 
 
Thanks in advance,
Orlando Ribeiro
 

Re: Validation using Xerces DOMParser

Posted by René Jensen <lu...@tbkol.dk>.
Citat Orlando Ribeiro <or...@inescporto.pt>:

> Hi,
>       
>       I am parsing an xml file using the Xerces 1_2_3 DOMParser, I
> implement the code like this:
>  
>       public org.w3c.dom.Document get_xerces_document (String
> Xml_FileName)
>        {      
> DOMParser domParser = new DOMParser();          
>        
>               try
>               {      
> domParser.parse( new InputSource( new FileInputStream( new
> File(xml_FileName) ) ) );
>  
>               }catch(IOException ex) {
>                      ex.printStackTrace();
>               }catch(org.xml.sax.SAXException ex) {
>                      ex.printStackTrace();
>               }
>               
>               return domParser.getDocument();
>        }
>  
>  
>       I want to perform the document validation using an xsd file
> (schema file), as showed below:
>  
> orlando.xsd:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
>        <xsd:element name="orlando" type="Dados"/>
>        <xsd:complexType name="Dados">
>              <xsd:sequence>
>                     <xsd:element name="name" type="xsd:string"/>
>                     <xsd:element name=" surname" type="xsd:string"/>
>                     <xsd:element name="age" type="xsd:integer"/>
>              </xsd:sequence>
>        </xsd:complexType>
> </xsd:schema>
>  
>  
> myExample.xml:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <orlando xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
> xsi:noNamespaceSchemaLocation="C:\TEMP\orlando.xsd">
>        <name>Orlando</name>
>        < surname >Ribeiro</ surname >
>        <age>24</age>
> </orlando>
>  
>  
> Is there some way of doing the document validation using the xsd file
> in
> the parsing process (DOMParser)?
> Could anybody tell me what should I do to make this work?
>  
>  
> Thanks in advance,
> Orlando Ribeiro
>  
> 

Yep. You should set the right features for the parser. For validation using a 
schema those would be:

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

and probably also:

DOMparser.setFeature( "http://xml.org/sax/features/namespaces", true );

You can read more about the features on the Xerces site.
http://xml.apache.org/xerces-j/features.html

       _\|/_
       (@ @)
---oOOo-(_)-oOOo---
    René Jensen
 lundeman@tbkol.dk

---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org