【问题标题】:Marshalling and Unmarshalling changes xml using moxy使用 moxy 编组和解组更改 xml
【发布时间】:2013-05-15 08:10:31
【问题描述】:

我正在尝试创建 this kind(xsd inside) 的文档。一些例子是here。由于根元素和其他一些常量元素中的常量值,我使用 Eclipse 生成了一个模板:

<?xml version="1.0" encoding="UTF-8"?>
<invoice:response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns="http://www.forum-datenaustausch.ch/invoice" xsi:schemaLocation="http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd" language="de">
  <invoice:processing>
    <invoice:transport from="" to="">
      <invoice:via sequence_id="0" via=""/>
    </invoice:transport>
  </invoice:processing>
  <invoice:payload response_timestamp="0">
    <invoice:invoice request_date="2001-12-31T12:00:00" request_id="" request_timestamp="0"/>
  </invoice:payload>
</invoice:response>

但是简单的解组和编组会改变内容:

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="http://www.forum-datenaustausch.ch/invoice" xmlns:ns1="http://www.w3.org/2000/09/xmldsig#" xmlns:ns0="http://www.w3.org/2001/04/xmlenc#" language="de">
   <processing>
      <transport from="" to="">
         <via via="" sequence_id="0"/>
      </transport>
   </processing>
   <payload response_timestamp="0">
      <invoice request_timestamp="0" request_date="2001-12-31T12:00:00.0" request_id=""/>
   </payload>
</response>

由于某种原因,架构位置属性消失了。这可以在编组之前手动添加。第二个问题是,所有前缀都消失了。 我不知道谁使用了生成的 xml(他们是否使用手写代码解组?有或没有验证?)。因此,我想要一个与给定示例最相似且有效的输出。 那么有没有办法让现有元素和属性保持不变,让 moxy 为每个元素添加命名空间前缀?

【问题讨论】:

    标签: java xml moxy


    【解决方案1】:

    以下内容应该会有所帮助。 EclipseLink 论坛也在处理这个问题:


    由于某种原因,架构位置属性消失了。

    您可以在Marshaller 上指定以下属性以输出架构位置:

    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd");
    

    第二个问题是,所有前缀都消失了。

    命名空间前缀没有了,但命名空间限定是相同的(所有元素都具有相同的本地名称和命名空间 URI)。在第一个文档中,invoice 前缀被分配给 http://www.forum-datenaustausch.ch/invoice 命名空间,而在第二个文档中,命名空间被分配为默认命名空间


    在设计时控制命名空间前缀

    您可以利用 @XmlSchema 注释提供 MOXy 提示,说明应使用哪些命名空间前缀(请参阅:http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html)。

    包裹信息

    @XmlSchema(
        elementFormDefault=XmlNsForm.QUALIFIED,
        namespace="http://www.forum-datenaustausch.ch/invoice",
        xmlns={
            @XmlNs(prefix="invoice", namespaceURI="http://www.forum-datenaustausch.ch/invoice"),
            @XmlNs(prefix="ds", namespaceURI="http://www.w3.org/2000/09/xmldsig#"),
            @XmlNs(prefix="xenc", namespaceURI="http://www.w3.org/2001/04/xmlenc#")
        }
    )
    package forum16559889;
    
    import javax.xml.bind.annotation.*;
    

    回应

    package forum16559889;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Response {
    }
    

    演示

    package forum16559889;
    
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Response.class);
    
            Response response = new Response();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd");
            marshaller.marshal(response, System.out);
        }
    
    }
    

    输出

    <?xml version="1.0" encoding="UTF-8"?>
    <invoice:response xsi:schemaLocation="http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:invoice="http://www.forum-datenaustausch.ch/invoice" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    

    在运行时控制命名空间前缀

    您可以利用 MOXy 的 NamespacePrefixMapper 扩展来控制运行时使用的命名空间前缀。

    package forum16559889;
    
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.MarshallerProperties;
    import org.eclipse.persistence.oxm.NamespacePrefixMapper;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Response.class);
    
            Response response = new Response();
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.forum-datenaustausch.ch/invoice generalInvoiceResponse_440.xsd");
    
            marshaller.setProperty(MarshallerProperties.NAMESPACE_PREFIX_MAPPER, new NamespacePrefixMapper() {
    
                @Override
                public String getPreferredPrefix(String namespaceUri,
                        String suggestion, boolean requirePrefix) {
                    if("http://www.forum-datenaustausch.ch/invoice".equals(namespaceUri)) {
                        return "invoice";
                    } else if("http://www.w3.org/2000/09/xmldsig#".equals(namespaceUri)) {
                        return "ds";
                    } else if("http://www.w3.org/2001/04/xmlenc#".equals(namespaceUri)) {
                        return "xenc";
                    } else {
                        return null;
                    }
                }
    
            });
    
            marshaller.marshal(response, System.out);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 2019-04-23
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      相关资源
      最近更新 更多