jackson-dataformat-xml工具包xml与java对象转换说明
1、引入依赖包
<dependency><groupId>com.fasterxml.jackson.dataformat</groupId><artifactId>jackson-dataformat-xml</artifactId><version>2.10.2</version></dependency>
2、测试使用的对象
importcom.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;importcom.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;importjava.util.List;/**
* 测试对象School,包含普通属性name:名称 列表属性grades:多个Grade对象集合
*/publicclassSchool{@JacksonXmlProperty(localName ="NAME")privateString name;@JacksonXmlElementWrapper(localName ="Grades")@JacksonXmlProperty(localName ="Grade")privateList<Grade> grades;publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicList<Grade>getGrades(){return grades;}publicvoidsetGrades(List<Grade> grades){this.grades = grades;}}
importcom.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;importcom.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;importjava.util.List;/**
* 测试对象Grade,包含普通属性name:名称 列表属性Classes:多个Classes对象集合
*/publicclassGrade{@JacksonXmlProperty(localName ="NAME")privateString name;@JacksonXmlElementWrapper(localName ="ClassList")@JacksonXmlProperty(localName ="Class")privateList<Classes> classes;publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicList<Classes>getClasses(){return classes;}publicvoidsetClasses(List<Classes> classes){this.classes = classes;}}
/**
* 测试对象Classes,包含普通属性name:名称 属性studentCount:数量
*/publicclassClasses{privateString name;privateInteger studentCount;publicStringgetName(){return name;}publicvoidsetName(String name){this.name = name;}publicIntegergetStudentCount(){return studentCount;}publicvoidsetStudentCount(Integer studentCount){this.studentCount = studentCount;}}
3、单元测试和效果
importcom.fasterxml.jackson.dataformat.xml.XmlMapper;importorg.dom4j.Document;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;importorg.junit.Test;importjava.io.IOException;importjava.io.StringReader;importjava.io.StringWriter;importjava.util.ArrayList;importjava.util.List;publicclassObjectToXml{//创建XmlMapper对象,用于实体与json和xml之间的相互转换privatestaticfinalXmlMapper xmlMapper =newXmlMapper();@Testpublicvoidt1()throwsException{School school =newSchool();
school.setName("学校名称");Grade grade1 =newGrade();
grade1.setName("年级1");Grade grade2 =newGrade();
grade2.setName("年级2");Grade grade3 =newGrade();
grade3.setName("年级3");Grade grade4 =newGrade();
grade4.setName("年级4");Classes classes1 =newClasses();
classes1.setName("年级1-班级1");
classes1.setStudentCount(60);Classes classes2 =newClasses();
classes2.setName("年级1-班级2");
classes2.setStudentCount(50);Classes classes3 =newClasses();
classes3.setName("年级1-班级3");
classes3.setStudentCount(52);List<Classes> classesList1 =newArrayList<>();
classesList1.add(classes1);
classesList1.add(classes2);
classesList1.add(classes3);
grade1.setClasses(classesList1);Classes classes4 =newClasses();
classes4.setName("年级2-班级1");
classes4.setStudentCount(45);Classes classes5 =newClasses();
classes5.setName("年级2-班级2");
classes5.setStudentCount(46);Classes classes6 =newClasses();
classes6.setName("年级2-班级3");
classes6.setStudentCount(42);List<Classes> classesList2 =newArrayList<>();
classesList2.add(classes4);
classesList2.add(classes5);
classesList2.add(classes6);
grade2.setClasses(classesList2);List<Grade> grades =newArrayList<>();
grades.add(grade1);
grades.add(grade2);
grades.add(grade3);
grades.add(grade4);
school.setGrades(grades);String xmlStr = xmlMapper.writeValueAsString(school);
xmlStr =formatXML(xmlStr);System.out.println(xmlStr);}/**
* 格式化xml
* @param inputXML
* @return
* @throws Exception
*/publicStringformatXML(String inputXML)throwsException{SAXReader reader =newSAXReader();Document document = reader.read(newStringReader(inputXML));String requestXML =null;XMLWriter writer =null;if(document !=null){try{StringWriter stringWriter =newStringWriter();OutputFormat format =newOutputFormat(" ",true);
format.setTrimText(true);
format.setExpandEmptyElements(true);
writer =newXMLWriter(stringWriter, format);
writer.write(document);
writer.flush();
requestXML = stringWriter.getBuffer().toString();}finally{if(writer !=null){try{
writer.close();}catch(IOException e){}}}}return requestXML;}}
输出效果:
<?xml version="1.0" encoding="UTF-8"?><School><NAME>学校名称</NAME><Grades><Grade><NAME>年级1</NAME><ClassList><Class><name>年级1-班级1</name><studentCount>60</studentCount></Class><Class><name>年级1-班级2</name><studentCount>50</studentCount></Class><Class><name>年级1-班级3</name><studentCount>52</studentCount></Class></ClassList></Grade><Grade><NAME>年级2</NAME><ClassList><Class><name>年级2-班级1</name><studentCount>45</studentCount></Class><Class><name>年级2-班级2</name><studentCount>46</studentCount></Class><Class><name>年级2-班级3</name><studentCount>42</studentCount></Class></ClassList></Grade><Grade><NAME>年级3</NAME></Grade><Grade><NAME>年级4</NAME></Grade></Grades></School>
4、注解说明
1、@JacksonXmlProperty
注解是用于对象属性,源代码如下,
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface JacksonXmlProperty { boolean isAttribute() default false; String namespace() default ""; String localName() default ""; }
包含三个可设置属性,
localName:表明在当前转换的xml串中,次属性的标签名称,如上面转换出来的 学校名称标签,
namespace:设置命名空间,假设给school对象的name属性设置命名空间:@JacksonXmlProperty(localName = “NAME”,namespace = “xxx”)
输出效果如下:
<wstxns1:NAME xmlns:wstxns1=“xxx”>学校名称</wstxns1:NAME>
isAttribute:设置该属性是否为标签内属性,默认为false
设置示例:把School对象的那么设置为标签内属性:
@JacksonXmlProperty(localName = “NAME”,isAttribute = true)
效果:NAME属性不在独立成为标签,而是成为School标签的内属性
2、@JacksonXmlElementWrapper
作用于对象的集合属性上,@JacksonXmlElementWrapper(localName = “ClassList”),能够让集合属性决定是否使用包装元素,并且也可以控制包装元素的本地名称和命名空间。
<ClassList><Class><name>年级2-班级1</name><studentCount>45</studentCount></Class><Class><name>年级2-班级2</name><studentCount>46</studentCount></Class><Class><name>年级2-班级3</name><studentCount>42</studentCount></Class></ClassList>
对象转xml字符串以上注解基本可以实现所需功能,其中转换出来的字符串是连接的,需要格式化一下方便查看结构
5、xml串转换对象
@Testpublicvoidt2()throwsJsonProcessingException{String xmlStr ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+"\n"+"<School>\n"+" <NAME>学校名称</NAME>\n"+" <Grades>\n"+" <Grade>\n"+" <NAME>年级1</NAME>\n"+" <ClassList>\n"+" <Class>\n"+" <name>年级1-班级1</name>\n"+" <studentCount>60</studentCount>\n"+" </Class>\n"+" <Class>\n"+" <name>年级1-班级2</name>\n"+" <studentCount>50</studentCount>\n"+" </Class>\n"+" <Class>\n"+" <name>年级1-班级3</name>\n"+" <studentCount>52</studentCount>\n"+" </Class>\n"+" </ClassList>\n"+" </Grade>\n"+" <Grade>\n"+" <NAME>年级2</NAME>\n"+" <ClassList>\n"+" <Class>\n"+" <name>年级2-班级1</name>\n"+" <studentCount>45</studentCount>\n"+" </Class>\n"+" <Class>\n"+" <name>年级2-班级2</name>\n"+" <studentCount>46</studentCount>\n"+" </Class>\n"+" <Class>\n"+" <name>年级2-班级3</name>\n"+" <studentCount>42</studentCount>\n"+" </Class>\n"+" </ClassList>\n"+" </Grade>\n"+" <Grade>\n"+" <NAME>年级3</NAME>\n"+" </Grade>\n"+" <Grade>\n"+" <NAME>年级4</NAME>\n"+" </Grade>\n"+" </Grades>\n"+"</School>";School school = xmlMapper.readValue(xmlStr,School.class);System.out.println(school.getName());}
版权归原作者 年轻码农 所有, 如有侵权,请联系我们删除。