我希望控制JAXB在编组XML时生成xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
声明的位置。我已经看到像this这样的解决方案使用JAXB_SCHEMA_LOCATION属性将其添加到根元素,但是我不希望它在根节点上,我希望它介于两者之间。这就是我所拥有的:
@XmlRootElement(name = "RootNode")
@XmlAccessorType(XmlAccessType.NONE)
public class RootNode {
@XmlElement(name = "IntermediateNode")
private IntermediateNode intermediateNode;
//getter & setter
}
@XmlAccessorType(XmlAccessType.NONE)
public class IntermediateNode {
@XmlElement(name = "MyEntity")
private MyEntity myEntity;
//getter and setter
}
@XmlAccessorType(XmlAccessType.NONE)
public class MyEntity {
@XmlElement(name = "Name")
private String name;
@XmlElement(name = "Title", nillable = true)
private String title;
//getters and setters
}
序列化如:
MyEntity myEntity = new MyEntity();
myEntity.setName("George");
myEntity.setTitle(null);
IntermediateNode intNode = new IntermediateNode();
intNode.setMyEntity(myEntity);
RootNode rootNode = new RootNode();
rootNode.setIntermediateNode(intNode);
JAXBContext context = JAXBContext.newInstance(RootNode.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(rootNode, System.out);
生成XML,如:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootNode>
<IntermediateNode>
<MyEntity>
<Name>George</Name>
<Title xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</MyEntity>
</IntermediateNode>
</RootNode>
但我想要的是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootNode>
<IntermediateNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyEntity>
<Name>George</Name>
<Title xsi:nil="true"/>
</MyEntity>
</IntermediateNode>
</RootNode>
我甚至尝试使用像这样的package-info.java将我的IntermediateNode和MyEntity类移动到他们自己的包中,但这只是将xmlns:xsi滚动到根元素。
@javax.xml.bind.annotation.XmlSchema(
xmlns = {
@javax.xml.bind.annotation.XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.example.intermediate;
有可能得到我想要的东西吗?