8

我正在尝试做相反的事情:JAXB - Ignore element

那就是我正在尝试将带有 JAXB 的 XML 解析为 POJO,如果它没有为给定元素找到匹配的 Java 属性,我希望 JAXB 失败。我知道我可能可以进行模式验证,但我宁愿不这样做。

例子:

@XmlRootElement(name="Dog")
public class Dog {
    private String name;
    public String getName() {return name;}
    public String setName(String name) {this.name = name};
}

此 XML 将失败:

<Dog>
<id>blah</id>
<name>Stuff</name>
</Dog>

因为没有idJava 属性

4

2 回答 2

6

您可以利用 aValidationEventHandler来获得您正在寻找的行为。意外元素被视为默认情况下忽略的警告。为了支持您的用例,您可以提供一个ValdidationEventHandler失败的警告(见下文)。

演示

package forum10721257;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Dog.class);

        File xml = new File("src/forum10721257/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler () {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                return false;
            }

        });
        Dog dog = (Dog) unmarshaller.unmarshal(xml);
    }

}

JAXB RI 异常

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"id"). Expected elements are <{}name>
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:90)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:236)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2715)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:488)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:202)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:174)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
    at forum10721257.Demo.main(Demo.java:26)

EclipseLink JAXB (MOXy)

我们最近将此行为添加到 MOXy。您将需要使用 EclipseLink 2.4.0 或 2.3.3。可以从以下位置获得夜间构建:


更新

我需要Dog根据您的问题调整课程。下面是我用的。

package forum10721257;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Dog")
public class Dog {
    private String name;
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
}
于 2012-05-23T17:39:14.590 回答
0

我能够验证缺少的 Java 属性(无模式)的唯一方法是将 XML 解组为我的 POJO,然后使用相同的 POJO 将其重新编组为 XML 并进行比较。

下面是一个使用 XMLUnit 的示例,它有助于忽略顺序和空格问题。

@Test
public void testSerialization() throws Exception {
    XMLUnit.setIgnoreWhitespace(true);

    EventSearchResults r = 
            JAXB.unmarshal(getClass().getResourceAsStream("/EventSearchResults.xml"), 
                    EventSearchResults.class);
    StringWriter sw = new StringWriter();
    JAXB.marshal(r, sw);
    String expected = 
            IOUtils.toString(getClass().getResourceAsStream("/EventSearchResults.xml"), "UTF-8");
    String actual = sw.toString();
    //System.out.println(expected);
    System.out.println(actual);
    assertXMLEqual(expected, actual);
}

这并不理想,但在大多数情况下适用于单元测试。

于 2012-05-23T17:29:51.047 回答