I have an XML file where all text must be contained/enclosed within an element i.e. if an element contain a child then it cannot contain text.
Example:
This is allowed:
<?xml version='1.0' encoding='UTF-8'?>
<mainParent source="">
<!-- FOR TESTING ONLY -->
<child1 scheduling=""> <!-- 1ST -->
<child2 domain="" type=""><![CDATA[ALLOWED TEXT]]></child2>
</child1>
<child1 scheduling=""> <!-- 2nd -->
<child2 domain="" type=""><![CDATA[ALLOWED TEXT 2]]></child2>
</child1>
</mainParent>
This is not allowed:
<?xml version='1.0' encoding='UTF-8'?>
<mainParent source="">
<!-- FOR TESTING ONLY -->
<child1 scheduling=""> <!-- 1ST -->
<child2 domain="" type=""><![CDATA[ALLOWED TEXT]]></child2>
NOT ALLOWED TEXT 1
</child1>
<child1 scheduling=""> <!-- 2nd -->
<child2 domain="" type=""><![CDATA[ALLOWED TEXT 2]]></child2>
</child1>
NOT ALLOWED TEXT 2
</mainParent>
Using DOM:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setXIncludeAware(true);
dbFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
doc = docBuilder.parse( new File(fileName) );
The parser only fails if an attributed is repeated or element is not closed. But would like it to also fail if a text entry is 'hanging'.
How can I enforce this?
Thanks.