3

I need to check if a given XML is well-formed or not, like if all tags are properly closed or not and there is no junk at the end of the XML. And I also need it to report all errors in the XML. I need to do this validation in java

For example

<Hello>
    <title>Today is a wonderful day </title>
    <car>
        <type>SUV
    <car>
        <type>van</type>
    </car>
</Hello>
</type>
</car>

So the above xml must report the error that there is junk at the end of file and that car and type tags are not closed properly at the given line number

harmeet
  • 57
  • 1
  • 1
  • 4
  • 2
    You should have a look at the various XML parsers/validators available and attempt to do it yourself. If you come across a problem, come back. – Sinkingpoint Apr 16 '15 at 08:38
  • 2
    @Harmeet, you can take a look at [this SO post](http://stackoverflow.com/questions/6362926/xml-syntax-validation-in-java) – Phoenix Apr 16 '15 at 08:39
  • I have used Sax parser but its reporting all errors at ones. And i am not trying to validate against any schema or DTT. – harmeet Apr 16 '15 at 08:41
  • I was just about to post what @Phoenix just posted :D – Cristian Marian Apr 16 '15 at 08:42
  • 1
    I've voted to close as a duplicate of the link @Phoenix posted. It seems to answer your question. – Sinkingpoint Apr 16 '15 at 08:44
  • I dont think the solution mentioned in the link given by @Phoenix gives all the errors.. – harmeet Apr 16 '15 at 09:39
  • @harmeet then I believe that you think wrong. However if you have a specific example, feel free to prove me wrong by placing it in the text of your question. – Erwin Bolwidt Apr 17 '15 at 02:49
  • try this: https://stackoverflow.com/questions/13083756/how-to-find-unclosed-tags-in-xml-with-java – thiennt Dec 12 '19 at 19:04

2 Answers2

12

Simply read the file and try to convert it to a DOM. If this does not fail you got a valid XML file.

File fXmlFile = new File("/path/to/my.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • this would fail at the occurence of first error.. I wanted a method that would report all the errors at ones.. – harmeet Apr 16 '15 at 09:30
  • 1
    In general this is not possible. Once a parser hits something that is not expected, it can tell you. It could try to go on, but it will likely just find a lot of errors which disapear as soon the first problem is fixed. – BetaRide Apr 16 '15 at 11:53
-2

Basically you want to get all the errors a xml code has.

What you are trying to do is a bit tricky without a schema. Consider this code:

<Hello>
    <title>Today is a wonderful day </title>
    <car>
         <type>SUV
    <car>
         <type>van</type>
    </car></type></car>
</Hello>

This code is a valid xml code but I guess that it's hardly what you want. So, trying to validate the code with parsers without a schema will do you no good.

If you don't want to use a schema, you could use a stack (having in mind the layout)and validate the code by adding/removing to/from the stack the begining and ending tags.

Cristian Marian
  • 750
  • 1
  • 8
  • 18
  • This code is not well formed XML code. Every tag that is opened must also be closed. In html you sometimes see these things but that is not well formed xml then either. – Christian Schneider Oct 12 '21 at 09:49