File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes JAXB or Xstream Filter certain Child Elements on Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXB or Xstream Filter certain Child Elements on Type" Watch "JAXB or Xstream Filter certain Child Elements on Type" New topic
Author

JAXB or Xstream Filter certain Child Elements on Type

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hi,
Hope everyone is well, quick question to see if anyone has any feedback.

I was experimenting with both JaxB and Xstream over last two days. I was basically using the XML libraries to marshal and unmarshal XML to / from Java objects. Now this was a very simple task which I got working very quickly. However, the XML I want to unmarshal into a list of Java objects is very long and contains many child elements that could be ignored and not put into the list of java objects.
For example the xml would look similar to:

So I would like the library to iterate through all the xml elements and unmarshal into a list of java objects which so far works, however as it iterates I would like to add additional functionality to check the Type and Flag element values, if TYPE value equals Test2 and or if Flag value equals False to ignore this child element all together and not include it in the finished list of Java objects. Does anyone know if this is possible with either JaxB or Xstream? Alternatively, can anyone suggest maybe a better approach to accomplish this which requires minimum code and manual parsing.

I have been looking at ValidationEventHandler and XmlAdapter in JaxB but I do not think these will allow me do what I want. I got close with the Xmldapter however the unmarshal has to return either null or an object for each xml child element it processes, it also changed the xml syntax to attribute form i.e TYPE = "Test1" etc which I did not see any way of altering.

Xstream allows you to implement a Converter which has a canConvert method, however this only works on Class type, and not child element type which I weant to check for each child element. Had a look at MapperWrapper wrapMapper method which can be overloaded in Xstream, but it only shows element attribute name, i.e FLAG and does not show value, also if it did show value I do not see anyway of telling the function to ignore child root element and all attributes for said child.

Anyway, that's my two cent. Any advice?

Best,
Mark
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 512
    
    3
You could make out an xslt document. Its design is to take out (ie eliminate) those Table1 elements in the xml document which have either TYPE "Test2" or FLAG false (uniformized in format rather than False or fAlse etc...). The output of the xslt transform() method will be channelled to an InputStream via the StreamResult of the transform() method. And then the InputStream will be fed into unmarshaller's unmarshal() method. That way, there is not much of interruption of the code already in place.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB or Xstream Filter certain Child Elements on Type