客户架构:
@XmlRootElement
public class Customer {
String name;
int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
@XmlElement
public void setId(int id) {
this.id = id;
}
}
解组编码:
public static void unmarshalXml2Obj() {
try {
File file = new File(
"D:/Workspace/CustomerFile.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
System.out.println("ID : " + customer.getId());
System.out.println("Name : " + customer.getName());
System.out.println("-------------------------");
} catch (JAXBException e) {
e.printStackTrace();
}
}
要解组的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer >
<id>100</id>
<name><FN>Johny</FN><LN>Depp</LN><a>AAAA</a><z>ZZZZ</z></name>
</customer>
输出:
test.example.Customer@eeb406
ID : 100
Name :
-------------------------
不使用尖括号时:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer >
<id>100</id>
<name><FN>Johny</FN><LN>Depp</LN><a>AAAA</a><z>ZZZZ</z></name>
</customer>
正确输出:
test.example.Customer@7a279c
ID : 100
Name : <FN>Johny</FN><LN>Depp</LN><a>AAAA</a><z>ZZZZ</z>
-------------------------
我无法在我的xml中使用&amp; lt&amp; gt。需要使用Angular括号(&lt;和&gt;)的解决方案。