2

I want to do something similar to XMLStreamReader example @BlaiseDoughan gave in his response to JAXB filtered parsing however I need to make the filtering decision based on sub elements not current node attributes.

XMLStreamReader does not have a peek API like XMLEventReader. For example, I want to unmarshall the following XML into a Gump object whose records list ends up only containing 1 item, the record whose associated name does not start with "Filtered-".

I'm using Eclipselink 2.3.2v20111124-r10461

<gump>
    <foo>Some text</foo>
    <bar>1.245</bar>
    <records>
        <record>
            <name>Filtered-Counter</name>
            <value>1</value>
        </record>
        <record>
            <name>Golden</name>
            <value>shiny</value>
        </record>
    </records>
    <baz>1234</baz>
</gump>

Gump.java

@XmlRootElement(name = "gump")
@XmlAccessorType(XmlAccessType.FIELD)
public class Gump implements Serializable {

    private String foo;
    private String bar;
    private String baz;

    @XmlElementWrapper
    @XmlElement(name = "record")
    private List<Record> records;

    // .. Field getters/setters 

    public Gump() {
      records = new ArrayList<>();
    }
}
Community
  • 1
  • 1
NBW
  • 1,467
  • 2
  • 18
  • 27

1 Answers1

2

The easiest thing to do would be to do a regular unmarshal, and then use an unmarshal listener that cleans up the collection on the after unmarshal event.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Thnx for your feedback. So with this approach you are suggesting the listener is attached to the unmarshalling of Gump and it would I presume need to iterate and remove elements meeting the filtering criteria. If so that approach feels like it would be less performant then a solution that discards/filters them out them as they are encountered in the stream processing during unmarshalling. Is that the case or am I missing something? – NBW Mar 27 '14 at 19:31
  • 1
    @NBW - StAX is a top down parser so you are not going to be able to exclude a parent based on its children since you don't have access to the children. Performance them comes down to which is faster: `Unmarshalling and then throwing away` vs `A process of storing up XML events looking at them and then applying them`. – bdoughan Mar 27 '14 at 19:40
  • This approach worked well. Too bad you can't tell the reader to throw out the current node because in this cause I could have a callback on Record tell the reader to essentially not add itself to the DOM thats being constructed. – NBW Mar 28 '14 at 12:41