无法使用JAXB在java中解组xml而不使用任何注释 - 没有错误但解组不正确

时间:2017-10-10 20:15:21

标签: java xml jaxb unmarshalling

我正在尝试使用JAXB 将XML内容转换为Java对象,而不使用任何注释。我的XML文件(CustomerDtl.xml)结构如下所述

<?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>

我的pojos是

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);
    }

此代码的输出是

id=C001
list size=1
list size=[pojo.Customer@3e3abc88]
Print Customer Name:null

执行此程序后,它会打印正确的customerId,但会输出错误的客户列表大小 1 。虽然列表大小为1,并且列表包含Customer对象,但是当我遍历列表并尝试获取Customer的不同属性值时,我将变为null。

任何人都能解释我需要纠正的内容吗?我尝试使用注释,它按预期工作。没有注释是不可能的?

3 个答案:

答案 0 :(得分:0)

在xml中,您不必指定类名(#aboutInfo)作为列表的子项(div),只需为每个元素直接使用列表名称。< / p>

所以,像这样更改xml:

Customer

答案 1 :(得分:0)

尝试以下内容

    

        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);
        }

答案 2 :(得分:0)

如果您不想要任何注释(使用您共享的精确xml),请转到此处。

1)将您的列表包装到包装类(以处理customerList)

2)现在我在评论中询问,如果你有“Customer”元素,那么默认的“xml”名称将被识别为小c。如果您不想对其进行注释,请在阅读时进行处理。

这样说之后是一个工作样本(使用runnable main)修改你的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;
    }

}