Java: Marshalling Object -- Removing extra ns2 annotation in xml(Java: Marshalling Object -- 移除 xml 中额外的 ns2 注释)
问题描述
我正在尝试根据定义的架构将对象内的数据编组到 xml 文件中.但是,当我打印出 xml 文件时,我会在 xml 标记上收到额外的注释.有没有办法摆脱额外的命名空间注释(即ns2)
I am trying to marshall data within an object into an xml file based on a defined schema. However when I print out the xml file, I recieve extra annotations on the xml tags. Is there any way to get rid of the extra namespace annotation (i.e. ns2)
这是我从编组收到的 xml 示例.
This is an example of the xml I receive from marshalling.
<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns:ns2="http://www.something.com/something">
<ns2:food>steak</ns2:food>
<ns2:beverage>water</ns2:beverage>
</root>
我想要的是这样的:
<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns="http://www.something.com/something">
<food>steak</food>
<beverage>water</beverage>
</root>
这就是我的 Java 代码正在做的事情:
This is what my Java code is doing:
JAXBContext context = JAXBContext.newInstance("com.schema");
JAXBElement<FoodSchema> element = new JAXBElement<FoodSchema>
(new QName("FoodSchema"), Food.class, foodSchema);
Marshaller marshaller = context.createMarshaller();
OutputStream os = new FileOutputStream(object.getFilePath());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(element, os);
非常感谢任何帮助!谢谢!
Any help is much appreciated! Thanks!
推荐答案
通过在用于构造的QName中添加命名空间URI(http://www.something.com/something")JAXB 元素,并利用包级别的 @XmlSchema 注释将为您提供您正在寻找的命名空间限定:
By adding a namespace URI ("http://www.something.com/something") to the QName used to construct the JAXB element, and leveraging the package level @XmlSchema annotation will get you the namespace qualification that you are looking for:
包裹信息
@XmlSchema(
namespace="http://www.something.com/something",
elementFormDefault=XmlNsForm.QUALIFIED)
package forum7014746;
import javax.xml.bind.annotation.*;
食物
package forum7014746;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Food {
private String food;
private String beverage;
public String getFood() {
return food;
}
public void setFood(String food) {
this.food = food;
}
public String getBeverage() {
return beverage;
}
public void setBeverage(String beverage) {
this.beverage = beverage;
}
}
演示
package forum7014746;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Food.class);
Food foodSchema = new Food();
foodSchema.setFood("steak");
foodSchema.setBeverage("water");
JAXBElement<Food> element = new JAXBElement<Food> (new QName("http://www.something.com/something","FoodSchema"), Food.class, foodSchema);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(element, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FoodSchema xmlns="http://www.something.com/something">
<beverage>water</beverage>
<food>steak</food>
</FoodSchema>
这篇关于Java: Marshalling Object -- 移除 xml 中额外的 ns2 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java: Marshalling Object -- 移除 xml 中额外的 ns2 注释
基础教程推荐
- Struts2 URL 无法访问 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
