Skip to content

Commit

Permalink
Add Unmarshaller method
Browse files Browse the repository at this point in the history
  • Loading branch information
cj96248 committed Nov 14, 2018
1 parent 79e935f commit 588d2bc
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/example/demo/lesson15/Employe2.java
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 + "]";
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/example/demo/lesson15/Note.java
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 src/test/java/com/example/demo/lesson15/SerilalizerSimple.java
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]
}


}
5 changes: 5 additions & 0 deletions src/test/resources/lesson15.xml
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>

0 comments on commit 588d2bc

Please sign in to comment.