gpt4 book ai didi

java - 将对象编码为 xml 并删除 xmlns

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:11 24 4
gpt4 key购买 nike

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

     @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。

最佳答案

在您的映射中,您很可能在 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 输出中删除命名空间限定,您可以简单地删除您放入的元数据以使其首先发生(假设这就是您想要执行的操作)。

了解更多信息

关于java - 将对象编码为 xml 并删除 xmlns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17656832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com