这是我的Parser类
public class Test {
public static void main(String args[]) throws Exception {
File file = new File("D:\\Test.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(MyOrder.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
MyOrder customer = (MyOrder) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer.getOrder().getSide());
}
}
Run Code Online (Sandbox Code Playgroud)
这是MyOrder.java文件
@XmlRootElement(name = "BXML")
public class MyOrder {
@XmlElement(name = "Bag")
protected Order order;
public MyOrder() {
}
@XmlAttribute
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的域对象(Order.java)
@XmlRootElement(name = "BXML")
public class Order {
public Order() {
}
@XmlAttribute(name = "Side")
protected BigInteger Side;
@XmlValue
public BigInteger getSide() {
return Side;
}
public void setSide(BigInteger side) {
Side = side;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图运行程序时遇到的异常
Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:
at public com.Order com.MyOrder.getOrder()
at com.MyOrder
Class has two properties of the same name "order"
this problem is related to the following location:
at public com.Order com.MyOrder.getOrder()
at com.MyOrder
this problem is related to the following location:
at protected com.Order com.MyOrder.order
at com.MyOrder
Run Code Online (Sandbox Code Playgroud)
Bra*_*sen 11
对于该@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.问题,您需要将初始化更改JAXBContext为以下内容:
JAXBContext jaxbContext = JAXBContext.newInstance(MyOrder.class, Order.class);
Run Code Online (Sandbox Code Playgroud)
对于Class has two properties of the same name "order"问题,您需要更改的定义protected Order order;来private Order order;.
此外,你想改变@XmlRootElement(name = "BXML")你的Order类@XmlRootElement(name = "Order").
小智 6
您可以看到下面的示例代码,用于从给定的 XML 生成 Java 对象。它在我的系统中运行良好。
客户.xml
<?xml version="1.0" encoding="UTF-8"?>
<company>
<customer id="100">
<age>25</age>
<name>Ram</name>
<Address>
<city>Bangalore</city>
<country>India</country>
</Address>
<Address>
<city>Patna</city>
<country>India</country>
</Address>
</customer>
<customer id="200">
<age>26</age>
<name>Ashu</name>
<Address>
<city>Delhi</city>
<country>India</country>
</Address>
<Address>
<city>Madhubani</city>
<country>India</country>
</Address>
</customer>
</company>
Run Code Online (Sandbox Code Playgroud)
公司.java
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="company")
public class Company {
@XmlElement(name="customer")
private List<Costumer> custList;
//
public List<Costumer> getCustList() {
return custList;
}
public void setCustList(List<Costumer> custList) {
this.custList = custList;
}
//
@Override
public String toString() {
return "Company [custList=" + custList + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
顾客.java
@XmlAccessorType(XmlAccessType.FIELD)
class Costumer {
@XmlElement(name="name")
private String name;
@XmlElement(name="age")
private int age;
@XmlElement(name="id")
private int id;
@XmlElement(name="Address")
private List<Address> addressList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + ", id=" + id + ", addressList=" + addressList + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
地址.java
@XmlAccessorType(XmlAccessType.FIELD)
class Address {
@XmlElement(name="city")
private String city;
@XmlElement(name="country")
private String country;
//
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
//
@Override
public String toString() {
return "Address [city=" + city + ", country=" + country + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
测试主程序.java
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class TestMain {
public static void main(String[] args) {
String xmlPath = "C:\\" + File.separator + "customer.xml";
try {
File file = new File(xmlPath);
JAXBContext jaxbContext = JAXBContext.newInstance(new Class[] {Company.class,Address.class,Costumer.class});
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Company customer = (Company) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="UTF-8"?>
<company>
<customer id="100">
<age>25</age>
<name>Ram</name>
<Address>
<city>Bangalore</city>
<country>India</country>
</Address>
<Address>
<city>Patna</city>
<country>India</country>
</Address>
</customer>
<customer id="200">
<age>26</age>
<name>Ashu</name>
<Address>
<city>Delhi</city>
<country>India</country>
</Address>
<Address>
<city>Madhubani</city>
<country>India</country>
</Address>
</customer>
</company>
Run Code Online (Sandbox Code Playgroud)