I have a process that parses an XML file using the 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. Xpath expressions are stored in a properties file so that they can be modified at any time. Now I need to process some more xml files that come from the old system, which will send XML files of only 4000 bytes. Existing processing reads 4,000 byte blocks and stores them in the Oracle database with each fragment as a single row in the database (making changes to an outdated system or processing in which pieces are stored as strings in the database are out of the question),
I can create a complete valid XML document by extracting all the lines associated with a particular XML document and merging them, and then using the existing processing (shown above) to parse the XML document.
The thing is, the data I need to extract from an XML document will always be on the first 4000 bytes. This snippet is not a valid XML document because it will be incomplete, but it will contain all the data I need. I can't make out just one piece, as the JDOM developer will reject it.
I am wondering if I can parse the wrong XML fragment without having to combine all the parts (which could get quite a lot) to get a valid XML document. This will save me a few trips to the database to check if a chunk is available, and I donβt have to merge 100 chunks just to use the first 4000 bytes.
I know that maybe I could use java string functions to retrieve the relevant data, but is this possible with a parser or even xpath? or do they both expect the XML document to be a well-formed document before it can parse it?
ziggy source share