1

我期待通过使用 xml-path 它跳过需要忽略的元素,但它似乎没有这样做。我错过了什么吗?

它应该忽略嵌套元素“responseHeader”

错误:

Caused by: org.xml.sax.SAXParseException: unexpected element (uri:"", local:"responseHeader"). Expected elements are <{}response>
    at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startUnmappedElement(UnmarshalRecordImpl.java:957)
    at org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl.startElement(UnmarshalRecordImpl.java:814)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2756)

这是xml输入

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/admin/tabular.xsl"?>
<response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://atomics.aol.com/cnet-search/response.xsd">
<responseHeader>
<status>0</status>
<sql>select * from csource_WEEKBOX LIMIT 0,20</sql>
<numFields>15</numFields>
<numRecords>10</numRecords>
<QTime>0</QTime>
</responseHeader>
<responseBody>
<record>
<field type="integer">
<name>rank</name>
<value>1</value>
</field>
<field type="integer">
<name>movieid</name>
<value>143966</value>
</field>
<field type="integer">
<name>mf_mid</name>
<value>58759</value>
</field>
<field type="string">
<name>movietitle</name>
<value>Prisoners</value>
</field>
<field type="integer">
<name>previous</name>
<value>0</value>
</field>
<field type="integer">
<name>gross</name>
<value>21430000</value>
</field>
<field type="integer">
<name>grosschange</name>
<value>0</value>
</field>
<field type="integer">
<name>number</name>
<value>3260</value>
</field>
<field type="integer">
<name>numberchange</name>
<value>0</value>
</field>
<field type="integer">
<name>average</name>
<value>6574</value>
</field>
<field type="integer">
<name>cume</name>
<value>21430000</value>
</field>
<field type="integer">
<name>weeks</name>
<value>1</value>
</field>
<field type="integer">
<name>batchNumber</name>
<value>104323</value>
</field>
<field type="string">
<name>rowStatus</name>
<value>I</value>
</field>
<field type="date">
<name>updateDateTime</name>
<value>2013-09-24 11:08:34</value>
</field>
</record>
<record>
...

绑定文件:

<?xml version="1.0"?>
<xml-bindings 
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        version="2.1" package-name="com.dao">
    <java-types>
        <java-type name="DaoResult">
            <xml-root-element name="response"/>
            <xml-type prop-order="results"/>
            <java-attributes>
                <xml-element java-attribute="results" xml-path="responseBody/record" type="com.aol.pxy.movies.model.WeekBox" container-type="java.util.List"/>
                <xml-transient java-attribute="totalCount"/>
                <xml-transient java-attribute="cachedTime"/>
                <xml-transient java-attribute="firstResult"/>
                <xml-transient java-attribute="inputQuery"/>
                <xml-transient java-attribute="paginationHandle"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

型号类:

public class DaoResult<T>  extends AbstractModel implements Serializable {

    private static final long serialVersionUID = -6369563906973225250L;

    @XmlTransient
    private String inputQuery;

    @Deprecated
    private Integer count = -1;

    private Integer totalCount;

    private List<T> results;

    private String paginationHandle;

    private Date cachedTime = new Date();
4

1 回答 1

0

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

当我不小心将输入文档指向外部映射文档时,我遇到了类似的错误。当我做下面的小样本时,一切都按预期工作。

演示代码

在下面的示例代码中oxm.xml是外部映射文档,并且input.xml是输入数据文档。

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "src/com/dao/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {DaoResult.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/com/dao/input.xml");
        DaoResult result = (DaoResult) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(result, System.out);
    }

}
于 2013-10-02T15:53:47.170 回答