无法使用Jaxb和XMLPath对XML进行解组

问题描述:

我有一个需要映射到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

注:我是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