1
votes

I was expecting by using xml-path that it skips elements which needs to be ignored, but it doesn't seem to be doing that. Am I missing something?

It should be ignoring the nested element "responseHeader"

Error:

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)

Here's the xml input

<?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>
...

binding file:

<?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>

Model class:

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();
1

1 Answers

0
votes

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

I got a similar error to you when I accidentally pointed to the input document as the external mapping document. When I did the little sample below everthing worked as expected.

Demo Code

In the example code below oxm.xml is the external mapping document, and input.xml is the input data document.

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);
    }

}