用XStream解析xml成对象
XStreamstream=newXStream();stream.alias("modules",Modules.class);reader=newFileReader...
XStream stream = new XStream();stream.alias("modules", Modules.class);reader = new FileReader("D:/modules.xml");Modules object = (Modules)stream.fromXML(reader);
其中modules.xml格式为:
<?xml version="1.0" encoding="UTF-8"?>
<modules> <id>00000001</id> <id>00005A01</id> <id>00005A02</id> </modules>
======================================
id可以看作String,这个Modules应该怎么设计呢? 展开
其中modules.xml格式为:
<?xml version="1.0" encoding="UTF-8"?>
<modules> <id>00000001</id> <id>00005A01</id> <id>00005A02</id> </modules>
======================================
id可以看作String,这个Modules应该怎么设计呢? 展开
1个回答
展开全部
邮件是多少,我给一个实例给你
算了,先帮你解决这个吧
import java.io.FileReader;import java.util.List;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamImplicit;import com.thoughtworks.xstream.io.xml.DomDriver;public class Xstream { public static void main(String[] args){ Modules mo=Modules.getResponse(); //将xml转成对象 System.out.println(mo.getId().get(0)); //将对象转成xml System.out.println(Modules.getResponse(mo)); } @XStreamAlias("modules") public static class Modules{ @XStreamImplicit(itemFieldName="id") private List<String> id; public List<String> getId() { return id; } public void setId(List<String> id) { this.id = id; } @XStreamAlias("id") public static class Id{ @XStreamAlias("id") private String id1; public String getId1() { return id1; } public void setId1(String id1) { this.id1 = id1; } } //xml转对象 public static Modules getResponse() { try { XStream xstream = new XStream(new DomDriver()); xstream.processAnnotations(Modules.class); xstream.autodetectAnnotations(true); return (Modules) xstream.fromXML(new FileReader("I:/myworkSpace/byc-test-server/src/main/java/com/byc/test/test.xml"));// return (Modules) xstream.fromXML(xml); } catch (Exception e) { // TODO Auto-generated catch block //失败 e.printStackTrace(); return null; } } //对象转xml public static String getResponse(Modules rs) { try { XStream xstream = new XStream(); xstream.processAnnotations(Modules.class); xstream.autodetectAnnotations(true); return (String) xstream.toXML(rs); } catch (Exception e) { // TODO Auto-generated catch block //失败 e.printStackTrace(); return null; } } } }直接copy就可以,xml就是你个的示例xml
算了,先帮你解决这个吧
import java.io.FileReader;import java.util.List;import com.thoughtworks.xstream.XStream;import com.thoughtworks.xstream.annotations.XStreamAlias;import com.thoughtworks.xstream.annotations.XStreamImplicit;import com.thoughtworks.xstream.io.xml.DomDriver;public class Xstream { public static void main(String[] args){ Modules mo=Modules.getResponse(); //将xml转成对象 System.out.println(mo.getId().get(0)); //将对象转成xml System.out.println(Modules.getResponse(mo)); } @XStreamAlias("modules") public static class Modules{ @XStreamImplicit(itemFieldName="id") private List<String> id; public List<String> getId() { return id; } public void setId(List<String> id) { this.id = id; } @XStreamAlias("id") public static class Id{ @XStreamAlias("id") private String id1; public String getId1() { return id1; } public void setId1(String id1) { this.id1 = id1; } } //xml转对象 public static Modules getResponse() { try { XStream xstream = new XStream(new DomDriver()); xstream.processAnnotations(Modules.class); xstream.autodetectAnnotations(true); return (Modules) xstream.fromXML(new FileReader("I:/myworkSpace/byc-test-server/src/main/java/com/byc/test/test.xml"));// return (Modules) xstream.fromXML(xml); } catch (Exception e) { // TODO Auto-generated catch block //失败 e.printStackTrace(); return null; } } //对象转xml public static String getResponse(Modules rs) { try { XStream xstream = new XStream(); xstream.processAnnotations(Modules.class); xstream.autodetectAnnotations(true); return (String) xstream.toXML(rs); } catch (Exception e) { // TODO Auto-generated catch block //失败 e.printStackTrace(); return null; } } } }直接copy就可以,xml就是你个的示例xml
追问
00005A01
A01
00005A02
A02
前面确实对的,这种情况呢,只要写下Modules的 class就行
追答
@XStreamAlias("modules") public static class Modules{ @XStreamImplicit(itemFieldName="module") private List module;
public List getModule() { return module; }
public void setModule(List module) { this.module = module; }
@XStreamAlias("module") public static class Module{ @XStreamAlias("id") private String id; @XStreamAlias("name") private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } }
方法直接用,不用改
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询