2

I am relatively new to JAXB, but I have a question that seems to be difficult to resolve via online documentation. My goal is simple: I have XML that is being parsed using JAXB, such that elements where included=false should not be part of the resulting object model.

I would like to solve this within the schema:

<complexType name="baseFilterType">
    <simpleContent>
        <extension base="string">
            <attribute type="string" name="code" use="required"/>
            <attribute type="boolean" name="isMandatory" default="false"/>
            <attribute type="boolean" name="included" default="true"/>
        </extension>
    </simpleContent>
</complexType>

<complexType name="baseFiltersType">
    <sequence>
        <element type="rptt:baseFilterType" name="filter" maxOccurs="unbounded" 
          minOccurs="0" />
    </sequence>
</complexType>

So, how do I change the schema shown above such that the any baseFilterType where included = false will not be apart of the object model generated for a baseFiltersType object.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Ryan Delucchi
  • 7,718
  • 13
  • 48
  • 60

1 Answers1

2

You could leverage the @XmlPath extension in EclipseLink JAXB (MOXy), I'm the MOXy tech lead.

@XmlRootElement
public class BaseFiltersType {

    private List<BaseFilterType> filter;

    @XmlPath("filter[@included='true']")
    public List<BaseFilterType> getFilter() {
        return filter;
    }

    public void setFilter(List<BaseFilterType> baseFilterType) {
        this.filter = baseFilterType;
    }

}

Answer to Related Question

In one of my answers to a related question I demonstrate how to use MOXy's @XmlPath annotation, and a StAX StreamFilter approach that would work with any JAXB implementation:

For More Information:

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • @Ryan Delucchi - In an answer to a related question ( http://stackoverflow.com/questions/5949265/jaxb-filtered-parsing/5950542#5950542 ), I have added an example of using a StAX StreamFilter. This would mean you would not need to make any Schema or JAXB changes to get the behvaviour you are looking for. – bdoughan May 12 '11 at 15:52
  • Awesome, thanks a lot! I think I like the StreamFilter approach best. Simplicity is what I am after for this particular task. Cheers. – Ryan Delucchi May 12 '11 at 16:25