org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'tns:root_element'

18,365

Solution 1

I think that main issue is with:

dbf.setValidating(true);

According to Java API, DocumentBuilderFactory.setValidating:

Specifies that the parser produced by this code will validate documents as they are parsed. By default the value of this is set to false.

Note that "the validation" here means a validating parser as defined in the XML recommendation. In other words, it essentially just controls the DTD validation. (except the legacy two properties defined in JAXP 1.2.)

To use modern schema languages such as W3C XML Schema or RELAX NG instead of DTD, you can configure your parser to be a non-validating parser by leaving the setValidating(boolean) method false, then use the setSchema(Schema) method to associate a schema to a parser.

Also you don't need:

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

Your working code probably is just (however I don't know what is in CommodityPropsErrorHandler class):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
SchemaFactory s_factory =
    SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
dbf.setSchema(s_factory.newSchema(new File(schemafile)));  
DocumentBuilder db = dbf.newDocumentBuilder();
CommodityPropsErrorHandler cp_eh = new CommodityPropsErrorHandler();
db.setErrorHandler(cp_eh);
Document doc = db.parse(new File(props_file));

Here is second, alternative approach with previous dbf.setValidating(true); (that is, using this two properties from JAXP, mentioned in Java API):

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);

dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
    XMLConstants.W3C_XML_SCHEMA_NS_URI);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",
    new File(schemafile));

DocumentBuilder db = dbf.newDocumentBuilder();
CommodityPropsErrorHandler cp_eh = new CommodityPropsErrorHandler();
db.setErrorHandler(cp_eh);
Document doc = db.parse(new File(props_file));

Solution 2

This line is for making validation namespace aware. Otherwise it will give Element not present in the doc.

dbf.setNamespaceAware(true);
Share:
18,365
jwookie
Author by

jwookie

Updated on July 16, 2022

Comments

  • jwookie
    jwookie almost 2 years

    I have spent past 2 hours on this. Am unable to figure out why this error is occurring. I have a simple xsd and xml code

    xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <schema xmlns="http://www.w3.org/2001/XMLSchema">
    <element name="root_element" type="string"/>   
    </schema>
    

    xsd file:

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

    My java code is:

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    SchemaFactory s_factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    dbf.setSchema(s_factory.newSchema(new File(schemafile)));  
    dbf.setValidating(true);
    dbf.setFeature("http://apache.org/xml/features/validation/schema", true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    CommodityPropsErrorHandler cp_eh = new CommodityPropsErrorHandler();
    db.setErrorHandler(cp_eh);
    Document doc = db.parse(new File(props_file));
    

    Any comments would be helpful. regards