Java OO for processing large XML

We are developing a system for processing XML messages.

The processing of a Java class requires the separation of various attributes and values ​​from large XML and passing them as parameters to individual handler classes for various operations.

We thought about the following options:

BUT)

Pass all the XML to each handler and let it extract the appropriate bits - but feel like it might be inefficient to pass XML every time

IN)

Convert XML to DTO or a set of smaller DTOs and pass each DTO to the appropriate handler

FROM)

Cut the XML into fragments and pass them to each handler method

We are not happy with each of them, so any suggestions, where to go?

XML example

<IdAction>supplied</IdAction> <RegId>true</RegId> <DeRegId>false</DeRegId> <SaveMessage>false</SaveMessage> <ServiceName>abcRequest</ServiceName> <timeToPerform>3600</timeToPerform> <timeToReceipt/> <SendToBES>true</SendToBES> <BESQueueName>com.abc.gateway.JMSQueue.forAddRequest</BESQueueName> <BESTransform/> <BESJMSProperties> <property> <propName>stateCode</propName> <propValue>OK</propValue> </property> <property> <propName>stateResponse</propName> <propValue>OK</propValue> </property> </BESJMSProperties> 

It contains 4 blocks processed by 4 handlers that make

  <IdAction>supplied</IdAction> <RegId>true</RegId> <DeRegId>false</DeRegId> 

another does

 <timeToPerform>3600</timeToPerform> <timeToReceipt/> 

next does

 <SendToBES>true</SendToBES> <BESQueueName>com.abc.gateway.JMSQueue.forAddRequest</BESQueueName> <BESTransform/> <BESJMSProperties> <property> <propName>stateCode</propName> <propValue>OK</propValue> </property> <property> <propName>stateResponse</propName> <propValue>OK</propValue> </property> </BESJMSProperties> 

etc.

+8
java oop
source share
4 answers

I don’t think you need any special design considerations in terms of memory usage or performance, so I would go with a solution that included the least amount of coding, and that would use the JAXB marshaller to parse your xml into DTO and then move on to your plan B. It might be harder to set up than StAX, but it saves you from writing any XML parsing.

http://jaxb.java.net/

if you use Spring, it is very easy to configure a bean for org.springframework.oxm.jaxb.Jaxb2Marshaller http://static.springsource.org/spring-ws/site/reference/html/oxm.html (8.5.2)

+3
source share

B sounds like the best option for me. And it’s the most inefficient, and C, apparently, will need one pass to parse it and select fragments, and then the second pass to process them correctly?

Use SAX to analyze minimal DTO sets to pass to dedicated handler classes.

Good question, by the way. It’s good to think about these things in advance and get 2, 3, 4 opinions :-)

+6
source share

Have you tried it? http://simple.sourceforge.net/

Personally, I will create a datamodel for xml and pass the datamodel. Take a look at the tutorials. Using a custom datamodel, you can display only the data that you want in the model, and for handler classes that you can pass on child nodes or a subset of the xml data model, and not across the entire subject area.

If you have xml with the following structure

 <book> <title>XML</title> <author> <firstname>John</firstname> <lastname>Doe</lastname> </author> <isbn>123541356eas</isbn> </book> 

Then you will have a datamodel something like this:

 [ Book ] [ Author ] --------------- ------------------ |String title | |String firstname| |String isbn | |String lastname | |Author author| ------->|----------------| --------------- 

Where the book has a link to the author. And then you can pass the Author object to the handler method.

+3
source share

You can use StAX for this use case. Each processBlock operation will act on an XMLStreamReader, promoting its state, and then subsequent processBlock operations can execute its own bit:

 package forum7011558; import java.io.FileReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; public class Demo { public static void main(String[] args) throws Exception { Demo demo = new Demo(); FileReader xml = new FileReader("src/forum7011558/input.xml"); XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(xml); demo.processBlock1(xsr); demo.processBlock2(xsr); demo.processBlock3(xsr); demo.processBlock4(xsr); } private void processBlock1(XMLStreamReader xsr) { // PROCESS BLOCK 1 } private void processBlock2(XMLStreamReader xsr) { // PROCESS BLOCK 2 } private void processBlock3(XMLStreamReader xsr) { // PROCESS BLOCK 3 } private void processBlock4(XMLStreamReader xsr) { // PROCESS BLOCK 4 } } 
+2
source share

All Articles