我看到其他时候也问过这个问题,我已经检查了答案,但我应该承认我仍然无法获得所需的输出:(现在我想要的是:
<?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;
}我已经修改了package-info,如下所示:
@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。这是因为这个的用法是可选的吗?
干杯
发布于 2020-02-10 15:48:52
问这个问题已经有一段时间了,但我发现自己也遇到了类似的情况。这有点令人不安,但如果你真的坚持这样做,用这种方式是可能的。
您可以手动将这些属性作为属性添加到您的实体类中。
@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>发布于 2015-05-28 19:52:03
包级上的Annotation XmlNs将前缀绑定到命名空间。但是,如果没有使用Namespace,则不会将Namespace节点添加到root-element中。
您可能应该将Tokenregistrationrequest放入令牌命名空间中:
@XmlRootElement(namespace = "TOKEN") //THIS HAS BEEN ADDED MANUALLY
public class Tokenregistrationrequest {
protected boolean purchase;这将导致
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tokenregistrationrequest xmlns="TOKEN">
<purchase>true</purchase>
</tokenregistrationrequest>其余的前缀声明将不会出现,因为在结果文档中没有来自这些名称空间的节点。如果有,前缀声明也应该序列化。
https://stackoverflow.com/questions/30505282
复制相似问题