Jirong Hu | 1 May 22:23 2011
Picon

Deserialize the serialized XML, error in ArrayList

Hi

In the attached code package, I created the xml file and saved as feed.xml. When I Deserialize, it added both links and entries into entries, ended up with 0 links but 4 entries in the Feed object. Below is the output. How can I fix this?

<feed>
  <title>title</title>
  <id>id</id>
  <link href="https://127.0.0.1:9444/jazz/secure/web/console/" rel="alternate"/>
  <entry>
    <title>Add New Customer</title>
    <summary>summary1</summary>
    <updated>updated1</updated>
    <id>AddNewCustomer_TC.xml</id>
    <link href="https://localhost:9444/jazz/secure/AddNewCustomer_TC" rel="alternate" type="application/xml"/>
    <link href="https://127.0.0.1:9444/jazz/web/console....1" rel="enclosure" type="text/html" title="Web Console"/>
  </entry>
  <entry>
    <title>Add New Product</title>
    <summary>summary2</summary>
    <updated>updated2</updated>
    <id>AddNewProduct_TC.xml</id>
    <link href="https://localhost:9444/jazz/secure/AddNewProduct_TC" rel="alternate" type="application/xml"/>
    <link href="https://127.0.0.1:9444/jazz/web/console....2" rel="enclosure" type="text/html" title="Web Console"/>
  </entry>
</feed>

Feed.title=title

links null
size of entries 4
Exception in thread "main" java.lang.ClassCastException: Link incompatible with Entry
at Converter.main(Converter.java:129)

Thanks
Jirong
Jirong Hu | 1 May 22:24 2011
Picon

Deserialize the serialized XML, error in ArrayList


Hi

In the attached code package, I created the xml file and saved as feed.xml. When I Deserialize, it added both links and entries into entries, ended up with 0 links but 4 entries in the Feed object. Below is the output. How can I fix this?

<feed>
  <title>title</title>
  <id>id</id>
  <link href="https://127.0.0.1:9444/jazz/secure/web/console/" rel="alternate"/>
  <entry>
    <title>Add New Customer</title>
    <summary>summary1</summary>
    <updated>updated1</updated>
    <id>AddNewCustomer_TC.xml</id>
    <link href="https://localhost:9444/jazz/secure/AddNewCustomer_TC" rel="alternate" type="application/xml"/>
    <link href="https://127.0.0.1:9444/jazz/web/console....1" rel="enclosure" type="text/html" title="Web Console"/>
  </entry>
  <entry>
    <title>Add New Product</title>
    <summary>summary2</summary>
    <updated>updated2</updated>
    <id>AddNewProduct_TC.xml</id>
    <link href="https://localhost:9444/jazz/secure/AddNewProduct_TC" rel="alternate" type="application/xml"/>
    <link href="https://127.0.0.1:9444/jazz/web/console....2" rel="enclosure" type="text/html" title="Web Console"/>
  </entry>
</feed>

Feed.title=title

links null
size of entries 4
Exception in thread "main" java.lang.ClassCastException: Link incompatible with Entry
at Converter.main(Converter.java:129)

Thanks
Jirong

Attachment (RQM.rar): application/rar, 13 KiB
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email
Jirong Hu | 1 May 22:27 2011
Picon

Re: Deserialize the serialized XML, error in ArrayList - Code

import java.util.ArrayList;
//import java.util.List;

public class Feed {

private String title;
private String id;
private ArrayList<Link> links = new ArrayList<Link>();
private ArrayList<Entry> entries = new ArrayList<Entry>();

public Feed(String title, String id) {
this.title = title;
this.id = id;
}

public void addLink(Link link) {
links.add(link);
}

public void addEntry(Entry entry) {
entries.add(entry);
}

public ArrayList<Link> getLinks() {
return this.links;
}

public ArrayList<Entry> getEntries() {
return this.entries;
}

public String getTitle() {
return this.title;
}

public String getId() {
return this.id;
}

}

import java.util.ArrayList;
//import java.util.List;

