I'm developing an app that uses an XML (I'm using javax.xml.parsers.Document
and friends) as input data.
When document misses something (misses in terms of my application logic) I'd show the user what is the wrong piece of XML, eg:
input.xml
:
<root>
<tag-a>
<tag-b />
</tag-a>
</root>
My application requires that tag-b
cannot be used inside tag-a
so I'd output:
Error: tag-b cannot have a tag-a as parent. Check:
input.xml:
...
3: <tag-b />
...
XSD or DTD is not an option because my problem is more complex than this in the example.
In my code I deal with Node
, Element
, ... , what I need is a magical getSourceXML
method, something like:
Element e = ...;
if (e /* something wrong */) {
System.err.println("Error " + e.getSourceXML());
}
Converting an Element
in XML is not useful because there could be more than one similar element (eg: same tag-name/attributes) and I'd show the user the right one (eg. the one on line 7 instead of the one on line 45).