You could use the @XmlPath extension in EclipseLink JAXB (MOXy) to handle this case (I’m the MOXy tech lead):
@XmlRootElement(name="addressbook")
public class Addressbook implements Serializable {
private ArrayList<Company> companyList = new ArrayList<Company>();
public Addressbook() {
}
@XmlPath("company[@name="abc"]")
public ArrayList<Company> getCompanyList() {
return companyList;
}
}
For More Information:
UPDATE – Using StreamFilter
The example below demonstrates how a StreamFilter could be leveraged for this use case:
import java.io.FileInputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Addressbook.class);
XMLInputFactory xif = XMLInputFactory.newFactory();
FileInputStream xmlStream = new FileInputStream("input.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xmlStream);
xsr = xif.createFilteredReader(xsr, new CompanyFilter());
Unmarshaller unmarshaller = jc.createUnmarshaller();
Addressbook addressbook = (Addressbook) unmarshaller.unmarshal(xsr);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(addressbook, System.out);
}
}
The implementation of the StreamFilter is as follows:
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLStreamReader;
public class CompanyFilter implements StreamFilter {
private boolean accept = true;
public boolean accept(XMLStreamReader reader) {
if(reader.isStartElement() && "company".equals(reader.getLocalName())) {
accept = "abc".equals(reader.getAttributeValue(null, "name"));
} else if(reader.isEndElement()) {
boolean returnValue = accept;
accept = true;
return returnValue;
}
return accept;
}
}