根元素缺少JAXB名称空间前缀

时间:2015-07-06 16:29:25

标签: xml jaxb jaxb2

命名空间没有以我的XML为前缀。当我编组时,我正在获取下面的XML。我需要它ns2:ircSelfCheckDownload

<ircSelfCheckDownload xsi:schemaLocation="http://www.example.com/external/schemas/irc/v2 irc.xsd" 
xmlns:ns2="http://www.example.com/external/schemas/irc/v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <lastUpdatedTimestamp>2015-07-06T12:15:26.657-04:00</lastUpdatedTimestamp>
</ircSelfCheckDownload> 

你能告诉我我错过了什么吗?

我的代码:

  Marshaller marshaller = jc.createMarshaller();

  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

  marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.pjm.com/external/schemas/irc/v2 irc.xsd");

  marshaller.marshal(obj, writer);

1 个答案:

答案 0 :(得分:0)

这对我有用。创建一个package-info.java文件,里面放了这个注释

@javax.xml.bind.annotation.XmlSchema(
        namespace="http://www.example.com/external/schemas/irc/v2",
        elementFormDefault=XmlNsForm.QUALIFIED,
        xmlns = { 
            @XmlNs(
                prefix = "ns2", 
                namespaceURI="http://www.example.com/external/schemas/irc/v2")
        }
    )
//put you package name
package example;

import javax.xml.bind.annotation.*;