-1

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();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Samraan
  • 204
  • 1
  • 2
  • 14
  • Hmm ... I searched for [xstream tutorial](http://www.google.de/search?q=xstream+tutorial) and got a lot of them. Any problems with that? – Seelenvirtuose Dec 18 '14 at 07:08
  • It's throwing some conversion exception for the Name – Samraan Dec 18 '14 at 07:18
  • The code you posted does not throw anything. Please edit your question to completely explain your problem with showing the details of your code that fit the problem description. – Seelenvirtuose Dec 18 '14 at 07:19
  • I have mentioned my approached, hope now you can figure out the problem thanks – Samraan Dec 18 '14 at 07:24
  • A detailed problem description is still missing. Please put some effort into the question, otherwise we will not be able to help. – Seelenvirtuose Dec 18 '14 at 07:26
  • Can you please me What all details do you want?? I already gave the problem description and the approach which I followed... – Samraan Dec 18 '14 at 07:59

1 Answers1

0

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

}
Panther
  • 3,312
  • 9
  • 27
  • 50
  • thanks for your response...Can you please elaborate this approach little, I'm new to this so couldn't able to understand that... – Samraan Dec 18 '14 at 09:21
  • what else you need friend – Panther Dec 18 '14 at 09:23
  • As you said I need create the list of object having the exact name as xml, You mean xml file which is Test.xml..I'm right?? and Where I need to declare the this Stmt: private List list = new ArrayList(); – Samraan Dec 18 '14 at 09:32
  • yes, as shown in my sammple code you will have list of Test type in your existing class , and you need this Test class having field name, age and phone. Hey could you use xml with tags starting with capital letters like Age and Phone. As in java, you will be having fields names starting with Capital letter. – Panther Dec 18 '14 at 09:43