marshall对象为xml并删除xmlns

时间:2013-07-15 14:36:11

标签: java xml jaxb

我使用以下代码进行解组:

     @Override
public String marshal(Object document) throws JAXBException {

    Class clazz = document.getClass();
    JAXBContext context =
        JAXBContext.newInstance( clazz.getPackage().getName() );
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
    StringWriter sw = new StringWriter();
    marshaller.marshal(document, sw);
    String xml = sw.toString();
    return xml;

}

结果如下:

 <IlpQuoteInput  QuoteId="2888284000185" xmlns="http://www.abc.com">
<Common IlpSellerId="0001">
    <Quotation QuotationDt="20130711"/>
    <Product CurrencyCd="E">
etc etc

这一切都很好,但我实际上不想在输出中使用xmlns,我该怎么办? 谢谢 PS我使用的是最新版本的JAXB和java 6.

1 个答案:

答案 0 :(得分:1)

在您的映射中,您很可能在package-info类上提供了@XmlSchema注释,其类似于以下内容:

@XmlSchema(
    namespace = "http://www.abc.com",
    elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

要从XML输出中删除命名空间限定,您可以简单地删除您放入的元数据,使其首先发生,假设这是您想要做的。

了解更多信息