I am currently working on a graphical XML editor for which it is essential that I can access the individual elements via their ID attribute. I have built up an XML schema in which I define the ID attribute. I use the javax.xml.parsers.DocumentBuilderFactory and assign the schema to it via the corresponding SchemaFactory. The validation of my XML document also works so far, I get error messages if constraints are not followed. However Attr.isId() always returns false.
A hint could be the following error message: Error: URI=null Line=2: Document root element "TABS" must match DOCTYPE root "null". This sounds to me like the parser is trying to validate the XML document using a DTD instead of a schema. I am using Java 1.8.
Parsing the Document and loading the Schema
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);
}
Excerpt from the schema definition
<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>
My .xml File
<?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>
My console output, if Attr.isId() is used:
id | false
name | false
id | false
name | false
id | false
name | false
type | false
And i Fixed the problem: The error lied in the namespace of my .xsd file. Here is the solution:
<?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>
and I also had to fix the namespace of my .xml file
<?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">