JAXB元帅在不同计算机上的字符串工作方式有所不同

时间:2018-11-07 11:26:34

标签: java jaxb marshalling unmarshalling

我有这个测试Java应用程序:

@XmlRootElement
public class Car {

    @XmlElement(namespace="http://www.example.com/CAR_NAME")
    private String name;

    @XmlElement(namespace="http://www.example.com/CAR_PRICE")
    private int price;

    @XmlElement(namespace="http://www.example.com/CAR_BUS")
    private Bus bus;

    //constructors, getters and setters

 @XmlRootElement
 public class Bus {

    @XmlElement(namespace="http://www.example.com/BUS_NAME")
    private String name;

    @XmlElement(namespace="http://www.example.com/BUS_PRICE")
    private int price;

    //constructors, getters and setters

@XmlRootElement
public class Request {

    @XmlAnyElement
    private Element element;

    //constructors, getters and setters

然后尝试将字符串xml转换为Element,并将此element设置为Request并将请求转换为string

public class Main {
    public static void main(String[] args) throws JAXBException, ParserConfigurationException, IOException, SAXException {

        String hz = "<car xmlns:ns6=\"http://www.example.com/CAR_BUS\" xmlns:ns5=\"http://www.example.com/CAR_NAME\" xmlns:ns2=\"http://www.example.com/CAR_PRICE\" xmlns:ns4=\"http://www.example.com/BUS_PRICE\" xmlns:ns3=\"http://www.example.com/BUS_NAME\">\n" +
                "    <ns5:name>CarName1</ns5:name>\n" +
                "    <ns2:price>999</ns2:price>\n" +
                "    <ns6:bus>\n" +
                "        <ns3:name>BusName1</ns3:name>\n" +
                "        <ns4:price>666</ns4:price>\n" +
                "    </ns6:bus>\n" +
                "</car>";


        JAXBContext jaxbContext = JAXBContext.newInstance(Car.class, Bus.class, Request.class);
        Marshaller marshaller = jaxbContext.createMarshaller();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Element documentElement = builder.parse(new InputSource(new StringReader(hz))).getDocumentElement();


        Document document = documentElement.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        String s = serializer.writeToString(documentElement);

        System.out.println(s);

        System.out.println("------------------------------");

        Request request = new Request(documentElement);

        StringWriter sw = new StringWriter();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(request, sw);
        String s1 = sw.toString();
        System.out.println(s1);

    }
}

当我在计算机上启动此应用程序时,得到下一个输出:

<?xml version="1.0" encoding="UTF-16"?>
<car xmlns:ns2="http://www.example.com/CAR_PRICE" xmlns:ns3="http://www.example.com/BUS_NAME" xmlns:ns4="http://www.example.com/BUS_PRICE" xmlns:ns5="http://www.example.com/CAR_NAME" xmlns:ns6="http://www.example.com/CAR_BUS">
    <ns5:name>CarName1</ns5:name>
    <ns2:price>999</ns2:price>
    <ns6:bus>
        <ns3:name>BusName1</ns3:name>
        <ns4:price>666</ns4:price>
    </ns6:bus>
</car>
------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request xmlns:ns6="http://www.example.com/CAR_BUS" xmlns:ns5="http://www.example.com/CAR_NAME" xmlns:ns2="http://www.example.com/CAR_PRICE" xmlns:ns4="http://www.example.com/BUS_PRICE" xmlns:ns3="http://www.example.com/BUS_NAME">
    <car>

        <ns5:name>CarName1</ns5:name>

        <ns2:price>999</ns2:price>

        <ns6:bus>

            <ns3:name>BusName1</ns3:name>

            <ns4:price>666</ns4:price>
    </ns6:bus>
</car>
</request>

但是当我在测试计算机上启动此应用程序时,我的下一个输出是:

<?xml version="1.0" encoding="UTF-16"?>
<car xmlns:ns2="http://www.example.com/CAR_PRICE" xmlns:ns3="http://www.example.
com/BUS_NAME" xmlns:ns4="http://www.example.com/BUS_PRICE" xmlns:ns5="http://www.example.com/CAR_NAME" xmlns:ns6="http://www.example.com/CAR_BUS">
    <ns5:name>CarName1</ns5:name>
    <ns2:price>999</ns2:price>
    <ns6:bus>
        <ns3:name>BusName1</ns3:name>
        <ns4:price>666</ns4:price>
    </ns6:bus>
</car>
------------------------------
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<request xmlns:ns6="http://www.example.com/CAR_BUS" xmlns:ns5="http://www.example.com/CAR_NAME" xmlns:ns2="http://www.example.com/CAR_PRICE" xmlns:ns4="http://www.example.com/BUS_PRICE" xmlns:ns3="http://www.example.com/BUS_NAME">
    <car>

        <name>CarName1</name>

        <price>999</price>

        <bus>

            <name>BusName1</name>

            <price>666</price>
    </bus>
</car>
</request>

在测试机上缺少标签ns5, ns6, ns2

我不知道这怎么可能。相同版本的Java,相同的应用程序但输出不同。因此,生产服务器出现问题。如何解决它或在哪里找到原因?

0 个答案:

没有答案