I want to convert the XML file to Java object which have array. Objective is to store the xml values into the array of the object. Suppose XML file as:
<test>
<Name> John</Nmae>
<age>19</age>
<phone>2225364</phone>
</test>
Now the Java Class is like:
public class TestArray
{
private List list = new ArrayList();
public display()
{
Iterator<String> itr = list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
I want to parse the xml file to object so that I can retrieve the value of Name, Age and Phone from List. My approach is mention below,but it throws an error like
Exception in thread "main" com.thoughtworks.xstream.converters.ConversionException: Name : Name : Name : Name:
My Approach:
FileReader fr = new FileReader("test.xml");
XStream xstream = new XStream(new StaxDriver());
xstream.alias("test", TestArray.class);
xstream.addImplicitCollection(TestArray.class,"list");
TestArray ta = (TestArray) xstream.fromXML(fr);
ta.display();
You need to create the list of object having the exact name as xml, so that it can map them. You need to have test class for each set of tags.
private List<Test> list = new ArrayList<Test>();
public class Test{
private String Name;
//other field and getter setters
}