class Entry {
private String title;
private String summary;
private String updated;
private String id;
private ArrayList<Link> links = new ArrayList<Link>();

public Entry(String title, String summary, String updated, String id) {
this.title = title;
this.summary = summary;
this.updated = updated;
this.id = id;
}
public void addLink(Link link){
links.add(link);
}

public String getTitle() {
return this.title;
}

public String getSummary() {
return this.summary;
}

public String getUpdated() {
return this.updated;
}

public String getId() {
return this.id;
}
}


//import java.util.List;

public class Link {
private String href;
private String rel;
private String type;
private String title;

// rel="enclosure" type="text/html" title="Web Console"
public Link(String href, String rel, String type, String title) {
this.href = href;
this.rel = rel;
this.type = type;
this.title = title;
}

public String getHref() {
return this.href;
}

public String getRel() {
return this.rel;
}

public String getType() {
return this.type;
}

public String getTitle() {
return this.title;
}
}


//import java.util.List;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
//import java.util.Iterator;

import com.thoughtworks.xstream.XStream;

public class Converter {

public Converter() {
// TODO Auto-generated constructor stub
}

public static void createXML() {

// links for feed, only has href and rel
Link feedlink1 = new Link(
null, null);
Link feedlink2 = new Link(
null, null);

// links for entry: rel="enclosure" type="text/html" title="Web Console"
Link entry1Link1 = new Link(
"alternate", "application/xml", null);
Link entry1Link2 = new Link(
"text/html", "Web Console");
Link entry2Link1 = new Link(
"alternate", "application/xml", null);
Link entry2Link2 = new Link(
"text/html", "Web Console");

// create two entries
Entry entry1 = new Entry("Add New Customer", "summary1", "updated1",
"AddNewCustomer_TC.xml");
entry1.addLink(entry1Link1);
entry1.addLink(entry1Link2);
Entry entry2 = new Entry("Add New Product", "summary2", "updated2",
"AddNewProduct_TC.xml");
entry2.addLink(entry2Link1);
entry2.addLink(entry2Link2);

// create the feed
Feed feed = new Feed("title", "id");
feed.addLink(feedlink1);
feed.addLink(feedlink2);
feed.addEntry(entry1);
feed.addEntry(entry2);

XStream xstream = createXStreamObj();

String xml = xstream.toXML(feed);
System.out.println(xml);

}

public static XStream createXStreamObj() {

XStream xstream = new XStream();

// Class aliasing
xstream.alias("entry", Entry.class);
xstream.alias("feed", Feed.class);
xstream.alias("link", Link.class);

// Omit the 'entries' tag: implicit collection:
// whenever you have a collection which doesn't need to display it's
// root tag, you can map it as an implicit collection.
xstream.addImplicitCollection(Feed.class, "links");
xstream.addImplicitCollection(Feed.class, "entries");
xstream.addImplicitCollection(Entry.class, "links");

// Attribute aliasing
xstream.useAttributeFor(Feed.class, "links");

xstream.useAttributeFor(Entry.class, "links");

xstream.useAttributeFor(Link.class, "href");
xstream.useAttributeFor(Link.class, "rel");
xstream.useAttributeFor(Link.class, "type");
xstream.useAttributeFor(Link.class, "title");
return xstream;

}

public static Feed createFeed() {
XStream xstream = createXStreamObj();

String xml = readFileAsString();
System.out.println(xml);

Feed feed = (Feed) xstream.fromXML(xml);

return feed;
}

/**
* <at> param args
*/
public static void main(String[] args) {

// Serializing an object to XML
// createXML();

// Deserializing an object back from XM
Feed feed = createFeed();
System.out.println("Feed.title=" + feed.getTitle() + "\n");

ArrayList<Link> links = feed.getLinks();
if (links == null) {
System.out.println("links null");
}
ArrayList<Entry> entries = feed.getEntries();
if (entries == null) {
System.out.println("entries null");
}
System.out.println("size of entries " + entries.size());
// using traditional for loop
for (int i = 0; i < entries.size(); i++) {
System.out.println(entries.get(i).getTitle());
}


}

private static String readFileAsString() {

// String filepath =
// "C:\\Tmp\\Rational\\Workspace\\rpt\\RQM\\DownLoad_XML_testcase.xml";
String filepath = "C:\\Tmp\\Rational\\Workspace\\rpt\\RQM\\feed.xml";
try {
byte[] buffer = new byte[(int) new File(filepath).length()];
BufferedInputStream f = null;
try {
f = new BufferedInputStream(new FileInputStream(filepath));
f.read(buffer);
} finally {
if (f != null)
try {
f.close();
} catch (IOException ignored) {
}
}
return new String(buffer);
} catch (java.io.IOException e) {
System.out.println("Error in reading file");
return null;
}
}
}

