Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB - Ignore element

Is there any way to just ignore an element from Jaxb parsing? I have a large XML file, and if I could ignore one of the large, complex elements, then it would probably parse a lot quicker.

It would be even better if it could not even validate the element contents at all and parse the rest of the document even if that element is not correct.

ex:this should only generate Foo.element1 and Foo.element2

<foo>     <element1>I want this</element1>     <element2>And this</element2>     <bar>        <a>ALL of bar should be ignored</a>        <b>this also should be ignored</b>        <c>            <x>a lot of C that take time to process</x>        </c>        <c>             <x>a lot of C that take time to process</x>        </c>        <c>           <x>a lot of C that take time to process</x>        </c>       <c>           <x>a lot of C that take time to process</x>       </c>   </bar> </foo> 
like image 695
questioner Avatar asked Feb 18 '11 16:02

questioner


People also ask

How do I ignore fields in JAXB?

You can use annotation @XmlTransient to ignore fields. Put this annotation on field itself or its getter.

How do you ignore a field in XML response?

Ignore XML Attribute. You can specify ignore="true" or ignore="false". The default value is false. Specifying ignore="false" has no effect on the attribute value assigned when an object of the type specified in the rule is created and no effect on the constraints.

What is @XmlTransient?

The @XmlTransient annotation is useful for resolving name collisions between a JavaBean property name and a field name or preventing the mapping of a field/property. A name collision can occur when the decapitalized JavaBean property name and a field name are the same.

What is JAXB context?

The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.


1 Answers

Assuming your JAXB model looks like this:

@XmlRootElement(name="foo") public class Foo {     @XmlElement(name="element1")    String element1;     @XmlElement(name="element2")    String element2;     @XmlElement(name="bar")    Bar bar; } 

then simply removing the bar field from Foo will skip the <bar/> element in the input document.

Alternatively, annotated the field with @XmlTransient instead of @XmlElement, and it will also be skipped.

like image 94
skaffman Avatar answered Sep 30 '22 14:09

skaffman