-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |