Moxy - 创建没有相应Java属性的XML元素

时间:2013-05-30 14:35:45

标签: xml-serialization marshalling moxy

我一直在使用Moxy成功地将复杂的xml类型编组/解组成更简单的java结构。特别是,我正在处理ISO Pain 20022消息,并且XML中存在许多我们不关心的字段:

<?xml version="1.0" encoding="UTF-8"?>
<iso:Document xmlns:iso="urn:iso:std:iso:20022:tech:xsd:pain.001.001.04" >
    <iso:CstmrCdtTrfInitn>
        <iso:GrpHdr>
           <iso:MsgId>OriginalMessageID</iso:MsgId>
            <iso:CreDtTm>2013-05-29T20:02:22.615</iso:CreDtTm>
            <iso:NbOfTxs>1</iso:NbOfTxs>
            <iso:InitgPty/>
        </iso:GrpHdr>
       <iso:PmtInf>
...
</iso:Document>

这是我的oxm绑定文件:

<xml-element java-attribute="messageId" xml-path="iso:CstmrCdtTrfInitn/iso:GrpHdr/iso:MsgId/text()"/>
<xml-element java-attribute="creationDateTime" xml-path="iso:CstmrCdtTrfInitn/iso:GrpHdr/iso:CreDtTm/text()"/>

我需要生成额外的两个xml元素 iso:NbOfTxs iso:InitgPty ,它们总是相同的,并且java上没有相应的属性正在生成xml的类。

这可能吗?

感谢。

1 个答案:

答案 0 :(得分:0)

我还在研究这个用例。下面是我目前能够生成的XML(我发现它不正确)。

<强>输出

<?xml version="1.0" encoding="UTF-8"?>
<iso:Document xmlns:iso="urn:iso:std:iso:20022:tech:xsd:pain.001.001.04">
   <iso:CstmrCdtTrfInitn>
      <iso:GrpHdr>
         <iso:MsgId>OriginalMessageID</iso:MsgId>
      </iso:GrpHdr>
   </iso:CstmrCdtTrfInitn>
   <iso:CstmrCdtTrfInitn>
      <iso:GrpHdr>
         <iso:CreDtTm>2013-05-29T20:02:22.615</iso:CreDtTm>
         <iso:NbOfTxs>1</iso:NbOfTxs>
         <iso:InitgPty/>
      </iso:GrpHdr>
   </iso:CstmrCdtTrfInitn>
</iso:Document>

<强>文档

以下是您的域模型的简化版本:

import java.util.Date;

public class Document {

    private String messageId;

    private Date creationDateTime;
}

<强> oxm.xml

在下面的元数据中,我试图使用XmlAdapterDate属性转换为更复杂的对象,可以写出其他2个XML元素。

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="forum16839007">
    <xml-schema
        namespace="urn:iso:std:iso:20022:tech:xsd:pain.001.001.04"
        element-form-default="QUALIFIED">
        <xml-ns prefix="iso" namespace-uri="urn:iso:std:iso:20022:tech:xsd:pain.001.001.04"/>
    </xml-schema>
    <java-types>
        <java-type name="Document" xml-accessor-type="FIELD">
            <xml-root-element name="Document"/>
            <java-attributes>
                <xml-element java-attribute="messageId" xml-path="iso:CstmrCdtTrfInitn/iso:GrpHdr/iso:MsgId/text()"/>
                <xml-element java-attribute="creationDateTime" xml-path=".">
                    <xml-java-type-adapter value="forum16839007.ExtraAdapter"/>
                </xml-element>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

<强> ExtraAdapter

以下XmlAdapterDate转换为更复杂的对象。

import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;

import org.eclipse.persistence.oxm.annotations.XmlPath;

public class ExtraAdapter extends XmlAdapter<ExtraAdapter.Extra, Date>{

    public static class Extra {

        @XmlPath("iso:CstmrCdtTrfInitn/iso:GrpHdr/iso:CreDtTm/text()")
        public Date creationDateTime;

        @XmlPath("iso:CstmrCdtTrfInitn/iso:GrpHdr/iso:NbOfTxs/text()")
        public final int NbOfTxs = 1;

        @XmlPath("iso:CstmrCdtTrfInitn/iso:GrpHdr/iso:InitgPty/text()")
        public final Empty initgPty = new Empty();
    }

    public static class Empty {
    }

    @Override
    public Date unmarshal(Extra extra) throws Exception {
        return extra.creationDateTime;
    }

    @Override
    public Extra marshal(Date date) throws Exception {
        Extra extra = new Extra();
        extra.creationDateTime = date;
        return extra;
    }

}

<强>演示

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class Demo {

    public static void main(String[] args) throws Exception {
        Map<String, Object> properties = new HashMap<String, Object>(1);
        properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum16839007/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {Document.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum16839007/input.xml");
        Document document = (Document) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(document, System.out);
    }

}