Jump to content

Archived

This topic is now archived and is closed to further replies.

Heur

xStream - Duas classes com nomes iguais, mas tipos diferentes

Recommended Posts

Olá pessoal.

Estou com um problemão, na leitura de um xml.

 

Eu peguei um xml, que não foi feito por mim. E tenho que ler, editar e escreve um xml

igual a esse.

 

O xml é bastante extenso e no meio do código um empasse...

 

há uma estrutura assim:

 

<activities>

 <activity>
    <id>1</id>
    <name>Activity 1</name>
 </activity>
 <activity>
    <id>1</id>
    <name>Activity 1</name>
    <time>4</time>
 </activity>

 <activity>
    <id>2</id>
    <name>Activity 2</name>
 </activity>
 <activity>
    <id>2</id>
    <name>Activity 2</name>
    <time>5</time>
 </activity>

 <activity>
    <id>3</id>
    <name>Activity 3</name>
 </activity>
 <activity>
    <id>3</id>
    <name>Activity 3</name>
    <time>6</time>
 </activity>

</activities>

 

Explicando. Eu tenho tags <activity> com dados diferentes.

E no xstream, eu to tentando colocar essas tags dentro de uma classe Activity.

 

Por exemplo:

public class Activity {

  private int id;
  private String name;
  private int time;

}

 

Eu fiz os getter e setters para essa classe. Dessa forma, eu devo colocar, para ler a tag <activities> dentro

de uma array, assim:

 

public class Activities {

   private List<Activity> activities = new ArrayList<Activity>();

   public void add(Activity atv) {
       this.activities.add(atv);
   }

   public List<Activity> getContent() {
       return this.activities;
   }
}

 

Porém, como vou fazer para inserir nessa array o outro tipo de tag?

 

As tags tem nomes iguais mais tipos de dados diferentes, ou à mais.

 

Alguém pode me ajudar?

 

Obrigado.

Share this post


Link to post
Share on other sites

então temos:

 

public class Activity {
private int id;
private String name;
private int time;

public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getTime() {
	return time;
}

public void setTime(int time) {
	this.time = time;
}
}

 

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

public class Activities {
private List<Activity> activities = new ArrayList<Activity>();

public List<Activity> getActivities() {
	return activities;
}

public void setActivities(List<Activity> activities) {
	this.activities = activities;
}
}

 

você precisa fazer o seguinte...

 

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("activity")
public class Activity {
private int id;
private String name;
private int time;

public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getTime() {
	return time;
}

public void setTime(int time) {
	this.time = time;
}
}

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

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias("activities")
public class Activities {

@XStreamImplicit
private List<Activity> activities = new ArrayList<Activity>();

public List<Activity> getActivities() {
	return activities;
}

public void setActivities(List<Activity> activities) {
	this.activities = activities;
}
}

 

executando isso:

import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;

public class Test {
public static void main(String[] args) throws Exception {
	XStream xs = new XStream();
	xs.processAnnotations(Activities.class);
	xs.processAnnotations(Activity.class);

	Activity activity = new Activity();
	activity.setId(1);
	activity.setName("Activity 1");
	activity.setTime(4);

	Activity activity2 = new Activity();
	activity2.setId(2);
	activity2.setName("Activity 2");

	Activity activity3 = new Activity();
	activity3.setId(3);
	activity3.setName("Activity 3");
	activity3.setTime(6);

	Activity activity4 = new Activity();
	activity4.setId(4);
	activity4.setName("Activity 4");

	Activities activities = new Activities();
	activities.setActivities(new ArrayList<Activity>());
	activities.getActivities().add(activity);
	activities.getActivities().add(activity2);
	activities.getActivities().add(activity3);
	activities.getActivities().add(activity4);

	System.out.println(xs.toXML(activities));
}
}

 

o resultado é:

<activities>
 <activity>
   <id>1</id>
   <name>Activity 1</name>
   <time>4</time>
 </activity>
 <activity>
   <id>2</id>
   <name>Activity 2</name>
   <time>0</time>
 </activity>
 <activity>
   <id>3</id>
   <name>Activity 3</name>
   <time>6</time>
 </activity>
 <activity>
   <id>4</id>
   <name>Activity 4</name>
   <time>0</time>
 </activity>
</activities>

 

é claro que o time vai aparecer zero pq o tipo é int se fosse integer quando não setado seria null ai o xstream omite!

Share this post


Link to post
Share on other sites

×

Important Information

Ao usar o fórum, você concorda com nossos Terms of Use.