JAXB - Ignore element

72,024

Solution 1

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.

Solution 2

JAXB will ignore any unmapped properties.

Implementation wise (atleast in EcliseLink JAXB (MOXy), which I lead). When we are processing the contents via a SAX parser (i.e. the input was a SAXSource) then we swap out our ContentHandler that is responsible for building objects to one that does no processing for that section (org.eclipse.persistence.oxm.unmapped.UnmappedContentHandler). When a we are using processing the contents via a StAX parser we just advance to the next mapped event.

If you do have a property that corresponds to that node you can annotate it with @XmlTransient to make it an unmapped property.

Solution 3

All what you need it's mark field as @XmlTransient (@XmlTransient annotation which should hide fields that are not required). Example below

JavaEE:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "DeletedIds")
public class DeletedIds {

    @XmlElement(name = "DeletedId")
    private List<DeletedId> id;    

    @XmlTransient
    @XmlElement(name = "success")
    private String success;

    //getters&setters
}

@XmlAccessorType(XmlAccessType.FIELD)
public class DeletedId {

    private int id;

    //getters&setters
}

Xml:

<DeletedIds>
    <DeletedId>
        <id>1</id>
    </DeletedId>
    <DeletedId>
        <id>2</id>
    </DeletedId>
</DeletedIds>

Solution 4

You have to use a SAX parser, and a document handler that effectively "skips" the node of non-interest. You can't get around reading the bytes, but at least you can get around having them waste extra resources.

If your code requires a DOM tree, then you basically use a SAX document handler that generates DOM nodes, but "skips" the nodes of non-interest. Definitely less convenient that using the supplied DOM tree generators, but a decent trade-off is you can't live with the extra memory overhead of the unwanted nodes but you need the DOM tree.

Share:
72,024
questioner
Author by

questioner

Updated on July 17, 2022

Comments

  • questioner
    questioner almost 2 years

    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>
    
  • jghost
    jghost over 11 years
    I know it's been over a year... but that is very much not true for standard JAXB... it complains and throws errors about not finding the missing field. It could be the latest version did this or some older version... or you're using another API that expanded on the core set. If I'm wrong, let me know but I just tried this and got errored out.
  • jghost
    jghost over 11 years
    here's the specific error: Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions Property [property_name] appears in @XmlType.propOrder, but no such property exists. Maybe you meant [unmapped_property_name]? this problem is related to the following location: at [JAXB_object_name]
  • JBert
    JBert over 11 years
    @jghost: sounds like you have set an XmlType element on your class with a propOrder setting and now XJC is complaining. Maybe you should just remove the field name from that propOrder setting and see how it reacts?
  • OrangeDog
    OrangeDog over 7 years
    Then it complains that unmapped_property_name is found, but not in propOrder.