xml與java物件轉換
阿新 • • 發佈:2022-12-07
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、測試使用的物件
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import java.util.List; /** * 測試物件School,包含普通屬性name:名稱 列表屬性grades:多個Grade物件集合 */ public class School { @JacksonXmlProperty(localName = "NAME") private String name; @JacksonXmlElementWrapper(localName = "Grades") @JacksonXmlProperty(localName = "Grade") private List<Grade> grades; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Grade> getGrades() { return grades; } public void setGrades(List<Grade> grades) { this.grades = grades; } }
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; import java.util.List; /** * 測試物件Grade,包含普通屬性name:名稱 列表屬性Classes:多個Classes物件集合 */ public class Grade { @JacksonXmlProperty(localName = "NAME") private String name; @JacksonXmlElementWrapper(localName = "ClassList") @JacksonXmlProperty(localName = "Class") private List<Classes> classes; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Classes> getClasses() { return classes; } public void setClasses(List<Classes> classes) { this.classes = classes; } }
/** * 測試物件Classes,包含普通屬性name:名稱 屬性studentCount:數量 */ public class Classes { private String name; private Integer studentCount; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getStudentCount() { return studentCount; } public void setStudentCount(Integer studentCount) { this.studentCount = studentCount; } }
3、單元測試和效果
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
public class ObjectToXml {
//建立XmlMapper物件,用於實體與json和xml之間的相互轉換
private static final XmlMapper xmlMapper = new XmlMapper();
@Test
public void t1() throws Exception {
School school = new School();
school.setName("學校名稱");
Grade grade1 = new Grade();
grade1.setName("年級1");
Grade grade2 = new Grade();
grade2.setName("年級2");
Grade grade3 = new Grade();
grade3.setName("年級3");
Grade grade4 = new Grade();
grade4.setName("年級4");
Classes classes1 = new Classes();
classes1.setName("年級1-班級1");
classes1.setStudentCount(60);
Classes classes2 = new Classes();
classes2.setName("年級1-班級2");
classes2.setStudentCount(50);
Classes classes3 = new Classes();
classes3.setName("年級1-班級3");
classes3.setStudentCount(52);
List<Classes> classesList1 = new ArrayList<>();
classesList1.add(classes1);
classesList1.add(classes2);
classesList1.add(classes3);
grade1.setClasses(classesList1);
Classes classes4 = new Classes();
classes4.setName("年級2-班級1");
classes4.setStudentCount(45);
Classes classes5 = new Classes();
classes5.setName("年級2-班級2");
classes5.setStudentCount(46);
Classes classes6 = new Classes();
classes6.setName("年級2-班級3");
classes6.setStudentCount(42);
List<Classes> classesList2 = new ArrayList<>();
classesList2.add(classes4);
classesList2.add(classes5);
classesList2.add(classes6);
grade2.setClasses(classesList2);
List<Grade> grades = new ArrayList<>();
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
*/
public String formatXML(String inputXML) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new StringReader(inputXML));
String requestXML = null;
XMLWriter writer = null;
if (document != null) {
try {
StringWriter stringWriter = new StringWriter();
OutputFormat format = new OutputFormat(" ", true);
format.setTrimText(true);
format.setExpandEmptyElements(true);
writer = new XMLWriter(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串轉換物件
@Test
public void t2() throws JsonProcessingException {
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());
}