how to use ArrayList

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

how to use ArrayList

Rushit Patel
 
I want the xml file as below
 
<!-- -------------------------------------------------------------------- -->
<animals>
    <animal>dog<animal>
    <animal>cat<animal>
</animals>
<!--  ---------------------------------------------------------------------------------------------------------- -->
 
The number of animal may increase or decrease
 
 
one code which i had tried but give diferent output
 
class animal {
 List animals = new ArrayList();;
 
     animal()
    {
      animals.add("dog");
      animals.add("goat");
     }
public static void main(String[] args) {
      XStream xStream = new XStream(new DomDriver());
      xStream.alias("animal",animal.class);
 
  
            System.out.println(xStream.toXML(new animal()));
           }
 
}
 
THE OUTPUT I GET IS
 

<animal>

<animals>

<string>dog</string>

<string>goat</string>

</animals>

<animal>

 
 
Can anyone tell me how to code so that i get expected output
 
--
Rushit Patel
Software Engineer
C-SAM (INDIA) PVT. LTD. 
www.c-sam.com
 
e-mail: [hidden email]
+ 91 265 2324 018/19 (O)
+ 91 265 2334 156    (F)
+ 91 982 5745 937   (M)
________________________
 
Confidentiality Notice
This e-mail and the document(s) accompanying this e-mail is intended solely for the recipient(s) to which it is addressed, and contains information that may be confidential or privileged and exempt from disclosure under applicable law. If you are not the intended recipient, be aware that any disclosure, copying, distribution or use of the contents of this e-mail is not authorized and is prohibited. If you have received this e-mail in error, please notify us immediately, and delete each and every copy of it.
 
 
 

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.12/220 - Release Date: 1/3/2006

Reply | Threaded
Open this post in threaded view
|

Re: how to use ArrayList

Kannan Ekanath
I think the output is very clear, animal is the class and under that you the array list,
Instead of ,
System.out.println(xStream.toXML(new animal()));
try,
System.out.println(xStream.toXML(new animal().getAnimals())); //assuming getter returns an arraylist or a list.

If you still want to call toXML only on the animal and do not want the <animal> tag you need to look at teh transformers

On 1/4/06, Rushit Patel <[hidden email]> wrote:
 
I want the xml file as below
 
<!-- -------------------------------------------------------------------- -->
<animals>
    <animal>dog<animal>
    <animal>cat<animal>
</animals>
<!--  ---------------------------------------------------------------------------------------------------------- -->
 
The number of animal may increase or decrease
 
 
one code which i had tried but give diferent output
 
class animal {
 List animals = new ArrayList();;
 
     animal()
    {
      animals.add("dog");
      animals.add("goat");
     }
public static void main(String[] args) {
      XStream xStream = new XStream(new DomDriver());
      xStream.alias("animal",animal.class);
 
  
            System.out.println(xStream.toXML(new animal()));
           }
 
}
 
THE OUTPUT I GET IS
 

<animal>

<animals>

<string>dog</string>

<string>goat</string>

</animals>

<animal>

 
 
Can anyone tell me how to code so that i get expected output
 
--
Rushit Patel
Software Engineer
C-SAM (INDIA) PVT. LTD. 
<a href="http://www.c-sam.com/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">www.c-sam.com
 
e-mail: [hidden email]
+ 91 265 2324 018/19 (O)
+ 91 265 2334 156    (F)
+ 91 982 5745 937   (M)
________________________
 
Confidentiality Notice
This e-mail and the document(s) accompanying this e-mail is intended solely for the recipient(s) to which it is addressed, and contains information that may be confidential or privileged and exempt from disclosure under applicable law. If you are not the intended recipient, be aware that any disclosure, copying, distribution or use of the contents of this e-mail is not authorized and is prohibited. If you have received this e-mail in error, please notify us immediately, and delete each and every copy of it.
 
 
 

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.12/220 - Release Date: 1/3/2006




--
Regards,
Kannan Ekanath
-- You aint any expert, if you are - "PROUD"
Reply | Threaded
Open this post in threaded view
|

Re: how to use ArrayList

Jörg Schaible-2
In reply to this post by Rushit Patel
Rushit Patel wrote:

>  
> I want the xml file as below
>  
> <!-- --------------------------------------------------------------------
> -->
> <animals>
>     <animal>dog<animal>
>     <animal>cat<animal>
> </animals>
> <!--
>
----------------------------------------------------------------------------
> ------------------------------ -->

[snip]

Use an implicit collection. See acceptance test "ImplicitCollectionTest".

- Jörg

Reply | Threaded
Open this post in threaded view
|

RE: Re: how to use ArrayList

Rushit Patel
Thanks very much ...

It solved my problem .

Cheers
Rushit patel


-----Original Message-----
From: news [mailto:[hidden email]] On Behalf Of Jörg Schaible
Sent: Thursday, January 05, 2006 10:13 PM
To: [hidden email]
Subject: [xstream-user] Re: how to use ArrayList

Rushit Patel wrote:

>  
> I want the xml file as below
>  
> <!--
> --------------------------------------------------------------------
> -->
> <animals>
>     <animal>dog<animal>
>     <animal>cat<animal>
> </animals>
> <!--
>
----------------------------------------------------------------------------
> ------------------------------ -->

[snip]

Use an implicit collection. See acceptance test "ImplicitCollectionTest".

- Jörg


--
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.13/221 - Release Date: 1/4/2006
 

--
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.371 / Virus Database: 267.14.14/222 - Release Date: 1/5/2006