我有一个问题.我想使用JAXB将对象转换为另一个对象.就像在,我有一个类com.home.Student,另一个类com.school.Student,都有相同的参数,实际上两者都是相同的(复制粘贴),但不同的包.我想用它们之间进行转换JAXB.
怎么做,请帮帮我.
bdo*_*han 40
您可以执行以下操作.
注意:
com.home.Student
package com.home;
public class Student {
private String name;
private Status status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
Run Code Online (Sandbox Code Playgroud)
com.home.Status
package com.home;
public enum Status {
FULL_TIME("F"),
PART_TIME("P");
private final String code;
Status(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
Run Code Online (Sandbox Code Playgroud)
com.school.Student
package com.school;
public class Student {
private String name;
private Status status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
Run Code Online (Sandbox Code Playgroud)
com.school.Status
package com.school;
public enum Status {
FULL_TIME("F"),
PART_TIME("P");
private final String code;
Status(String code) {
this.code = code;
}
public String getCode() {
return code;
}
}
Run Code Online (Sandbox Code Playgroud)
com.example.Demo;
package com.example;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
com.home.Student studentA = new com.home.Student();
studentA.setName("Jane Doe");
studentA.setStatus(com.home.Status.FULL_TIME);
JAXBContext contextA = JAXBContext.newInstance(com.home.Student.class);
JAXBElement<com.home.Student> jaxbElementA = new JAXBElement(new QName("student"), com.home.Student.class, studentA);
JAXBSource sourceA = new JAXBSource(contextA, jaxbElementA);
JAXBContext contextB = JAXBContext.newInstance(com.school.Student.class);
Unmarshaller unmarshallerB = contextB.createUnmarshaller();
JAXBElement<com.school.Student> jaxbElementB = unmarshallerB.unmarshal(sourceA, com.school.Student.class);
com.school.Student studentB = jaxbElementB.getValue();
System.out.println(studentB.getName());
System.out.println(studentB.getStatus().getCode());
}
}
Run Code Online (Sandbox Code Playgroud)
Moh*_*our 37
如果你包含一些解释你的问题的代码会很好.
JAXB 101说你应该放置正确的注释,然后你可以正确地序列化和反序列化.你应该正确地注释你的类@XmlRootElement,@XmlElement,@XmlAttribute等
例如:
@XmlRootElement(name="student")
@XmlAccessorType(XmlAccessType.NONE)
class Student {
@XmlElement(name="name")
private String name;
@XmlElement(name="age")
private int age;
public Student() {
}
public String getName() { return name; }
public int getAge() { return age; }
}
Run Code Online (Sandbox Code Playgroud)
然后你可以使用JAXB Marshaller序列化它:
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.marshal(student, writer);
Run Code Online (Sandbox Code Playgroud)
并通过Unmarshelling输入来反序列化它.
JAXBContext context = JAXBContext.newInstance(Student.class);
Unmarshaller m = context.createUnmarshaller();
return (Student)m.unmarshal(new StringReader(input));
Run Code Online (Sandbox Code Playgroud)
请务必查看我上面提到的JavaDoc,因为有很多方法可以做到这一点.
如果你不能修改你的类,你仍然可以使用JAXB(或者你可以使用XStream)假设你的类如下:
class Student {
private String name;
private int age;
public Student() {
}
public void setName(String name) { this.name = name; }
public String getName() { return name; }
public void setAge(int age) { this.age = age; }
public int getAge() { return age; }
}
Run Code Online (Sandbox Code Playgroud)
您可以执行以下操作来序列化它:
Student student = new Student();
student.setAge(25);
student.setName('FooBar');
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(Student.class);
Marshaller m = context.createMarshaller();
m.marshal(new JAXBElement(new QName(Student.class.getSimpleName()), Student.class, student), writer);
System.out.println(writer.toString());
Run Code Online (Sandbox Code Playgroud)
如果您正在使用XStream,则可以在没有注释的情况下进行序列化(并且它更易于控制). http://x-stream.github.io/tutorial.html
| 归档时间: |
|
| 查看次数: |
61550 次 |
| 最近记录: |