xml 格式化输出

前端之家收集整理的这篇文章主要介绍了xml 格式化输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

xml 格式化输出

我用XStream 转出的xml是这样

Xml代码
  1. <list>
  2. <Contacts>
  3. <id>1</id>
  4. <name>11</name>
  5. <email>111</email>
  6. <phone>1111</phone>
  7. </Contacts>
  8. <Contacts>
  9. <id>2</id>
  10. <name>22</name>
  11. <email>222</email>
  12. <phone>2222</phone>
  13. </Contacts>
  14. <Contacts>
  15. <id>3</id>
  16. <name>33</name>
  17. <email>333</email>
  18. <phone>3333</phone>
  19. </Contacts>
  20. </list>

但我不希望吧phone暴漏出去,希望的输出结果是
Xml代码
  1. <list>
  2. <Contacts>
  3. <id>1</id>
  4. <name>11</name>
  5. <email>111</email>
  6. </Contacts>
  7. <Contacts>
  8. <id>2</id>
  9. <name>22</name>
  10. <email>222</email>
  11. </Contacts>
  12. <Contacts>
  13. <id>3</id>
  14. <name>33</name>
  15. <email>333</email>
  16. </Contacts>
  17. </list>
可以通过注解(@XStreamOmitField)解决
javabean:
Java代码
  1. @H_404_646@public@H_404_646@classEmployee{
  2. @H_404_646@privateStringempNo;
  3. @H_404_646@privateStringjob;
  4. @XStreamOmitField
  5. @H_404_646@privateStringphone;
  6. @H_404_646@publicStringgetEmpNo(){
  7. @H_404_646@returnempNo;
  8. }
  9. @H_404_646@public@H_404_646@voidsetEmpNo(StringempNo){
  10. @H_404_646@this.empNo=empNo;
  11. }
  12. @H_404_646@publicStringgetJob(){
  13. @H_404_646@returnjob;
  14. }
  15. @H_404_646@public@H_404_646@voidsetJob(Stringjob){
  16. @H_404_646@this.job=job;
  17. }
  18. @H_404_646@publicStringgetPhone(){
  19. @H_404_646@returnphone;
  20. }
  21. @H_404_646@public@H_404_646@voidsetPhone(Stringphone){
  22. @H_404_646@this.phone=phone;
  23. }
  24. }

Java代码
  1. XStreamxstream=@H_404_646@newXStream();
  2. Employeeemp=@H_404_646@newEmployee();
  3. emp.setEmpNo("1");
  4. emp.setJob("bb");
  5. emp.setPhone("111111111");
  6. xstream.processAnnotations(Employee.@H_404_646@class);
  7. Stringxml=xstream.toXML(emp);
  8. System.out.println(xml);
原文链接:https://www.f2er.com/xml/298544.html

猜你在找的XML相关文章