Java:编组对象 - 删除 xml 中额外的 ns2 注释

发布于 2024-11-28 19:25:28 字数 1195 浏览 3 评论 0原文

我正在尝试根据定义的架构将对象内的数据编组到 xml 文件中。但是,当我打印 xml 文件时,我在 xml 标签上收到了额外的注释。有什么方法可以摆脱额外的名称空间注释(即 ns2)

这是我从编组收到的 xml 的示例。

<?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 代码正在做的事情:

            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);

非常感谢任何帮助!谢谢!

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)

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>

What I want is something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes">
<root xmlns="http://www.something.com/something">
    <food>steak</food>
    <beverage>water</beverage>
</root>

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不顾 2024-12-05 19:25:28

通过将命名空间 URI(“http://www.something.com/something”)添加到用于构造 JAXB 元素的 QName,并利用包级别 @XmlSchema 注释将为您提供所需的名称空间限定:

package-info

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

import javax.xml.bind.annotation.*;

Food

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;
    }

}

Demo

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);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FoodSchema xmlns="http://www.something.com/something">
    <beverage>water</beverage>
    <food>steak</food>
</FoodSchema>

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:

package-info

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

import javax.xml.bind.annotation.*;

Food

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;
    }

}

Demo

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);
    }

}

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FoodSchema xmlns="http://www.something.com/something">
    <beverage>water</beverage>
    <food>steak</food>
</FoodSchema>
乱了心跳 2024-12-05 19:25:28

添加到 xsd 架构定义 elementFormDefaultattributeFormDefault

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ...
elementFormDefault="qualified" attributeFormDefault="unqualified" ... >

add to xsd schema definition elementFormDefault and attributeFormDefault:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ...
elementFormDefault="qualified" attributeFormDefault="unqualified" ... >
や莫失莫忘 2024-12-05 19:25:28

感谢您的回答。只是给出示例代码
之前:

 marshaller.marshal(new  JAXBElement(new QName(**""**,"Document"),Document.class,swiftDoc), sw);

之后:

marshaller.marshal(new  JAXBElement(new QName(**"http://www.w3.org/2001/XMLSchema**","Document"),Document.class,swiftDoc), sw);

和多余的“ns:”被删除。

Thanks for answer. Just to give sample code
before:

 marshaller.marshal(new  JAXBElement(new QName(**""**,"Document"),Document.class,swiftDoc), sw);

after:

marshaller.marshal(new  JAXBElement(new QName(**"http://www.w3.org/2001/XMLSchema**","Document"),Document.class,swiftDoc), sw);

and extra "ns:" was removed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文