JAXB marshal to string works differently on different computers

情到浓时终转凉″ 提交于 2020-01-06 05:26:35

问题


I have this test java app:

@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

And I try convert string xml to Element set this element to Request and convert request to 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);

    }
}

When I start this application on my machine I get next output:

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

But When I start this application on test computer I habe next output:

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

On the test machine missing tags ns5, ns6, ns2

I do not understand how this can be. The same version of Java, the same application but different output. Because of this, I have problems in the production server. How to fix it or where to find the cause?

来源:https://stackoverflow.com/questions/53188560/jaxb-marshal-to-string-works-differently-on-different-computers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!