我想删除使用NamespacePrefixMapper类而不是使用package-info.java文件的marshal生成的xml中的额外注释ns2。
我的代码如下:
请求类:
package com.example;
import javax.xml.bind.annotation.*;
@XmlType(propOrder = { "password", "title", "firstName", "lastName", "email"})
@XmlRootElement
public class Req {
private String password;
private String title;
private String firstName;
private String lastName;
private String email;
@XmlElement(name= "Password")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@XmlElement(name= "Title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@XmlElement(name= "FirstName")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement(name= "LastName")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlElement(name= "Email")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
PreferredMapper类:
package com.example;
import javax.xml.namespace.NamespaceContext;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
public class PreferredMapper extends NamespacePrefixMapper{
private static final String URI ="http://schema.bsi.ib.example/service/request/v1";
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if(URI.equals(namespaceUri)) {
return "";
}
return suggestion;
}
@Override
public String[] getPreDeclaredNamespaceUris() {
return new String[] { URI };
}
}
测试类:
package com.example;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import com.example.Req;
import com.example.PreferredMapper;
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
public class Test {
public static void main(String args[]) throws JAXBException
{
setRequest("Tiger123", "Miss","Rose","Crocker","hello@world.com");
}
public static void setRequest(String passWord, String title, String firstName, String lastName,String email) throws JAXBException
{
JAXBContext context = JAXBContext.newInstance(Req.class);
Req ibw=new Req();
ibw.setEmail(email);
ibw.setPassword(passWord);
ibw.setTitle(title);
ibw.setFirstName(firstName);
ibw.setLastName(lastName);
JAXBElement<Req> element=new JAXBElement<Req> (new QName("http://schema.bsi.ib.example/service/request/v1","CustomerProfile"),Req.class,ibw);
Marshaller m=context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new PreferredMapper());
m.marshal(element, System.out);
}
}
输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:CustomerProfile xmlns:ns2="http://schema.bsi.ib.example/service/request/v1">
<Password>Tiger123</Password>
<Title>Miss</Title>
<FirstName>Rose</FirstName>
<LastName>Crocker</LastName>
<Email>hello@world.com</Email>
</ns2:CustomerProfile>
无法使用上述代码删除ns2请帮助
答案 0 :(得分:0)
如果你想“删除ns2”但是在语义上保持XML相同,那么你最终会得到像
这样的东西<CustomerProfile xmlns="http://schema.bsi.ib.example/service/request/v1">
<Password xmlns="">Tiger123</Password>
<Title xmlns="">Miss</Title>
因为目前只有CustomerProfile元素在命名空间中,所以它的子元素不在。请记住,您在映射器中建议的前缀映射只是 - 建议 - 如果它认为不合适,编组人员可能会忽略它们。
我认为您拒绝修改package-info
,因为您想要从现有.xsd
生成所有内容。您是否可以修改此架构并将elementFormDefault="qualified"
添加到<xs:schema>
元素?如果你能做到那么
<CustomerProfile xmlns="http://schema.bsi.ib.example/service/request/v1">
<Password>Tiger123</Password>
<Title>Miss</Title>
(没有xmlns=""
)将生效,您现有的映射器可能会开始工作。
如果您无法修改架构,那么您可能不希望在CustomerProfile
上使用默认命名空间......