I created a list of polymorphic objects. I can convert them into XML and vice versa (marshall and unmarshal using JAXB.) But I would like to read only the objects from the chosen class.

I used a few tutorials about converting object lists by JAXB, for example this one:
<http://memorynotfound.com/convert-xml-to-polymorphic-object-using-jax-b/> or this one: <http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html>
I have got Animal list, and two classes that extends the abstract animal class, Dog, Cat and a Bird. I saved that list(Containing dogs and cats) to XML, and I would like to get from the XML file only the Cats. Is it possible to make?

Using the code from the tutorial -> <http://memorynotfound.com/convert-xml-to-polymorphic-object-using-jax-b/> 

```
    JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    Root root = (Root)unmarshaller.unmarshal(new StringReader(xml));

```

I would get ouptut: 

> 
> Root{animals=[com.memorynotfound.xml.jaxb.Dog@520a3426, com.memorynotfound.xml.jaxb.Cat@18eed359,com.memorynotfound.xml.jaxb.Bird@643bd123]}
> 
> 
> 

What i Try to achieve is:

> 
> Root{animals=[ com.memorynotfound.xml.jaxb.Cat@18eed359]}
> 
> 
> 

In one of the answers i got an idea of XMLEvent. It is a very fine idea, but almost every tutorial says about filtering. What I want to achieve is to get rid of all the other classes.
I tried to change the code:

```
public XMLEvent nextEvent() throws XMLStreamException {
    // Read next event
    XMLEvent e = super.nextEvent();
    // If it's a start element for dog
    if (e.getEventType() == XMLEvent.START_ELEMENT &&  ("Cat".equals(e.asStartElement().getName().getLocalPart()) ||
            "Bird".equals(e.asStartElement().getName().getLocalPart()))
    )
    {

        // Then run through events until a closing dog event
        do {
            e = super.nextEvent();
        } while (e.getEventType() != XMLEvent.END_ELEMENT || ! ("Cat".equals(e.asStartElement().getName().getLocalPart()) ||
                "Bird".equals(e.asStartElement().getName().getLocalPart()))

        );
        // then read next event after the  ends
        e = super.nextEvent();
    }
    return e;
}

```

But it is not working, so I am not sure if I understand the solution.