gpt4 book ai didi

java - JAXB2 将 xmlns 添加到 XML 输出

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

我看到其他时间有人问过这个问题,我已经查看了答案,但我应该承认我仍然无法获得所需的输出:(现在我想要的是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="TOKEN">
<purchase>true</purchase>
</tokenregistrationrequest>

我的类定义如下所示:

@XmlRootElement() //THIS HAS BEEN ADDED MANUALLY
public class Tokenregistrationrequest {
protected boolean purchase;
}

我已经修改了包信息,如下所示:

@javax.xml.bind.annotation.XmlSchema(xmlns = {
@javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance"),
@javax.xml.bind.annotation.XmlNs(prefix = "xs", namespaceURI = "http://www.w3.org/2001/XMLSchema"),
@javax.xml.bind.annotation.XmlNs(prefix = "", namespaceURI = "TOKEN")
})

当我运行代码时,我得到的只是......

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest>
<purchase>true</purchase>
</tokenregistrationrequest>

我很确定我在这里遗漏了一些基本的东西......任何帮助将不胜感激

编辑:在进行更多测试时,我发现编译测试类 JAXB 工件时已编译,但未编译 package-info.java。这是因为它的使用是可选的吗?

干杯

最佳答案

提问已经有一段时间了,但我发现自己处于类似的情况。这有点令人不安,但如果你真的坚持的话,用这种方式是可能的。

您只需手动将这些属性作为属性添加到实体类中。

@XmlAttribute(name = "xmlns")
private String token = "TOKEN";

@XmlAttribute(name = "xmlns:xsd")
private String schema = "http://www.w3.org/2001/XMLSchema";

@XmlAttribute(name = "xmlns:xsi")
private String schemaInstance = "http://www.w3.org/2001/XMLSchema-instance";

那么你应该得到这个作为输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest xmlns="TOKEN"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<purchase>true</purchase>
</tokenregistrationrequest>

关于java - JAXB2 将 xmlns 添加到 XML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30505282/

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