Java Jaxb:xsi在根元素中被ns2替换
我使用了以下代码片段Java Jaxb:xsi在根元素中被ns2替换,java,namespaces,jaxb,marshalling,Java,Namespaces,Jaxb,Marshalling,我使用了以下代码片段 marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setSchema(getSchema(xsdSchema)); marshaller.setProperty("com
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);
marshaller.setSchema(getSchema(xsdSchema));
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",
new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String arg0, String arg1,
boolean arg2) {
return "tf";
}
});
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
"http://www.xyz.com/tf " + xsdSchema);
marshaller.marshal(obj, new StreamResult(xml));
输出xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tf:abc xmlns:tf="http://www.xyz.com/tf" xmlns:ns2="http://www.w3.org/2001/XMLSchema-instance" ns2:schemaLocation="http://www.xyz.com/tf schema/myxsd.xsd">
如你所见,我用“ns2”代替“xsi”
我需要的是ns2中的xsi
提前感谢。您可以扩展
名称空间前缀apper
的实现来执行此操作,而不是总是从getPreferredPrefix返回tf
您可以扩展名称空间前缀apper
的实现来执行此操作,而不是总是从tf
返回getPreferredPrefix
您的namespaceprefixmapper应该为声明的每个命名空间返回值:
package mapdemo;
import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class JAXBMarshallerExample {
public static NamespacePrefixMapper val = new NamespacePrefixMapper() {
private static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
private static final String TF_URI = "http://www.xyz.com/tf/whatever.xsd";
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(XSI_URI.equals(namespaceUri)) {
return "xsi";
} else if(TF_URI.equals(namespaceUri)) {
return "tf";
}
return suggestion;
}
@Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { XSI_URI, TF_URI};
}
};
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(Output.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", val);
Output output = new Output();
output.setId("1");
marshaller.marshal(output, System.out);
}
@XmlRootElement(namespace="http://www.xyz.com/tf/whatever.xsd")
public static class Output {
String id;
public String getId() {
return id;
}
@XmlElement
public void setId(String id) {
this.id = id;
}
}
}
您的namespaceprefixmapper应该为声明的每个命名空间返回值:
package mapdemo;
import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class JAXBMarshallerExample {
public static NamespacePrefixMapper val = new NamespacePrefixMapper() {
private static final String XSI_URI = "http://www.w3.org/2001/XMLSchema-instance";
private static final String TF_URI = "http://www.xyz.com/tf/whatever.xsd";
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(XSI_URI.equals(namespaceUri)) {
return "xsi";
} else if(TF_URI.equals(namespaceUri)) {
return "tf";
}
return suggestion;
}
@Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { XSI_URI, TF_URI};
}
};
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(Output.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", val);
Output output = new Output();
output.setId("1");
marshaller.marshal(output, System.out);
}
@XmlRootElement(namespace="http://www.xyz.com/tf/whatever.xsd")
public static class Output {
String id;
public String getId() {
return id;
}
@XmlElement
public void setId(String id) {
this.id = id;
}
}
}
谢谢@SorenD,尝试过你的方法。但它仍然是ns2,而不是xsi.Hi@PavanSky。我已经用完整的代码更新了示例。否则,请更新有关setSchema(…)和您试图封送的对象的信息。谢谢@SorenD,尝试了您的方法。但它仍然是ns2,而不是xsi.Hi@PavanSky。我已经用完整的代码更新了示例。否则,请使用有关setSchema(…)和您试图封送的对象的信息进行更新。