0

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).

marka.thore
  • 2,795
  • 2
  • 20
  • 35
  • @vanje: I don't think it's an answer for my question because `XMLOutputter` writes the XML corresponding to an `Element`, what if there are more similar elements? I'd like to retrieve the original element, maybe with its code line. – marka.thore Apr 01 '16 at 11:00
  • I think the second answer is what you want. – vanje Apr 01 '16 at 12:14

0 Answers0