java org.xml.sax.SAXParseException: cvc-elt.1: 找不到元素“tns:root_element”的声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7136583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'tns:root_element'
提问by jwookie
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
我在这上面花了 2 个小时。我无法弄清楚为什么会发生此错误。我有一个简单的 xsd 和 xml 代码
xml file:
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<element name="root_element" type="string"/>
</schema>
xsd file:
.xsd 文件:
<?xml version="1.0" encoding="UTF-8"?>
<root_element>"asd"</root_element>
My java code is:
我的Java代码是:
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
任何评论都会有所帮助。问候
回答by Grzegorz Szpetkowski
I think that main issue is with:
我认为主要问题在于:
dbf.setValidating(true);
According to Java API, DocumentBuilderFactory.setValidating
:
据的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 parserby leaving the setValidating(boolean) method false, then use the setSchema(Schema)method to associate a schema to a parser.
指定此代码生成的解析器将在解析文档时验证文档。默认情况下,此值设置为 false。
请注意,此处的“验证”是指 XML 建议中定义的验证解析器。换句话说,它本质上只是控制 DTD 验证。(JAXP 1.2 中定义的遗留两个属性除外。)
要使用现代模式语言(例如 W3C XML Schema 或 RELAX NG 而不是 DTD,您可以通过将 setValidating(boolean) 方法保留为false将解析器配置为非验证解析器,然后使用setSchema(Schema)方法关联模式到解析器。
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):
您的工作代码可能只是(但是我不知道CommodityPropsErrorHandler
课堂上有什么):
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):
这是第二种替代方法dbf.setValidating(true);
(即使用 Java API 中提到的来自 JAXP 的这两个属性):
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));
回答by Mukesh Vispute
This line is for making validation namespace aware. Otherwise it will give Element not present in the doc.
此行用于使验证命名空间感知。否则它会给文档中不存在的元素。
dbf.setNamespaceAware(true);