xml 格式化输出
我用XStream 转出的xml是这样
- <list>
- <Contacts>
- <id>1</id>
- <name>11</name>
- <email>111</email>
- <phone>1111</phone>
- </Contacts>
- <Contacts>
- <id>2</id>
- <name>22</name>
- <email>222</email>
- <phone>2222</phone>
- </Contacts>
- <Contacts>
- <id>3</id>
- <name>33</name>
- <email>333</email>
- <phone>3333</phone>
- </Contacts>
- </list>
但我不希望吧phone暴漏出去,希望的输出结果是
- <list>
- <Contacts>
- <id>1</id>
- <name>11</name>
- <email>111</email>
- </Contacts>
- <Contacts>
- <id>2</id>
- <name>22</name>
- <email>222</email>
- </Contacts>
- <Contacts>
- <id>3</id>
- <name>33</name>
- <email>333</email>
- </Contacts>
- </list>
javabean:
- publicclassEmployee{
- privateStringempNo;
- privateStringjob;
- @XStreamOmitField
- privateStringphone;
- publicStringgetEmpNo(){
- returnempNo;
- }
- publicvoidsetEmpNo(StringempNo){
- this.empNo=empNo;
- }
- publicStringgetJob(){
- returnjob;
- }
- publicvoidsetJob(Stringjob){
- this.job=job;
- }
- publicStringgetPhone(){
- returnphone;
- }
- publicvoidsetPhone(Stringphone){
- this.phone=phone;
- }
- }
- XStreamxstream=newXStream();
- Employeeemp=newEmployee();
- emp.setEmpNo("1");
- emp.setJob("bb");
- emp.setPhone("111111111");
- xstream.processAnnotations(Employee.class);
- Stringxml=xstream.toXML(emp);
- System.out.println(xml);