JAXB:命名空间丢失

时间:2013-04-03 11:17:21

标签: java xml namespaces jaxb

我正在尝试从通过JAXB生成的类中序列化XML。

班级:

package ws.avail;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "pos",
    "uniqueID",
    "availStatusMessages"
})
@XmlRootElement(name = "OTA_HotelAvailNotifRQ")
public class OTAHotelAvailNotifRQ {
 ...
}

文件“package-info.java”:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.opentravel.org/OTA/2003/05", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package ws.avail;

marshaller(简化而没有错误处理):

context = JAXBContext.newInstance(rootElement.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(rootElement, stringWriter);
return stringWriter.toString();

此代码生成:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
<OTA_HotelAvailNotifRQ Version="1.000" Target="TEST">  
....

我期待这样的事情:

<?xml version="1.0" encoding="UTF-8"?>  
<OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" Version="1.000" Target="TEST">  
....

我有类似的代码并且工作正常,但我无法理解为什么此代码不显示命名空间。任何线索?谢谢!

1 个答案:

答案 0 :(得分:4)

您的地图是正确的

我试用了你的模型,它对我有用:

<强>演示

package ws.avail;

import java.io.StringWriter;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        OTAHotelAvailNotifRQ rootElement = new OTAHotelAvailNotifRQ();
        StringWriter stringWriter = new StringWriter();

        JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(rootElement, stringWriter);
        System.out.println(stringWriter.toString());
    }

}

<强>输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05"/>

<强>解

通常在出现此问题时,package-info类未编译或未与应用程序打包。


更多信息