xstream 小结 一个标签下有多个同名标签

 我来答
匿名用户
2016-05-23
展开全部
  方式一用加注解的方式
  xml文件中,有多个元素response,a,b,c,d,name
  5个name 中国,美国,俄罗斯,英国,法国
  
  1)bean.Java
  2)在list上加注解
  @XStreamImplicit(itemFieldName="searchContent")// @XStreamImplicit加注解,itemFieldName说明隐含的元素名
  3)在test.java中,在根节点转换之前调用
  stream.autodetectAnnotations(true);
  
  
  package bean;
  public class Testresponse {

private User user;
  public User getUser() {
return user;
}
  public void setUser(User user) {
this.user = user;
}
}
  
  package bean;
  import java.util.List;
  import com.thoughtworks.xstream.annotations.XStreamImplicit;

public class User {
@XStreamImplicit(itemFieldName="name")//标注加在list上
private List<String> name;
  public List<String> getName() {
return name;
}
  public void setName(List<String> name) {
this.name = name;
}
  }
  
  package bean;
  import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Set;
import com.thoughtworks.xstream.XStream;
  public class Test {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
Reader reader = null;
File file = new File(
"C://Users//ThinkPad//workspace8//xstreamtest//WebRoot//a.xml");
try {
reader = new FileReader(file);
  XStream xstream = new XStream();
  HashMap<String, Class> hashmap = new HashMap<String, Class>();
hashmap.put("user", User.class);
hashmap.put("reponse", Testresponse.class);
  Set<String> keyset = hashmap.keySet();
for (String str : keyset) {
xstream.alias(str, hashmap.get(str));
}
  xstream.autodetectAnnotations(true);
Testresponse response = (Testresponse) xstream.fromXML(reader);
System.out.println(response);
String s = xstream.toXML(response);
System.out.println(s);
  } catch (Exception e) {
e.printStackTrace();
}
  }
}
  
  
  <?xml version="1.0" encoding="UTF-8"?>
<reponse>
<user>
<name>jack1</name>
<name>jack2</name>
<name>jack3</name>
</user>
</reponse>

  
xiangjuan314
2016-05-23 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2889万
展开全部
有多个同名标签时怎么解析

方式一用加注解的方式

xml文件中,有多个元素response,a,b,c,d,name

5个name 中国,美国,俄罗斯,英国,法国

1)bean.java

2)在list上加注解

@XStreamImplicit(itemFieldName="searchContent")// @XStreamImplicit加注解,itemFieldName说明隐含的元素名

3)在test.java中,在根节点转换之前调用

stream.autodetectAnnotations(true);

package bean;

public class Testresponse {

private User user;

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
}

package bean;

import java.util.List;

import com.thoughtworks.xstream.annotations.XStreamImplicit;

public class User {
@XStreamImplicit(itemFieldName="name")//标注加在list上
private List<String> name;

public List<String> getName() {
return name;
}

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

}

package bean;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Set;
import com.thoughtworks.xstream.XStream;

public class Test {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
Reader reader = null;
File file = new File(
"C://Users//ThinkPad//workspace8//xstreamtest//WebRoot//a.xml");
try {
reader = new FileReader(file);

XStream xstream = new XStream();

HashMap<String, Class> hashmap = new HashMap<String, Class>();
hashmap.put("user", User.class);
hashmap.put("reponse", Testresponse.class);

Set<String> keyset = hashmap.keySet();
for (String str : keyset) {
xstream.alias(str, hashmap.get(str));
}

xstream.autodetectAnnotations(true);
Testresponse response = (Testresponse) xstream.fromXML(reader);
System.out.println(response);
String s = xstream.toXML(response);
System.out.println(s);

} catch (Exception e) {
e.printStackTrace();
}

}
}

<?xml version="1.0" encoding="UTF-8"?>
<reponse>
<user>
<name>jack1</name>
<name>jack2</name>
<name>jack3</name>
</user>
</reponse>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式