Jaxb返回null值

时间:2013-07-03 05:26:03

标签: java jaxb

我正在使用JAXB做一个项目,我遇到了问题。程序的输出,即indexFields的长度得到一个空值。  我的xml文件看起来像这样

<?xml version="1.0" ?> 
<XmlImportConfig>
    <indexfields value="Corporate - Finance Documents">
        <indexlist>
            <index name="Field1" value="FDEPTNAME"/>
            <index name="Field2" value="FDEPTGRP"/>
            <index name="Field3" value="FDOCAREA"/>
            <index name="Field4" value="FDOCTYPE"/>
            <index name="Field10" value="RECDISPCODE"/>
            <index name="Field11" value="RECMODIFYDATE"/>
            <index name="Field12" value="RECDISPDATE"/>  
            <index name="Field15" value="FILECMIDKEY"/>  
        </indexlist>
    </indexfields>
    <indexfields value="Corporate - Governance Documents">
        <indexlist>
            <index name="Field1" value="GDEPTNAME"/>
            <index name="Field2" value="GDEPTGRP"/>
            <index name="Field3" value="GDOCAREA"/>
            <index name="Field4" value="GDOCTYPE"/>
            <index name="Field10" value="RECDISPCODE"/>
            <index name="Field11" value="RECMODIFYDATE"/>
            <index name="Field12" value="RECDISPDATE"/>  
            <index name="Field15" value="FILECMIDKEY"/>  
        </indexlist>
  </indexfields>
</XmlImportConfig>

我写了一个示例代码

@XmlRootElement(name="XmlImportConfig")
public class XmlImportConfig {

    private indexfields indexField;

    private ArrayList<indexlist> listOfIndexes;
    private ArrayList<indexfields> listOfIndexFields;


    public indexfields getIndexField() {
        return indexField;
    }

    @XmlElement(name="indexfields")
    public void setIndexField(indexfields indexField) {
        this.indexField = indexField;
    }

    public ArrayList<indexlist> getListOfIndexes(){
        return listOfIndexes;
    }


    public ArrayList<indexfields> getListOfIndexFields(){
        return listOfIndexFields;
    }

    @XmlElementWrapper(name = "XmlImportConfig")

    @XmlElement(name = "indexfields")
    public void setListOfIndexFields(ArrayList<indexfields> listOfIndexFields) {
        this.listOfIndexFields = listOfIndexFields;
    } 



}

文件2:

@XmlRootElement(name="indexfields")
@XmlAccessorType(XmlAccessType.FIELD)
public class indexfields {

    @XmlAttribute
    private String value;
    private String indexlist;

    public String getValue() {
        return value;
    }


    public void setValue(String value) {        
        this.value = value;
    }

}

测试代码来运行它

public class testXml {
 public static void main(String[] args) {

  try {

   // create JAXB context and initializing Marshaller
   JAXBContext jaxbContext = JAXBContext.newInstance(XmlImportConfig.class);

   Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

   // specify the location and name of xml file to be read
   File XMLfile = new File("C:\\Users\\Admin\\Desktop\\xml file\\KeywordFormConfig.xml");

   // this will create Java object - country from the XML file
   XmlImportConfig importCOnfig = (XmlImportConfig) jaxbUnmarshaller.unmarshal(XMLfile);



   System.out.println("get Index Field value  "+((indexfields)importCOnfig.getIndexField()).getValue());


   System.out.println("length of indexFields "+importCOnfig.getListOfIndexFields());

} catch (JAXBException e) {
   // some exception occured
   e.printStackTrace();
  }

 }
}

1 个答案:

答案 0 :(得分:2)

对象模型上的映射不会映射XML文档,这就是在解组操作期间不能正确填充的原因。您可以通过填充对象模型然后对其进行编组以查看XML输出来了解这一点。下面是一组您可以使用的映射。

JAVA模型

XmlImportConfig

由于indexFields是重复元素,因此最好将其映射到列表字段/属性。

import java.util.ArrayList;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="XmlImportConfig")
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlImportConfig {

    @XmlElement(name="indexfields")
    private ArrayList<IndexFields> listOfIndexFields;

    public ArrayList<IndexFields> getListOfIndexFields() {
        return listOfIndexFields;
    }

}

IndexFields

由于indexList只是一个分组元素,我们将使用@XmlElementWrapper注释来映射它。

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class IndexFields {

    @XmlAttribute
    private String value;

    @XmlElementWrapper
    @XmlElement(name="index")
    private List<Index> indexlist;

    public String getValue() {
        return value;
    }

}

<强>索引

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Index {

    @XmlAttribute
    private String name;

    @XmlAttribute
    private String value;

}

DEMO CODE

演示

import java.io.File;
import javax.xml.bind.*;

public class TestXml {

    public static void main(String[] args) {
        try {
            // create JAXB context and initializing Marshaller
            JAXBContext jaxbContext = JAXBContext
                    .newInstance(XmlImportConfig.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            // specify the location and name of xml file to be read
            File XMLfile = new File("src/forum17440308/KeywordFormConfig.xml");

            // this will create Java object - country from the XML file
            XmlImportConfig importCOnfig = (XmlImportConfig) jaxbUnmarshaller
                    .unmarshal(XMLfile);

            for (IndexFields indexFields : importCOnfig.getListOfIndexFields()) {
                System.out.println("get Index Field value  "
                        + indexFields.getValue());
            }
            System.out.println("length of indexFields "
                    + importCOnfig.getListOfIndexFields());

        } catch (JAXBException e) {
            // some exception occured
            e.printStackTrace();
        }
    }

}

输出

get Index Field value  Corporate - Finance Documents
get Index Field value  Corporate - Governance Documents
length of indexFields [forum17440308.IndexFields@47533240, forum17440308.IndexFields@27082c55]