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

夙愿已清 提交于 2019-12-22 09:11:46

问题


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


回答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));



回答2:


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

dbf.setNamespaceAware(true);


来源:https://stackoverflow.com/questions/7136583/org-xml-sax-saxparseexception-cvc-elt-1-cannot-find-the-declaration-of-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!