gpt4 book ai didi

java - Jaxb 返回空值

转载 作者:行者123 更新时间:2023-11-30 09:20:22 27 4
gpt4 key购买 nike

我正在使用 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();
}

}
}

最佳答案

对象模型上的映射不映射 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;
}

}

索引字段

因为 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;

}

演示代码

演示

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]

关于java - Jaxb 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17440308/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com