标签:iso ota 格式 attr ati 代码 net ret field
1、引入XStream工具,maven配置如下:
1 <dependency> 2 <groupId>com.thoughtworks.xstream</groupId> 3 <artifactId>xstream</artifactId> 4 <version>1.4.10</version> 5 </dependency>
2、要解析的xml格式如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <mail-config> 3 <head> 4 <systemType>ccp</systemType> 5 </head> 6 <mails> 7 <mail> 8 <isOneTest>true</isOneTest> 9 <mialfrom>from11</mialfrom> 10 <mailto>to11</mailto> 11 <mailcc>cc11</mailcc> 12 <mailbcc>bcc11</mailbcc> 13 <mailBody id=‘zh‘> 14 <TestCode1>testcode1-zh</TestCode1> 15 <TestCode2>testcode2-zh</TestCode2> 16 <TestCode3>testcode3-zh</TestCode3> 17 </mailBody> 18 <mailBody id=‘en‘> 19 <TestCode1>testcode1-en</TestCode1> 20 <TestCode2>testcode2-en</TestCode2> 21 <TestCode3>testcode3-en</TestCode3> 22 </mailBody> 23 </mail> 24 </mails> 25 </mail-config>
3、根据xml的结构定义对应的Java对象。定义的时候使用到相关的注解@XStreamAlias、@XStreamAsAttribute、@XStreamImplicit(具体解析参考https://blog.csdn.net/Robert_mm/article/details/8459879)
根节点类如下:
1 import com.thoughtworks.xstream.annotations.XStreamAlias; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 @XStreamAlias("mail-config") 6 public class MailConfig { 7 private MailHead head; 8 private List<Mail> mails=new ArrayList<Mail>(); 9 10 public MailHead getHead() { 11 return head; 12 } 13 14 public void setHead(MailHead head) { 15 this.head = head; 16 } 17 18 public List<Mail> getMails() { 19 return mails; 20 } 21 22 public void setMails(List<Mail> mails) { 23 this.mails = mails; 24 } 25 }
第二层定义如下:
head
1 import com.thoughtworks.xstream.annotations.XStreamAlias; 2 3 import java.io.Serializable; 4 5 @XStreamAlias("head") 6 public class MailHead implements Serializable { 7 private String systemType; 8 private String systemCode; 9 10 public String getSystemType() { 11 return systemType; 12 } 13 14 public void setSystemType(String systemType) { 15 this.systemType = systemType; 16 } 17 18 public String getSystemCode() { 19 return systemCode; 20 } 21 22 public void setSystemCode(String systemCode) { 23 this.systemCode = systemCode; 24 } 25 }
mail类中注意注解@XStreamImplicit(itemFieldName="mailBody"),根据xml结构mailBody是没有根节点的,并排两个作为一个List,所以此处要使用这个注解才能正确解析成功。
1 import com.thoughtworks.xstream.annotations.XStreamAlias; 2 import com.thoughtworks.xstream.annotations.XStreamImplicit; 3 4 import java.io.Serializable; 5 import java.util.List; 6 7 @XStreamAlias("mail") 8 public class Mail implements Serializable { 9 private boolean isOneTest=false; 10 private String mialfrom; 11 private String mailto; 12 private String mailcc; 13 private String mailbcc; 14 15 @XStreamImplicit(itemFieldName="mailBody") 16 private List<MailBodyConfig> mailBodys; 17 18 public boolean isOneTest() { 19 return isOneTest; 20 } 21 22 public void setOneTest(boolean oneTest) { 23 isOneTest = oneTest; 24 } 25 26 public String getMialfrom() { 27 return mialfrom; 28 } 29 30 public void setMialfrom(String mialfrom) { 31 this.mialfrom = mialfrom; 32 } 33 34 public String getMailto() { 35 return mailto; 36 } 37 38 public void setMailto(String mailto) { 39 this.mailto = mailto; 40 } 41 42 public String getMailcc() { 43 return mailcc; 44 } 45 46 public void setMailcc(String mailcc) { 47 this.mailcc = mailcc; 48 } 49 50 public String getMailbcc() { 51 return mailbcc; 52 } 53 54 public void setMailbcc(String mailbcc) { 55 this.mailbcc = mailbcc; 56 } 57 58 public List<MailBodyConfig> getMailBodys() { 59 return mailBodys; 60 } 61 62 public void setMailBodys(List<MailBodyConfig> mailBodys) { 63 this.mailBodys = mailBodys; 64 } 65 }
import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; import java.io.Serializable; @XStreamAlias("mailBody") public class MailBodyConfig implements Serializable { @XStreamAsAttribute private String id; //xml中的节点名为:TestCode1,解决名称不对应问题 @XStreamAlias("TestCode1") private String testCode1; @XStreamAlias("TestCode2") private String testCode2; @XStreamAlias("TestCode3") private String testCode3; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTestCode1() { return testCode1; } public void setTestCode1(String testCode1) { this.testCode1 = testCode1; } public String getTestCode2() { return testCode2; } public void setTestCode2(String testCode2) { this.testCode2 = testCode2; } public String getTestCode3() { return testCode3; } public void setTestCode3(String testCode3) { this.testCode3 = testCode3; } }
具体调用测试代码如下:
1 public void TestXml() 2 { 3 String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 4 "<mail-config>\n" + 5 " <head>\n" + 6 " <systemType>ccp</systemType>\n" + 7 " </head>\n" + 8 " <mails>\n" + 9 " <mail>\n" + 10 " <isOneTest>true</isOneTest>\n" + 11 " <mialfrom>from11</mialfrom>\n" + 12 " <mailto>to11</mailto>\n" + 13 " <mailcc>cc11</mailcc>\n" + 14 " <mailbcc>bcc11</mailbcc>\n" + 15 " <mailBody id=‘zh‘>\n" + 16 " <TestCode1>testcode1zh</TestCode1>\n" + 17 " <TestCode2>testcode2zh</TestCode2>\n" + 18 " <TestCode3>testcode3zh</TestCode3>\n" + 19 " </mailBody>\n" + 20 " <mailBody id=‘en‘>\n" + 21 " <TestCode1>testcode1-en</TestCode1>\n" + 22 " <TestCode2>testcode2-en</TestCode2>\n" + 23 " <TestCode3>testcode3-en</TestCode3>\n" + 24 " </mailBody>\n" + 25 " </mail>\n" + 26 " </mails>\n" + 27 "</mail-config>"; 28 //设置了这个别名才能识别下面 xml 中的 类对象节点的别名,否则要用类全限名称 29 XStream xstream = new XStream(); 30 XStream.setupDefaultSecurity(xstream); 31 xstream.autodetectAnnotations(true); 32 xstream.alias("mail-config",MailConfig.class); 33 xstream.allowTypes(new Class[]{MailConfig.class, Mail.class}); 34 35 System.out.print(xml); 36 MailConfig mailconfig=(MailConfig)xstream.fromXML(xml); 37 38 System.out.print("转换成功!"); 39 }
标签:iso ota 格式 attr ati 代码 net ret field
原文地址:https://www.cnblogs.com/cc-9527/p/9005827.html