0

我想在 marshal 使用 NamespacePrefixMapper 类而不是使用 package-info.java 文件生成的 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 请帮助

4

1 回答 1

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......

于 2013-02-07T09:13:08.297 回答