pom.xml中引入如下依赖:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.2</version>
</dependency>
要转化的xml如下:person.xml
<?xml version="1.0" encoding="UTF-8"?>
<person>
<username name="first-name">王哥</username>
<phone-number name="tel-number" type="private">111122211</phone-number>
<phone-number name="tel-number" type="public">22222222</phone-number>
<address key="working_location">北京-xxxx</address>
<sex>M</sex>
<weight>75kg</weight>
</person>
下面直接贴代码:
Person.java
package com.xml.demo;
import org.eclipse.persistence.oxm.annotations.XmlPath;
import java.util.List;
public class Person {
@XmlPath("node[@name='first-name']/text()")
private String firstName;
private String sex;
private String weight;
private Address address;
@XmlPath("phone-number[@name='tel-number']")
private List<PhoneNumber> phoneNumbers;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<PhoneNumber> getPhoneNumbers() {
return phoneNumbers;
}
public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}
Address.java
package com.xml.demo;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class Address {
private String key;
@XmlValue
private String text; //地址具体名称
@XmlAttribute(name="key")
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Address(String key, String text) {
this.key = key;
this.text = text;
}
public Address() {
}
}
PhoneNumber.java
package com.xml.demo;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
public class PhoneNumber {
@XmlAttribute
private String type;
@XmlValue
private String number;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public PhoneNumber(String type, String number) {
this.type = type;
this.number = number;
}
public PhoneNumber() {
}
}
测试类:
package com.xml.demo;
import blog.predicate.Customer;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import org.eclipse.persistence.jaxb.xmlmodel.ObjectFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) throws Exception {
Person p=new Person();
p.setFirstName("王哥");
p.setSex("M");
p.setWeight("75kg");
Address addr=new Address("working_location","北京-xxxx");
p.setAddress(addr);
List<PhoneNumber> phoneNumbers=new ArrayList<PhoneNumber>();
PhoneNumber phoneNumber1=new PhoneNumber("private","111122211");
PhoneNumber phoneNumber2=new PhoneNumber("public","22222222");
phoneNumbers.add(phoneNumber1);
phoneNumbers.add(phoneNumber2);
p.setPhoneNumbers(phoneNumbers);
JAXBContext jc = JAXBContextFactory.createContext(new Class[]{Person.class, ObjectFactory.class},null);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(p, System.out);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/main/resources/person.xml");
Person p1 = (Person) unmarshaller.unmarshal(xml);
System.out.println(p1.getAddress().getText());
}
}