How to ignore some tags in XML in Jaxb

2019-02-19 00:56发布

问题:

my xml file is below:

<ExternalCases>
    <ignoreTag>
        <CashLess>
            <caseType>CashLess</caseType>
            <claimNo>9</claimNo>
            <status>Open</status>
        </CashLess>
    </ignoreTag>
</ExternalCases>

i want to ignore <ignoreTag> and i want <CashLess> tag in my Unmarshaller process.

my class like below:

@XmlRootElement(name="ExternalCases")
public class ExternalCases {

    List<CashLess> cashLess;

    @XmlElement(name="CashLess", required=false)
    public List<CashLess> getCashLess() {
        return cashLess;
    }
    public void setCashLess(List<CashLess> cashLess) {
        this.cashLess = cashLess;
    }
}

Thanks.

回答1:

Ignoring ignoreTag

You could create a StAX filtered XMLStreamReader and have JAXB unmarshal that to ignore one or more XML elements.

  • http://docs.oracle.com/javase/7/docs/api/javax/xml/stream/XMLInputFactory.html#createFilteredReader%28javax.xml.stream.XMLStreamReader,%20javax.xml.stream.StreamFilter%29

Below is a link to a full example I gave in an answer to a similar question:

  • JAXB filtered parsing

If ignoreTag is a Grouping Element

If instead ignoreTag is a grouping element for the collection then you could map it as:

@XmlElementWrapper(name="ignoreTag")
@XmlElement(name="CashLess", required=false)
public List<CashLess> getCashLess() {
    return cashLess;


回答2:

Use answers of these questions::

JAXB ignoring xml tag attribute

JAXB - Ignore element

might be helpful!!!



标签: java xml jaxb