-
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
4 changed files
with
116 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,20 @@ | ||
package com.example.demo.lesson15; | ||
|
||
import javax.xml.bind.annotation.XmlAccessType; | ||
import javax.xml.bind.annotation.XmlAccessorType; | ||
import javax.xml.bind.annotation.XmlElement; | ||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement(name= "Employee") | ||
public class Employe2 { | ||
|
||
@XmlElement | ||
private String id; | ||
@XmlElement | ||
private String name; | ||
@Override | ||
public String toString() { | ||
return "Employee [id=" + id + ", 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,16 @@ | ||
package com.example.demo.lesson15; | ||
|
||
import javax.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement | ||
public class Note { | ||
|
||
private String to; | ||
private String from; | ||
private String heading; | ||
private String body; | ||
@Override | ||
public String toString() { | ||
return "Note [to=" + to + ", from=" + from + ", heading=" + heading + ", body=" + body + "]"; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/com/example/demo/lesson15/SerilalizerSimple.java
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,75 @@ | ||
package com.example.demo.lesson15; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringReader; | ||
import java.net.URI; | ||
import java.net.URL; | ||
|
||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Unmarshaller; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
public class SerilalizerSimple { | ||
|
||
@Test | ||
@Ignore | ||
public void test1() throws JAXBException { | ||
JAXBContext context = JAXBContext.newInstance(Employee.class); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
Employee employee = (Employee)unmarshaller.unmarshal(new File("./src/test/resources/lesson15.xml")); | ||
System.out.println(employee);//Employee [id=111, name=Test] | ||
} | ||
|
||
@Test | ||
@Ignore | ||
public void test11() throws JAXBException { | ||
JAXBContext context = JAXBContext.newInstance(Employee.class); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
Employee employee = (Employee)unmarshaller.unmarshal(SerilalizerSimple.class.getResourceAsStream("/lesson15.xml")); | ||
System.out.println(employee);//Employee [id=111, name=Test] | ||
} | ||
|
||
@Test | ||
public void test2() throws JAXBException, IOException { | ||
JAXBContext context = JAXBContext.newInstance(Employee.class); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
InputStream is = new FileInputStream("./src/test/resources/lesson15.xml"); | ||
Employee employee = (Employee)unmarshaller.unmarshal(is); | ||
System.out.println(employee);//Employee [id=111, name=Test] | ||
} | ||
@Test | ||
public void test3() throws JAXBException, IOException { | ||
JAXBContext context = JAXBContext.newInstance(Note.class); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
URL url = new URL("http://www.w3school.com.cn/example/xmle/note.xml"); | ||
System.out.println(url); | ||
Note note = (Note)unmarshaller.unmarshal(url); | ||
System.out.println(note);//Note [to=null, from=null, heading=null, body=null] | ||
} | ||
@Test | ||
public void test4() throws JAXBException, IOException { | ||
JAXBContext context = JAXBContext.newInstance(Employee.class); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
String xmlStr = "<Employee><id>1504</id><name>Test</name></Employee>"; | ||
Employee employee = (Employee)unmarshaller.unmarshal(new StreamSource(new StringReader(xmlStr))); | ||
System.out.println(employee);//Employee [id=1504, name=Test] | ||
} | ||
|
||
@Test | ||
public void test2X() throws JAXBException { | ||
String xmlStr = "<Employee><id>111</id><name>Test</name></Employee>"; | ||
JAXBContext context = JAXBContext.newInstance(Note.class); | ||
Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
Employee employee = (Employee)unmarshaller.unmarshal(System.in); | ||
System.out.println(employee);//Employee [id=111, name=Test] | ||
} | ||
|
||
|
||
} |
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,5 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<Employee> | ||
<id>111</id> | ||
<name>Test</name> | ||
</Employee> |