Jax2bMarshaller setClassesToBeBound和额外的xmlns

时间:2017-08-01 17:25:53

标签: java xml jaxb2

我正在使用Jaxb2Marshaller并将多个类绑定到它。当我编组时,对象似乎映射到xml,但是有多个xmlns到其他与映射对象无关的对象。有没有办法避免不相关的xmlns出现在输出中,最好不修改基础对象?

on Bike Object:

@XmlElement(name = "bike", namespace = "http://company.com/bike", required = true)
总线对象上的

@XmlElement(name = "bus", namespace = "http://company.com/bus", required = true)

代码

Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(com.company.bike.class, com.company.bus.class, com.company.car.class);

StringWriter outWriter = new StringWriter();
StreamResult result = new StreamResult( outWriter );

Class bike = new Bike();
bike.setBikeType("fast-bike");
marshaller.marshal(bike, result);

结果

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:bike xsi:schemaLocation="" xmlns:ns2="http://company.com/bike" xmlns:ns4="http://company.com/bus" xmlns:ns1="http://company.com/car" xmlns:ns3="http://company.com/bike-type">
    <ns3:bike-type>
      fast-bike
    </ns3:bike-type>
</ns4:bike>

0 个答案:

没有答案