Parsing malformed/incomplete/invalid XML files

ziggy picture ziggy · Aug 8, 2011 · Viewed 8.8k times · Source

I have a process that parses an XML file using JDOM and xpath to parse the file as shown below:

private static SAXBuilder   builder         =   null;
private static Document     doc         =   null; 
private static XPath        xpathInstance       =   null;

builder = new SAXBuilder();
Text list = null;

try {
    doc = builder.build(new StringReader(xmldocument));

} catch (JDOMException e) {
            throw new Exception(e);
} 



try {
    xpathInstance = XPath.newInstance("//book[author='Neal Stephenson']/title/text()");
    list = (Text) xpathInstance.selectSingleNode(doc);
} catch (JDOMException e) {
    throw new Exception(e);
}

The above works fine. The xpath expressions are stored in a properties file so these can be changed anytime. Now i have to process some more xml files that come from a legacy system that will only send the xml files in chunks of 4000 bytes. The existing processing reads the 4000 byte chunks and stores them in an Oracle database with each chunk as one row in the database (Making any changes to the legacy system or the processing that stores the chunks as rows in the database is out of the question).

I can build the complete valid XML document by extracting all the rows related to a specific xml document and merging them and then use the existing processing (shown above) to parse the xml document.

The thing is though, the data i need to extract from the XML document will always be on the first 4000 bytes. This chunk ofcourse is not a valid XML document as it will be incomplete but will contain all the data i need. I cant parse just the one chunk as the JDOM builder will reject it.

I am wondering whether i can parse the malformed XML chunk without having to merge all parts (which could get to quite many) in order to get a valid XML document. This will save me several trips to the database to check if a chunk is available and i wont have to merge 100s of chunks only for being able to use the first 4000 bytes.

I know i could probably use java's string functions to extract the relevant data but is this possible using a parser or even xpath? or do they both expect the xml document to be a well formed document before it can parse it?

Answer

Vlad picture Vlad · Aug 8, 2011

You could try to use JSoup to parse the invalid XML. By definition XML should be well-formed, otherwise it's invalid and should not be used.

UPDATE - example:

public static void main(String[] args) {
    for (Node node : Parser.parseFragment("<test><author name=\"Vlad\"><book name=\"SO\"/>" ,
            new Element(Tag.valueOf("p"), ""),
            "")) {
        print(node, 0);
    }
}

public static void print(Node node, int offset) {
    for (int i = 0; i < offset; i++) {
        System.out.print(" ");
    }
    System.out.print(node.nodeName());
    for (Attribute attribute: node.attributes()) {
        System.out.print(", ");
        System.out.print(attribute.getKey() + "=" + attribute.getValue());
    }
    System.out.println();
    for (Node child : node.childNodes()) {
        print(child, offset + 4);
    }
}