无法解组使用JAXB Java的一个xml,而无需使用任何annotation-没有错误,但解组是不正确
问题描述:
我试图XML内容转换为使用JAXB 一个Java对象,而无需使用任何注释。我的XML文件(CustomerDtl.xml)结构如下无法解组使用JAXB Java的一个xml,而无需使用任何annotation-没有错误,但解组是不正确
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<basePojo>
<customerId>C001</customerId>
<customerList>
<Customer>
<name>Ram</name>
<phoneNo>123445</phoneNo>
</Customer>
<Customer>
<name>Tom</name>
<phoneNo>2332322</phoneNo>
</Customer>
</customerList>
</basePojo>
提到的和我的POJO是
BasePojo.java
package pojo;
import java.util.List;
import pojo.Customer;
public class BasePojo {
List<Customer> customerList;
String customerId;
public List<Customer> getCustomerList() {
return customerList;
}
public void setCustomerList(List<Customer> customerList) {
this.customerList = customerList;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
}
Customer.java
package pojo;
public class Customer {
private String name;
private String phoneNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
}
下文提到的一段代码,我已经写XML内容转换成Java对象
try {
JAXBContext jc = JAXBContext.newInstance(BasePojo.class);
StreamSource xml = new StreamSource("CustomerDtl.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<BasePojo> jaxbElementObject = unmarshaller.unmarshal(
xml, BasePojo.class);
BasePojo bPojo = jaxbElementObject.getValue();
System.out.println("id="+bPojo.getCustomerId());
System.out.println("list size="+bPojo.getCustomerList().size());
Iterator iterator = bPojo.getCustomerList().iterator();
while (iterator.hasNext()) {
Customer studentDetails = (Customer) iterator.next();
System.out.println("Print Name:" + studentDetails.getName());
}
} catch (JAXBException e) {
System.out.println("Exception:" + e);
}
和OUT把这个代码是
id=C001
list size=1
list size=[[email protected]]
Print Customer Name:null
后执行这个程序是打印正确的customerId,但它打印错误的客户名单大小。尽管列表大小为1,并且列表包含Customer对象,但是当我遍历列表并尝试获取Customer的不同属性值时,我变为null。
任何人都可以解释我需要纠正什么吗?使用注释我试过了,它按预期工作。没有注释是不可能的?
答
在xml中,您不必指定类名称(Customer
)作为您的列表的子项(customerList
),只需使用列表名称直接为每个元素。
所以,改变这样的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<basePojo>
<customerId>C001</customerId>
<customerList>
<name>Ram</name>
<phoneNo>123445</phoneNo>
</customerList>
<customerList>
<name>Tom</name>
<phoneNo>2332322</phoneNo>
</customerList>
</basePojo>
答
尝试类似下面
try {
XMLInputFactory xif = XMLInputFactory.newFactory();
StreamSource xml = new StreamSource("CustomerDtl.xml");
XMLStreamReader xsr = xif.createXMLStreamReader(xml);
while(xsr.hasNext()) {
if(xsr.isStartElement() && "Customer".equals(xsr.getLocalName())) {
break;
}
xsr.next();
}
JAXBContext jc = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Customer customer = unmarshaller.unmarshal(xsr, Customer.class).getValue();
System.out.println(customer.getName());
} catch (JAXBException e) {
System.out.println("Exception:" + e);
}
答
在这里你去,如果你不希望任何注释(与您共享的确切XML)。
1)包装你列表的包装类(处理customerList)
2)现在,当我问评论,如果您有“客户”元素,则默认公认的“XML”的名字将与小C。如果你不想对此进行注释,那么在阅读时应该对其进行处理。
说了这么这里之后是工作示例(带可运行主)修改您的BasePojo
package pojo;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
import javax.xml.transform.stream.StreamSource;
public class BasePojo {
String customerId;
CustomerList customerList;
public CustomerList getCustomerList() {
return customerList;
}
public void setCustomerList(CustomerList customerListObject) {
this.customerList = customerListObject;
}
public String getCustomerId() {
return customerId;
}
public void setCustomerId(String customerId) {
this.customerId = customerId;
}
public static void main(String[] args) {
try {
String customerData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
+ "<basePojo>\n"
+ "<customerId>C001</customerId>\n"
+ "<customerList>\n"
+ " <Customer>\n"
+ " <name>Ram</name>\n"
+ " <phoneNo>123445</phoneNo>\n"
+ " </Customer>\n"
+ "\n"
+ " <Customer>\n"
+ " <name>Tom</name>\n"
+ " <phoneNo>2332322</phoneNo>\n"
+ " </Customer>\n"
+ "</customerList>\n"
+ "</basePojo>";
JAXBContext jc = JAXBContext.newInstance(BasePojo.class);
StringReader reader = new StringReader(customerData);
StreamSource xml = new StreamSource(reader);
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(new ByteArrayInputStream(customerData.getBytes("UTF-8")));
xsr = new StreamReaderDelegate(xsr) {
@Override
public String getLocalName() {
String localName = super.getLocalName();
if ("Customer".equals(localName)) {
return "customer";
}
return localName;
}
};
Unmarshaller unmarshaller = jc.createUnmarshaller();
JAXBElement<BasePojo> jaxbElementObject = unmarshaller.unmarshal(xsr, BasePojo.class);
BasePojo bPojo = jaxbElementObject.getValue();
System.out.println("id=" + bPojo.getCustomerId());
System.out.println("list size=" + bPojo.getCustomerList().getCustomer().size());
Iterator iterator = bPojo.getCustomerList().getCustomer().iterator();
while (iterator.hasNext()) {
Customer studentDetails = (Customer) iterator.next();
System.out.println("Print Name:" + studentDetails.getName());
}
} catch (Exception e) {
System.out.println("Exception:" + e);
}
}
}
class CustomerList {
private List<Customer> customer;
public List<Customer> getCustomer() {
return customer;
}
public void setCustomer(List<Customer> customer) {
this.customer = customer;
}
}
您可以尝试使用(最终客户的客户:pBojo.getCustomerList()){...而不是你的while循环。我不知道你的泛型迭代器是否导致了这个问题。如果你打算使用迭代器,它应该至少是迭代器 iterator = ...(像Eclipse这样的IDE会给你在当前代码中的警告) –
让我们跳过迭代部分。为什么列表大小是1,有什么想法? – Bikku
好问题。你使用的是什么注释使它工作? –