2013-09-25 53 views
1

我期望通過使用xml-path來跳過需要忽略的元素,但它似乎並沒有這樣做。我錯過了什麼嗎?eclipselink moxy 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> 

C型姑娘:

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

回答

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

} 
相關問題