0

Currently I am processing a XML data (in MBs) as follows,

  1. Create DOM object for XML record.
  2. Apply XPath queries on DOM object to retrieve fields.

My XML is as follows,

<root>
  <element>
    <sub-element>A</sub-element>
    <sub-element>B</sub-element>
    </sub-element>
    <sub-element>D</sub-element>
    <sub-element>E</sub-element>
  </element>
</root>

Scenario #1: retrieve list of sub-elements of an element is expected to return the following output

sub-element - {'A','B','','D','E'} - 3rd node in XML contains empty/null, which should be created as empty node. But currently I am getting output as {'A','B','D','E'} 3rd value is not created.

Scenario #2: there are some complex queries being used in our application which is tedious in XPATH to retrieve a field from XML DOM object (like parameterized XPATHs)

Is there any way that we could achieve this using DOM itself in efficient and fastest way?

or

Do we need to create a object graph from XML (through XStream) and process the query through normal java way of proccessing the POJO object?

or

Any best way of doing this?

Thanks in advance.

Viswa
  • 1,357
  • 3
  • 18
  • 30

1 Answers1

0

Well, almost any other tree model is going to be better than DOM. You seem to be proposing to drop the XPath part of the solution, but it would be far better to drop the DOM part. For example Saxon's XPath engine is 5-10 times faster when run on a native Saxon tree than when run against a DOM. (DOM is really inefficient for searching, largely because of the clumsy way it handles namespaces).

But your question is pretty confusing. Your XML sample isn't well-formed: I suspect you have written </sub-element> where you meant <sub-element/>. If your XPath is retrieving that empty element and you don't want it, then you just need to correct your XPath.

In Scenario #2, if writing an XPath to express your complex conditions is "tedious", then navigating to find the same data using low-level DOM navigation is going to be infinitely more tedious. But then, you seem to be confused about your objectives. Your title says you want to be "fast and efficient", but now suddenly your aim is to be less "tedious". You need to make your mind up what you are trying to achieve.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164