Jörg Schaible | 2 May 08:27 2011

Re: Deserialize the serialized XML, error in ArrayList - Code

Hi Jirong,

Jirong Hu wrote:

> import java.util.ArrayList;
> //import java.util.List;
> 
> public class Feed {
> 
> private String title;
> private String id;
> private ArrayList<Link> links = new ArrayList<Link>();
> private ArrayList<Entry> entries = new ArrayList<Entry>();

[snip]

> XStream xstream = new XStream();
> 
> // Class aliasing
> xstream.alias("entry", Entry.class);
> xstream.alias("feed", Feed.class);
> xstream.alias("link", Link.class);
> 
> // Omit the 'entries' tag: implicit collection:
> // whenever you have a collection which doesn't need to display it's
> // root tag, you can map it as an implicit collection.
> xstream.addImplicitCollection(Feed.class, "links");
> xstream.addImplicitCollection(Feed.class, "entries");

You declare two implicit collections in the same class, but give XStream no 
hint, which element has to be in which class. At runtime those two lists are 
not distinguishable. See also last thread here on the list.

> xstream.addImplicitCollection(Entry.class, "links");
>
> 
> // Attribute aliasing
> xstream.useAttributeFor(Feed.class, "links");
> xstream.useAttributeFor(Entry.class, "links");

What are this two definitions for?

> xstream.useAttributeFor(Link.class, "href");
> xstream.useAttributeFor(Link.class, "rel");
> xstream.useAttributeFor(Link.class, "type");
> xstream.useAttributeFor(Link.class, "title");
> return xstream;

[snip]

- Jörg

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jirong Hu | 2 May 14:51 2011
Picon

Re: Re: Deserialize the serialized XML, error in ArrayList - Code

Sorry, I forgot to give you the xml file. See the sample below:
1. This is a feed for test cases. Every entry is a test case. 
2. link is in both feed and entry. Actually I may not need the link info.
3. I want to write test case info into a csv file.

It works fine from class to xml, but not the other way.

Thanks again for your help.

Jirong

<feed>
  <title>title</title>
  <id>id</id>
  <link href="https://127.0.0.1:9444/jazz/secure/web/console/" rel="alternate"/>
  <entry>
    <title>Add New Customer</title>
    <summary>summary1</summary>
    <updated>updated1</updated>
    <id>AddNewCustomer_TC.xml</id>
    <link href="https://localhost:9444/jazz/secure/AddNewCustomer_TC" rel="alternate" type="application/xml"/>
    <link href="https://127.0.0.1:9444/jazz/web/console....1" rel="enclosure" type="text/html" title="Web Console"/>
  </entry>
  <entry>
    <title>Add New Product</title>
    <summary>summary2</summary>
    <updated>updated2</updated>
    <id>AddNewProduct_TC.xml</id>
    <link href="https://localhost:9444/jazz/secure/AddNewProduct_TC" rel="alternate" type="application/xml"/>
    <link href="https://127.0.0.1:9444/jazz/web/console....2" rel="enclosure" type="text/html" title="Web Console"/>
  </entry>
</feed>


On Mon, May 2, 2011 at 2:27 AM, Jörg Schaible <joerg.schaible-ysaFb9yBoMtWk0Htik3J/w@public.gmane.org> wrote:
Hi Jirong,

Jirong Hu wrote:

> import java.util.ArrayList;
> //import java.util.List;
>
> public class Feed {
>
> private String title;
> private String id;
> private ArrayList<Link> links = new ArrayList<Link>();
> private ArrayList<Entry> entries = new ArrayList<Entry>();

[snip]

> XStream xstream = new XStream();
>
> // Class aliasing
> xstream.alias("entry", Entry.class);
> xstream.alias("feed", Feed.class);
> xstream.alias("link", Link.class);
>
> // Omit the 'entries' tag: implicit collection:
> // whenever you have a collection which doesn't need to display it's
> // root tag, you can map it as an implicit collection.
> xstream.addImplicitCollection(Feed.class, "links");
> xstream.addImplicitCollection(Feed.class, "entries");

You declare two implicit collections in the same class, but give XStream no
hint, which element has to be in which class. At runtime those two lists are
not distinguishable. See also last thread here on the list.


> xstream.addImplicitCollection(Entry.class, "links");
>
>
> // Attribute aliasing
> xstream.useAttributeFor(Feed.class, "links");
> xstream.useAttributeFor(Entry.class, "links");

What are this two definitions for?


> xstream.useAttributeFor(Link.class, "href");
> xstream.useAttributeFor(Link.class, "rel");
> xstream.useAttributeFor(Link.class, "type");
> xstream.useAttributeFor(Link.class, "title");
> return xstream;

[snip]

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



Jörg Schaible | 2 May 17:16 2011

Re: Re: Deserialize the serialized XML, error in ArrayList - Code

I already told you what's wrong. Cannot say anything else.

Jirong Hu wrote:

> Sorry, I forgot to give you the xml file. See the sample below:
> 1. This is a feed for test cases. Every entry is a test case.
> 2. link is in both feed and entry. Actually I may not need the link info.
> 3. I want to write test case info into a csv file.
> 
> It works fine from class to xml, but not the other way.
> 
> Thanks again for your help.
> 
> Jirong
> 
> <feed>
>   <title>title</title>
>   <id>id</id>
>   <link href="https://127.0.0.1:9444/jazz/secure/web/console/"
> rel="alternate"/>
>   <link href="https://localhost:9444/jazz/secure/service/tcase/?"
> rel="self"/>
>   <entry>
>     <title>Add New Customer</title>
>     <summary>summary1</summary>
>     <updated>updated1</updated>
>     <id>AddNewCustomer_TC.xml</id>
>     <link href="https://localhost:9444/jazz/secure/AddNewCustomer_TC"
> rel="alternate" type="application/xml"/>
>     <link href="https://127.0.0.1:9444/jazz/web/console....1"
> rel="enclosure" type="text/html" title="Web Console"/>
>   </entry>
>   <entry>
>     <title>Add New Product</title>
>     <summary>summary2</summary>
>     <updated>updated2</updated>
>     <id>AddNewProduct_TC.xml</id>
>     <link href="https://localhost:9444/jazz/secure/AddNewProduct_TC"
> rel="alternate" type="application/xml"/>
>     <link href="https://127.0.0.1:9444/jazz/web/console....2"
> rel="enclosure" type="text/html" title="Web Console"/>
>   </entry>
> </feed>
> 
> 
> On Mon, May 2, 2011 at 2:27 AM, Jörg Schaible
> <joerg.schaible@...>wrote:
> 
>> Hi Jirong,
>>
>> Jirong Hu wrote:
>>
>> > import java.util.ArrayList;
>> > //import java.util.List;
>> >
>> > public class Feed {
>> >
>> > private String title;
>> > private String id;
>> > private ArrayList<Link> links = new ArrayList<Link>();
>> > private ArrayList<Entry> entries = new ArrayList<Entry>();
>>
>> [snip]
>>
>> > XStream xstream = new XStream();
>> >
>> > // Class aliasing
>> > xstream.alias("entry", Entry.class);
>> > xstream.alias("feed", Feed.class);
>> > xstream.alias("link", Link.class);
>> >
>> > // Omit the 'entries' tag: implicit collection:
>> > // whenever you have a collection which doesn't need to display it's
>> > // root tag, you can map it as an implicit collection.
>> > xstream.addImplicitCollection(Feed.class, "links");
>> > xstream.addImplicitCollection(Feed.class, "entries");
>>
>> You declare two implicit collections in the same class, but give XStream
>> no hint, which element has to be in which class. At runtime those two
>> lists are
>> not distinguishable. See also last thread here on the list.
>>
>>
>> > xstream.addImplicitCollection(Entry.class, "links");
>> >
>> >
>> > // Attribute aliasing
>> > xstream.useAttributeFor(Feed.class, "links");
>> > xstream.useAttributeFor(Entry.class, "links");
>>
>> What are this two definitions for?
>>
>>
>> > xstream.useAttributeFor(Link.class, "href");
>> > xstream.useAttributeFor(Link.class, "rel");
>> > xstream.useAttributeFor(Link.class, "type");
>> > xstream.useAttributeFor(Link.class, "title");
>> > return xstream;
>>
>> [snip]
>>
>> - Jörg
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jirong Hu | 2 May 17:31 2011
Picon

Re: Re: Re: Deserialize the serialized XML, error in ArrayList - Code

Sorry, I haven't worked in Java for a long time and this is the first time use XStream and I am not familiar with XML too. But I have to do this for now. Would you mind fixing that for me? I really don't understand what you mean.


Thanks again.
Jirong

On Mon, May 2, 2011 at 11:16 AM, Jörg Schaible <joerg.schaible <at> scalaris.com> wrote:
I already told you what's wrong. Cannot say anything else.

Jirong Hu wrote:

> Sorry, I forgot to give you the xml file. See the sample below:
> 1. This is a feed for test cases. Every entry is a test case.
> 2. link is in both feed and entry. Actually I may not need the link info.
> 3. I want to write test case info into a csv file.
>
> It works fine from class to xml, but not the other way.
>
> Thanks again for your help.
>
> Jirong
>
> <feed>
>   <title>title</title>
>   <id>id</id>
>   <link href="https://127.0.0.1:9444/jazz/secure/web/console/"
> rel="alternate"/>
>   <link href="https://localhost:9444/jazz/secure/service/tcase/?"
> rel="self"/>
>   <entry>
>     <title>Add New Customer</title>
>     <summary>summary1</summary>
>     <updated>updated1</updated>
>     <id>AddNewCustomer_TC.xml</id>
>     <link href="https://localhost:9444/jazz/secure/AddNewCustomer_TC"
> rel="alternate" type="application/xml"/>
>     <link href="https://127.0.0.1:9444/jazz/web/console....1"
> rel="enclosure" type="text/html" title="Web Console"/>
>   </entry>
>   <entry>
>     <title>Add New Product</title>
>     <summary>summary2</summary>
>     <updated>updated2</updated>
>     <id>AddNewProduct_TC.xml</id>
>     <link href="https://localhost:9444/jazz/secure/AddNewProduct_TC"
> rel="alternate" type="application/xml"/>
>     <link href="https://127.0.0.1:9444/jazz/web/console....2"
> rel="enclosure" type="text/html" title="Web Console"/>
>   </entry>
> </feed>
>
>
> On Mon, May 2, 2011 at 2:27 AM, Jörg Schaible
> <joerg.schaible <at> scalaris.com>wrote:
>
>> Hi Jirong,
>>
>> Jirong Hu wrote:
>>
>> > import java.util.ArrayList;
>> > //import java.util.List;
>> >
>> > public class Feed {
>> >
>> > private String title;
>> > private String id;
>> > private ArrayList<Link> links = new ArrayList<Link>();
>> > private ArrayList<Entry> entries = new ArrayList<Entry>();
>>
>> [snip]
>>
>> > XStream xstream = new XStream();
>> >
>> > // Class aliasing
>> > xstream.alias("entry", Entry.class);
>> > xstream.alias("feed", Feed.class);
>> > xstream.alias("link", Link.class);
>> >
>> > // Omit the 'entries' tag: implicit collection:
>> > // whenever you have a collection which doesn't need to display it's
>> > // root tag, you can map it as an implicit collection.
>> > xstream.addImplicitCollection(Feed.class, "links");
>> > xstream.addImplicitCollection(Feed.class, "entries");
>>
>> You declare two implicit collections in the same class, but give XStream
>> no hint, which element has to be in which class. At runtime those two
>> lists are
>> not distinguishable. See also last thread here on the list.
>>
>>
>> > xstream.addImplicitCollection(Entry.class, "links");
>> >
>> >
>> > // Attribute aliasing
>> > xstream.useAttributeFor(Feed.class, "links");
>> > xstream.useAttributeFor(Entry.class, "links");
>>
>> What are this two definitions for?
>>
>>
>> > xstream.useAttributeFor(Link.class, "href");
>> > xstream.useAttributeFor(Link.class, "rel");
>> > xstream.useAttributeFor(Link.class, "type");
>> > xstream.useAttributeFor(Link.class, "title");
>> > return xstream;
>>
>> [snip]
>>
>> - Jörg
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>    http://xircles.codehaus.org/manage_email
>>
>>
>>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email



김의진 | 3 May 01:39 2011
Picon

<String> Message Convert to <UserText>


Hi..

 - Xml message
<service>                    
  <api>                      
    <name>ABC</name>         
    <value>12</value>        
    <option>address1</option>
  </api>                     
  <api>                      
    <name>XYZ</name>         
    <value>20</value>        
    <option>address2</option>
  </api>                     
  <api>                      
    <name>XYZ</name>         
    <value>30</value>        
    <option>address3</option>
    <optvalues>              
      <string>qwe</string>   
      <string>asd</string>   
      <string>zxc</string>   
    </optvalues>             
  </api>                     
</service>  


How to make, That <String> message in xml Code convert to <UserText>

 <optvalues>              
      <string>qwe</string>   
      <string>asd</string>   
      <string>zxc</string>   
    </optvalues> 

        ---------------------------------------------

 <optvalues>              
      <UserText>qwe</UserText>   
      <UserText>asd</UserText>   
      <UserText>zxc</UserText>   
    </optvalues> 

Jörg Schaible | 3 May 14:25 2011

Re: Re: Re: Deserialize the serialized XML, error in ArrayList - Code

Hi Jirong,

Jirong Hu wrote:

> Sorry, I haven't worked in Java for a long time and this is the first time
> use XStream and I am not familiar with XML too. But I have to do this for
> now. Would you mind fixing that for me? I really don't understand what you
> mean.

as said, the last user question on this list just a day before you asked, 
had exactly the same problem: 
http://thread.gmane.org/gmane.comp.java.xstream.user/7550

He was able to solve it with my explanation and posted also the solution.

- Jörg

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

Jörg Schaible | 3 May 14:28 2011

Re: <String> Message Convert to <UserText>

Hi 김의진,

김의진 wrote:

> Hi..
> 
>  - Xml message
> <service>
>   <api>
>     <name>ABC</name>
>     <value>12</value>
>     <option>address1</option>
>   </api>
>   <api>
>     <name>XYZ</name>
>     <value>20</value>
>     <option>address2</option>
>   </api>
>   <api>
>     <name>XYZ</name>
>     <value>30</value>
>     <option>address3</option>
>     <optvalues>
>       <string>qwe</string>
>       <string>asd</string>
>       <string>zxc</string>
>     </optvalues>
>   </api>
> </service>
> 
> 
> How to make, That <String> message in xml Code convert to <UserText>
> 
>  <optvalues>
>       <string>qwe</string>
>       <string>asd</string>
>       <string>zxc</string>
>     </optvalues>
> 
>         ---------------------------------------------
> 
>  <optvalues>
>       <UserText>qwe</UserText>
>       <UserText>asd</UserText>
>       <UserText>zxc</UserText>
>     </optvalues>

If you have nowhere else a string tag, you may simply define an alias for 
String.class to UserText.

- Jörg

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane