我当前正在处理图形XML编辑器,对于我必须通过其ID属性访问单个元素至关重要。我已经构建了一个XML模式,其中定义了ID属性。我使用javax.xml.parsers.documentBuilderFactory,并通过相应的schemafactory将架构分配给它。到目前为止,我的XML文档的验证也有效,如果未遵循约束,我会收到错误消息。但是attr.isid((总是返回false。
提示可能是以下错误消息:错误:uri = null line = 2:文档根元素"选项卡"必须匹配Doctype root" null"。对我来说,这听起来像是解析器试图使用DTD而不是模式来验证XML文档。我正在使用Java 1.8。
解析文档并加载模式
public static Document generateDom(IDocument xmlDocument) {
String editorText = xmlDocument.get();
StringReader reader = new StringReader(editorText);
InputSource inputSource = new InputSource(reader);
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
setSchema(documentFactory);
documentFactory.setValidating(true);
Document document = null;
try {
DocumentBuilder builder = documentFactory.newDocumentBuilder();
document = builder.parse(inputSource);
} catch (ParserConfigurationException | SAXException | IOException e) {
// TODO ErrorHandling
e.printStackTrace();
}
return document;
}
private static void setSchema(DocumentBuilderFactory documentFactory) {
URL url = DomLoader.class.getResource("..\..\..\resources\scriptGenerator.xsd");
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
schema = schemaFactory.newSchema(url);
} catch (SAXException e1) {
// TODO ErrorHandling
e1.printStackTrace();
}
documentFactory.setSchema(schema);
}
摘自架构定义
<xs:element name="GROUP">
<xs:complexType>
<xs:sequence>
<xs:sequence>
<xs:element ref="SQL" />
</xs:sequence>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:choice>
<xs:element ref="TEXT" />
<xs:element ref="BUTTON" />
<xs:element ref="ALWAYS" />
</xs:choice>
</xs:sequence>
</xs:sequence>
<xs:attribute name="id" use="required" type="xs:ID" />
<xs:attribute name="name" use="required" type="xs:string" />
</xs:complexType>
</xs:element>
我的.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<TABS>
<TAB id="ID_ea1270bc-2554-4291-b5ad-8c7802b5fc49" name="Tab 1">
<GROUP id="ID_3155171c-d2ac-4717-aa38-009005c79e18" name="Group 1">
<SQL>
<PREPARE>
<CONSTANT/>
</PREPARE>
<MERGE>
<CONSTANT/>
</MERGE>
<SYNCHRONIZE>
<CONSTANT/>
</SYNCHRONIZE>
</SQL>
</GROUP>
</TAB>
</TABS>
使用了我的控制台输出,如果使用attr.isid((:
id | false
name | false
id | false
name | false
id | false
name | false
type | false
好吧,我至少找到了错误的解决方案
Error: URI=null Line=2: Document root element "TABS" must match DOCTYPE root "null"
如果您使用document -factory.setValidating(true(文档构建器实际上期望DTD而不是模式。因此,关闭此功能也会使错误消失。
,我解决了问题:错误的错误在我的.xsd文件的名称空间中。这是解决方案:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
targetNamespace="http://www.w3.org/2001/scriptGenerator"
xmlns="http://www.w3.org/2001/scriptGenerator">
<xs:element name="TABS">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="1" maxOccurs="unbounded" ref="TAB" />
</xs:sequence>
</xs:complexType>
</xs:element>
我还必须修复.xml文件的命名空间
<?xml version="1.0" encoding="UTF-8"?>
<TABS xmlns="http://www.w3.org/2001/scriptGenerator"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2001/scriptGenerator
scriptGenerator.xsd">