0

I have the following XML-File

<books> 
   <book>
      <author>Fitzek</author>
      <titel>Abgeschnitten</titel>
   </book>
   <book>
      <author>Dan Brown</author>
      <titel>Symbol</titel>
    </book>
    <book isbn="123456">
      <author>Sebastian Fitzek</author>
      <titel>Der Augensammler</titel>
    </book>
</books>

I show this XML-File on an JSF Page at the moment. My intention is to validate each element with the correct element in the XSD file an show an information or error message to the page if the specific element isn't valid.
I only found a solution to validate the whole file and not the specific elements.

I hope somebody can help me.
Thanks a lot.

Eddi
  • 11
  • 3
  • [Read this Wikipedia page, it has some good resources.](http://en.wikipedia.org/wiki/XML_validation) – DevlshOne Dec 03 '13 at 12:49
  • Exactly why does the full document validation not fit with your requirements I wonder? – Gimby Dec 03 '13 at 12:58
  • [see here ][1] its already explained. [Or u can use this also][2] [1]: http://stackoverflow.com/questions/10944332/validate-xml-created-using-jaxb-against-an-xsd-file [2]: http://stackoverflow.com/questions/1560422/how-to-validate-against-schema-in-jaxb-2-0-without-marshalling – Sanjeev Dec 03 '13 at 13:01
  • I would like to show which elements aren't validate and show text fields on the page to correct the wrong values. If i check the full document i can say "The file is valid" or "The file isn't valid". But this will not help me – Eddi Dec 03 '13 at 13:05

1 Answers1

0

XML parsers and validators are, to say the least, not robust in the sense you need. In my limited experience, they will fail on the first problem and, if they try to recover, the errors will cascade and multiply.

The issue is that you need to get robust information out of the XML file and robust information out of the validator. Your assumption is that the XML file is well-formed (syntactically correct) and that it is not piece-wise valid (there are elements that fail validation against a schema). Unfortunately, this is not a use-case that most people envision.

Here is a brute force way to handle this. First, create a set of element schemas, one for each data element you want to display and correct. Each schema will consist of only one tag and the required data.

  1. Validate the XML file. If there are no errors, you can simply display the results.
  2. Read in the XML file into a DOM without validation. This gives a tree with data in it.
  3. Walk the DOM looking for elements author and titel (sp). For each element, create a tiny XML file with only the one tag and data element in it. Validate that element against the element schema you created beforehand.

Now, you have a list of data elements with optional validation messages properly associated with each data field.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42
  • Thanks for your solution. I don't know if it's the best solution. My example is very small and easy but the XML file can be very large. I will / must think about that – Eddi Dec 04 '13 at 10:48