2017-04-02 72 views
0

JAXBを使用してXMLデータモデルを作成していますが、複雑ではないXMLモデルではうまく機能しているようですが、少し複雑なXMLでは、JAXBもうまく動作していないようです。入れ子POJOクラスのJaxbを使用してJavaでXMLモデルを生成する方法

これは私がacheiveしたいXML-世代です:

<?xml version="1.0" encoding="UTF-8"?> 
<SampleRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="Sample.xsd"> 

    <Name>Sample Name</Name> 
    <Description>This is a description</Description> 
    <Graph>Graph test</Graph> 

    <VerifyAttr> 
     <Data>id</Data> 
     <Value>32</Value> 
    </VerifyAttr> 

    <VeirfyXpath> 
     <MemXpath>/Root/Name</MemXpath> 
     <Value>Mosawi</Value> 
    </<VeirfyXpath>> 

</SampleRoot> 

これは私が作成したネストされたPOJOクラス(私は、ネストされたPOJOを作成するべきではありませんおそらく、それらを分離?)である:

package com.sample.model; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement(name="SampleRoot") 
public class Sample { 
    private String name; 
    private String description; 
    private String graph; 

    public String getName() { 
     return name; 
    } 
    @XmlElement 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getDescription() { 
     return description; 
    } 
    @XmlElement 
    public void setDescription(String description) { 
     this.description = description; 
    } 
    public String getGraph() { 
     return graph; 
    } 
    @XmlElement 
    public void setGraph(String graph) { 
     this.graph = graph; 
    } 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public class VerifyAttr { 
     private String data; 
     private String value; 

     public String getData() { 
      return data; 
     } 
     @XmlElement 
     public void setData(String data) { 
      this.data = data; 
     } 
     public String getValue() { 
      return value; 
     } 
     @XmlElement 
     public void setValue(String value) { 
      this.value = value; 
     } 

    } 

    @XmlAccessorType(XmlAccessType.FIELD) 
    public class VerifyXpath { 
     private String memXpath; 
     private String value; 

     public String getMemXpath() { 
      return memXpath; 
     } 
     @XmlElement 
     public void setMemXpath(String memXpath) { 
      this.memXpath = memXpath; 
     } 
     public String getValue() { 
      return value; 
     } 
     @XmlElement 
     public void setValue(String value) { 
      this.value = value; 
     } 

    } 

} 

そしてここでPOJOモデルを行使するためのデモです:

public static void main(String[] args) { 
    Sample sample = new Sample(); 

    sample.setName("Name"); 
    sample.setDescription("This is a description"); 
    sample.setGraph("Graph Test"); 

    VerifyAttr va = sample.new VerifyAttr(); 
    va.setData("id"); 
    va.setValue("32"); 

    VerifyXpath vx = sample.new VerifyXpath(); 
    vx.setMemXpath("/Root/Name"); 
    vx.setValue("Mosawi"); 


    try { 
     JAXBContext jaxbContext = JAXBContext.newInstance(Sample.class); 
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 

     jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

     jaxbMarshaller.marshal(sample, new File("Sample.xml")); 
     jaxbMarshaller.marshal(sample, System.out); 

    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
} 

私が持っている問題はそのトンであります上で定義した完全なXMLは生成されません。これは生成されるものです:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<SampleRoot> 
    <description>This is a description</description> 
    <graph>Graph Test</graph> 
    <name>Name</name> 
</SampleRoot> 

私は間違っていますか?上記のようにモデルXMLを取得できないのはなぜですか?問題はdemo mainですか?

答えて

0

さて、私はJaxBのいくつかのドキュメントを読んでこれを理解することができました。

@XmlRootElement 
@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlType(propOrder={"dataXpath","valueXpath"}) 
public class VerifyAttr { 
    //... 
} 
VerifyXpath.java

Sample.java VerifyAttr.java VerifyXpath.java

VerifyAttr.java

@XmlRootElement 
@XmlAccessorType(XmlAccessType.PROPERTY) 
@XmlType(propOrder={"dataAttr","valueAttr"}) 
public class VerifyAttr { 
    //... 
} 

:私は、JAXB注釈を3クラスにPOJOをデカップリングして調整してきました

ルートノードのトップレベル:

Sample.java

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class VerifyAttr { 
    @XmlElement(name="VerifyAttr", type=VerifyAttr.class) 
    private ArrayList<VerifyAttr> va; 

    @XmlElement(name="VerifyXpath", type=VerifyXpath.class) 
    private ArrayList<VerifyXpath> vx; 

    // some other fields 
    //..getter and setters 
} 

うまくいけば、これは、JAXB

に他の誰かが新しいのに役立ちます
関連する問題