package-info.java ファイルを使用する代わりに、NamespacePrefixMapper クラスを使用してマーシャルによって生成された 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 を削除できません。助けてください