[ACCEPTED]-Can't find unclosed element in XML-xml

Accepted answer
Score: 10

I use Notepad++ which has an excellent XML Tools 3 plugin that lets you check XML Syntax and 2 takes you to the line that is problematic. It 1 also has useful utilities.

enter image description here

Score: 5

I just opened an XML file in VS 2010 (with 3 ReSharper), broke the XML and what do you 2 know? The error was highlighted immediately. If 1 you have access to the same, it's that simple.

Score: 5

xmllint is a standard tool for this. From the Validation & DTDs page:

The 15 simplest way is to use the xmllint program 14 included with libxml. The --valid option 13 turns-on validation of the files given as 12 input. For example the following validates 11 a copy of the first revision of the XML 10 1.0 specification:

xmllint --valid --noout test/valid/REC-xml-19980210.xml

the -- noout is used to 9 disable output of the resulting tree.

The 8 --dtdvalid dtd allows validation of the 7 document(s) against a given DTD.

Libxml2 6 exports an API to handle DTDs and validation, check 5 the associated description.

If your document 4 isn't "pretty-printed" it can 3 still be hard to find the offending node, so 2 you might want to use xmllint to rewrite 1 the file to be indented.

Score: 0

Since you do not have an XML Schema, there 10 is no fool-proof way of finding the offending 9 code, for example XML allows for recursive 8 structures. But you CAN write your own XML 7 Schema, although that will potentially be 6 a lot of stuff to learn. Alternatively, I 5 would create a simple, stupid, validator 4 of the node level and the element name, as 3 so:

private void parseAndCheckStructure(XMLStreamReader reader) throws XMLStreamException {

    // first read header, this is probably not the offending element (?)
    int event = -1;
    while (reader.hasNext()) {
        event = reader.next();
        if (event == XMLStreamConstants.START_ELEMENT){
            break;
        } else if (event == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException();
        }
    }

    // read the rest of the document.
    int level = 1;
    do {
        event = reader.next();
        if (event == XMLStreamConstants.START_ELEMENT){
            level++;
            String localName = reader.getLocalName();
            if(localName.equals("FirstElement")) {
                parseFirstElementWithALoopLikeTheCurrent(reader);

                level--;
            } else if(localName.equals("SecondElement")) {
                parseSecondElementWithALoopLikeTheCurrent(reader);

                level--;

            } else throw new RuntimeException("Unknown element " + localName + " at level " + level + " and location " + reader.getLocation());

        } else if(event == XMLStreamConstants.END_ELEMENT) {
            // keep track of level
            level--;
        }
    } while(level > 0);

}

Alternatively, parse the whole document 2 within the above do-while loop, and do checks 1 like

if(level == 4 && localName.equals("MyElement")) {
    // ok
} else {
    // throw exception with the location
}

It sucks, but it works.

Score: 0

Try Opening the .xml file with chrome browser, It'll 1 pin point the exact location of the fault.

More Related questions