2012-03-26 28 views
1

我有一個需要映射到Java對象(即DTO)的XML。我的XML有不有我的DTO任何Java對象的一些包裝元素..我的XML看起來像這樣無法使用Jaxb和XMLPath對XML進行解組

<UseCaseView> 
<FindCandidates> 
    <CandidatesRequest> 
     <APIRequest> 
      <Code>Code</Code> 
     </APIRequest> 
    </CandidatesRequest> 
</FindCandidates> </UseCaseView> 

的「FindCandidates」和「CandidatesRequest」只是包裝元素和「APIRequest」再次一個DTO ..

我使用xmlpath中像這樣在我的DTO ..我的DTO是這樣的..

@XmlRootElement(name = "UseCaseView") 
public class FindRequestDTO implements Serializable{ 

private static final long serialVersionUID = 5528726225975606325L; 

private ApiRequestDTO apiRequest; 


@XmlPath("FindCandidates/CandidatesRequest/APIRequest") 
public ApiRequestDTO getAPIRequest() { 
    return apiRequest; 
    ......... 

這不是APIRequest元素映射到我的ApiRequestDTO,如果我刪除了兩個包裝元素和 直接使用XMLElement( NAME =「APIRequest」),它的工作......但我需要忽略這兩個包裝元素,構建我的DTO .. 我已經加入了Jaxb.properties與

"javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory" 
在我的資源文件夾中文件

有人可以幫助我知道怎麼回事錯在這裏..

感謝,

+0

:當使用本機API,你不需要jaxb.properties文件?以下示例可能有所幫助:https://github.com/bdoughan/blog20110908。如果沒有,我明天會發布答案。 – 2012-03-26 23:27:32

+0

@Blaise - thx快速回復..返回的實現類是com.sun.xml.bind.v2.runtime.JAXBContextImpl – sampath 2012-03-26 23:36:58

+0

您的類路徑中是否有eclipselink.jar?我在之前的評論中鏈接到的示例都是使用Maven進行編譯和運行的設置。 – 2012-03-26 23:42:44

回答

2

注:我是EclipseLink JAXB (MOXy)鉛和JAXB 2 (JSR-222)專家小組的成員。

下面是一個應該幫助的完整示例。

jaxb.properties

要指定莫西爲您的JAXB提供者,你需要添加一個名爲jaxb.properties文件,在同一個包以下項爲您的域模型。

javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory 

FindRequestDTO

package forum9881188; 

import java.io.*; 
import javax.xml.bind.annotation.*; 
import org.eclipse.persistence.oxm.annotations.*; 

@XmlRootElement(name = "UseCaseView") 
public class FindRequestDTO implements Serializable { 

    private static final long serialVersionUID = 5528726225975606325L; 

    private ApiRequestDTO apiRequest; 

    @XmlPath("FindCandidates/CandidatesRequest/APIRequest") 
    public ApiRequestDTO getAPIRequest() { 
     return apiRequest; 
    } 

    public void setAPIRequest(ApiRequestDTO apiRequest) { 
     this.apiRequest = apiRequest; 
    } 

} 

ApiRequestDTO

package forum9881188; 

public class ApiRequestDTO { 
} 

演示

package forum9881188; 

import javax.xml.bind.*; 

public class Demo { 

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

     FindRequestDTO fr = new FindRequestDTO(); 
     fr.setAPIRequest(new ApiRequestDTO()); 

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

} 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<UseCaseView> 
    <FindCandidates> 
     <CandidatesRequest> 
     <APIRequest/> 
     </CandidatesRequest> 
    </FindCandidates> 
</UseCaseView> 

更多信息


UPDATE

如果由於某種原因,你不能得到莫西實施JAXBContext,你總是可以使用本機API來引導。當你創建`JAXBContext`什麼是返回的實現類

package forum9881188; 

import javax.xml.bind.*; 
import org.eclipse.persistence.jaxb.JAXBContextFactory; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     //JAXBContext jc = JAXBContext.newInstance(FindRequestDTO.class); 
     JAXBContext jc = JAXBContextFactory.createContext(new Class[] {FindRequestDTO.class}, null); 

     FindRequestDTO fr = new FindRequestDTO(); 
     fr.setAPIRequest(new ApiRequestDTO()); 

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

} 
+0

當我嘗試更新解決方案時,我got:Caught:groovy.lang.MissingMethodException:方法沒有簽名:static org.eclipse.persistence.jaxb.JAXBContextFactory.createContext()適用於參數類型:([Ljava.lang.Object; null] values:[[ class] CommonSvcRs],null] 可能的解決方案:createContext([Ljava.lang.Class; java.util.Map),createContext(java.lang.String,java.lang.ClassLoader),createContext([Ljava.lang.Class ;,java.util.Map,java.lang.ClassLoader),createContext([Ljava.lang.reflect.Type; java.util.Map,java.lang.ClassLoader),... – Norm212 2014-04-08 20:12:31