Skip to content

Commit

Permalink
Generate XML
Browse files Browse the repository at this point in the history
  • Loading branch information
cj96248 committed Sep 20, 2018
1 parent 27dd865 commit 03c2fde
Show file tree
Hide file tree
Showing 7 changed files with 276 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/example/demo/lesson10/Five.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.demo.lesson10;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Five {

private String name;
private Four four;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Four getFour() {
return four;
}
public void setFour(Four four) {
this.four = four;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/example/demo/lesson10/Four.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.demo.lesson10;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Four {
private String id;
private String name;
public String getId() {
return id;
}
@XmlAttribute
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
49 changes: 49 additions & 0 deletions src/main/java/com/example/demo/lesson10/Seven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.demo.lesson10;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement
@XmlType(propOrder = {"code", "name", "age", "desc"})
public class Seven {

private String code;
private String name;
private String desc;
private int age;
private double slary;

public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSlary() {
return slary;
}
@XmlTransient
public void setSlary(double slary) {
this.slary = slary;
}

}
45 changes: 45 additions & 0 deletions src/main/java/com/example/demo/lesson10/Six.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.demo.lesson10;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Six")
public class Six {

private String code;
private String name;
private String desc;

public Six() {}

public Six(String code, String name, String desc) {
super();
this.code = code;
this.name = name;
this.desc = desc;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/example/demo/lesson10/Three.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.demo.lesson10;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Three {
private String name;
public String getName() {
return name;
}
@XmlElement(name = "Naming")
public void setName(String name) {
this.name = name;
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/example/demo/lesson10/Two.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.demo.lesson10;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "Second")
public class Two {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Two [name=" + name + "]";
}

}
98 changes: 98 additions & 0 deletions src/test/java/com/example/demo/lesson10/GenerateXML.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.example.demo.lesson10;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.junit.Ignore;
import org.junit.Test;

import com.example.demo.lesson10.Two;

public class GenerateXML {

@Test
@Ignore
public void test() throws JAXBException {
// 首先创建 JAXBContext
JAXBContext context = JAXBContext.newInstance(Two.class);
// 初始化 Marshaller
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// 创建简单的 Java bean
Two o = new Two();
o.setName("Test two");
// 将结果输出到控制台
marshaller.marshal(o, System.out);
}

@Test
@Ignore
public void test3() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Three.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Three t = new Three();
t.setName("Test three");
marshaller.marshal(t, System.out);
}

@Test
@Ignore
public void test4() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Four.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Four f = new Four();
f.setId("1004");
f.setName("Test 4");
marshaller.marshal(f, System.out);
}

@Test
@Ignore
public void test5() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Five.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Four four = new Four();
four.setName("This is 4");

Five five = new Five();
five.setName("Test 5");
five.setFour(four);

marshaller.marshal(five, System.out);
}

@Test
@Ignore
public void test6() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Six.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Six six = new Six("1006", "Test6", "Some descrptions");

marshaller.marshal(six, System.out);
}

@Test
@Ignore
public void test7() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(Seven.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Seven s = new Seven();
s.setCode("1007");
s.setName("Test name");
s.setAge(22);
s.setDesc("The desc");
s.setSlary(21.45);

marshaller.marshal(s, System.out);
}

}

0 comments on commit 03c2fde

Please sign in to comment.