5

i have some xml code, and i want to parse this to java object please help me.

   <Error>
    <number>1020</number>
    <Type>fatal</Type>
    <Text>Nagaraju</Text>
    <Text>Suresh</Text>
    <Text>Sound</Text>
    <Text>Rajesh</Text>
   </Error>

java object is

   Class Error{
      int number;
      String type;
      List<String>texts=new ArrayList<String>();
   }
1
  • I think XStream is at its best when you define an object first and then allow the XML to follow along. Commented Jul 21, 2015 at 18:35

2 Answers 2

5

I would map it manually personally, or use JAX-B perhaps, but if you really want to use XStream:

  1. You'll need to map the class Error to the element Error. You can do this with an "alias". http://x-stream.github.io/alias-tutorial.html
  2. The List isn't usually represented that way, it would normally be nested. Serialise your Error object out to XML to see what it would normally be represented as. If you want to do this way you'll likely need a convertor: http://x-stream.github.io/converter-tutorial.html
  3. You could also use implicit collections, by registering the Error and text element. http://x-stream.github.io/javadoc/com/thoughtworks/xstream/XStream.html#addImplicitCollection(java.lang.Class, java.lang.String, java.lang.Class)
Sign up to request clarification or add additional context in comments.

4 Comments

I think you can use implicit collections instead of a converter
is there any way to parse that xml other that converter?
How to use that implicit collections?
-1

use the code:

    String response = "<\patron_tenders><\patron_tender name=\"alpha\" city=\"mumbai\" >"
+ "<\partial_card_number>************6319<\partial_card_number>"
+ "<\zip_code>123456<\zip_code>"
+ "<\tender_type_name>Visa<\tender_type_name>"
+ "<\token_id>80195<\token_id>" + "<\expired>false<\expired>"
+ "<\patron_tender>"+"<\patron_tender name=\"beta\" city=\"pune\" >"
+ "<\partial_card_number>************3545<\partial_card_number>"
+ "<\zip_code>565768<\zip_code>"
+ "<\tender_type_name>Visa2<\tender_type_name>"
+ "<\token_id>83397<\token_id>" + "<\expired>true<\expired>"
+ "<\patron_tender><\patron_tenders>"; 

1 Comment

There is usually some correlation between question and answer, but for this answer: not so much.