Reputation: 69
I am doing a project using JAXB and am stuck with a problem. the output of the program ie fore the length of the indexFields am getting a null value. My xml file looks like this
<?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>
I have written a sample code
@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;
}
}
file 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;
}
}
test code to run it
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();
}
}
}
Upvotes: 2
Views: 8536
Reputation: 149047
The mappings on your object model do not map the XML document this is why it isn't populating correctly during the unmarshal operation. You can get a sense of this by populating your object model and then marshalling it to see the XML output. Below is a set of mappings you could use instead.
JAVA MODEL
XmlImportConfig
Since indexFields
is a repeating element it is best to map it to a list field/property.
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
Since indexList
is just a grouping element we will use an @XmlElementWrapper
annotation to map it.
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;
}
}
Index
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class Index {
@XmlAttribute
private String name;
@XmlAttribute
private String value;
}
DEMO CODE
Demo
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();
}
}
}
Output
get Index Field value Corporate - Finance Documents
get Index Field value Corporate - Governance Documents
length of indexFields [forum17440308.IndexFields@47533240, forum17440308.IndexFields@27082c55]
Upvotes: 2