在编组jaxb时从根元素中删除xmlns属性

时间:2023-01-14 08:44:26

This might be a related to JAXB Marshaller - How do I suppress xmlns namespace attributes?

这可能与JAXB Marshaller有关 - 如何抑制xmlns命名空间属性?

But my problem is a little different. I do the regular java marshalling and my xsd has no namespaces.The generated xml is without namespaces as well, except for the root element.

但我的问题有点不同。我做常规的java编组,我的xsd没有名称空间。生成的xml也没有名称空间,除了根元素。

<?xml version="1.0" encoding="UTF-8"?><rootElement xmlns:ns2="unwanted namespace">

The unwanted namespace is from another schema from the same project and I am not sure why that is being picked up at this stage.

不受欢迎的命名空间来自同一个项目中的另一个模式,我不确定为什么会在此阶段获取它。

My rootElement.java generated by jaxb2-maven-plugin looks like :

我的rootElement.java由jaxb2-maven-plugin生成,如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"feed"
})
@XmlRootElement(name = "rootElement", namespace = "")
public class RootElement{
....
}

At this point all I want is to get rid of the xmlns:ns2="unwanted namespace" attribute from the generated xml and I am struggling with it.

此时我想要的是从生成的xml中删除xmlns:ns2 =“unwanted namespace”属性,我正在努力解决它。

I looked at my package-info.java and it looks like:

我查看了我的package-info.java,它看起来像:

@javax.xml.bind.annotation.XmlSchema(namespace = "unwanted namespace", elementFormDefault =   javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.mypackage;

I tried adding he -npa but it wont work on jaxb2-maven-plugin for some reason. I tried the NamespaceMapper but that works for changing prefixes. I could not get it to remove the namespace altogether. This is bothering me for a day now.

我尝试添加他-npa但是由于某种原因它不能用于jaxb2-maven-plugin。我尝试了NamespaceMapper,但它适用于更改前缀。我无法完全删除命名空间。这让我困扰了一天。

1 个解决方案

#1


1  

I have similar requirements at the moment. The only solution that worked for me is implementing a wrapper for XMLStreamWriter.

我现在有类似的要求。对我有用的唯一解决方案是为XMLStreamWriter实现一个包装器。

Please, take a look at my answer here. I've also described there other solutions I tried out.

请在这里查看我的答案。我还描述了我试过的其他解决方案。

Serialization using code from the link above looks like this:

使用上面链接中的代码进行序列化如下所示:

XMLOutputFactory factory = XMLOutputFactory.newFactory();

StringWriter writer = new StringWriter(XML_BUFFER_INITIAL_SIZE);
XMLStreamWriter xmlWriter = null;

try {
  xmlWriter = factory.createXMLStreamWriter(writer);
  JAXBContext context = JAXBContext.newInstance(MyJAXBGeneratedClass.class);
  Marshaller marshaller = context.createMarshaller();
  marshaller.marshal(reportContainer, new NamespaceStrippingXMLStreamWriter(xmlWriter));
  xmlWriter.flush();
}
finally {
  if (xmlWriter != null)
    xmlWriter.close();
}

return writer.toString();

#1


1  

I have similar requirements at the moment. The only solution that worked for me is implementing a wrapper for XMLStreamWriter.

我现在有类似的要求。对我有用的唯一解决方案是为XMLStreamWriter实现一个包装器。

Please, take a look at my answer here. I've also described there other solutions I tried out.

请在这里查看我的答案。我还描述了我试过的其他解决方案。

Serialization using code from the link above looks like this:

使用上面链接中的代码进行序列化如下所示:

XMLOutputFactory factory = XMLOutputFactory.newFactory();

StringWriter writer = new StringWriter(XML_BUFFER_INITIAL_SIZE);
XMLStreamWriter xmlWriter = null;

try {
  xmlWriter = factory.createXMLStreamWriter(writer);
  JAXBContext context = JAXBContext.newInstance(MyJAXBGeneratedClass.class);
  Marshaller marshaller = context.createMarshaller();
  marshaller.marshal(reportContainer, new NamespaceStrippingXMLStreamWriter(xmlWriter));
  xmlWriter.flush();
}
finally {
  if (xmlWriter != null)
    xmlWriter.close();
}

return writer.toString();