1

我需要创建带有付款信息的 xml 文档。有一些命名空间,为每个标签添加前缀是必要的。那是我的设置:

xsd:

<xs:complexType name="Document">
    <xs:sequence>
        <xs:element name="FIToFICstmrCdtTrf" type="FIToFICustomerCreditTransferV02"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="FIToFICustomerCreditTransferV02">
    <xs:sequence>
        <xs:element name="GrpHdr" type="GroupHeader33"/>
        <xs:element maxOccurs="unbounded" minOccurs="1" name="CdtTrfTxInf" type="CreditTransferTransactionInformation11"/>
    </xs:sequence>
</xs:complexType>

xjb:

<jaxb:bindings schemaLocation="../xsd/pacs.008.001.02.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='Document']">
  <annox:annotate>
    <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="SCTSIBIBlkCredTrf" namespace="urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf">
    </annox:annotate>
  </annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="xs:complexType[@name='FIToFICustomerCreditTransferV02']">
  <annox:annotate>
    <annox:annotate annox:class="javax.xml.bind.annotation.XmlType" namespace="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"/>
  </annox:annotate>
</jaxb:bindings>

生成的类:

@XmlRootElement(name = "SCTSIBIBlkCredTrf", namespace = "urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf")
public class Document implements Serializable {

private final static long serialVersionUID = 1L;

@XmlElement(name = "FIToFICstmrCdtTrf", required = true)
protected FIToFICustomerCreditTransferV02 fiToFICstmrCdtTrf;

生成xml的代码:

private void startXmlFiles(String[] args) {
    JAXBContext jaxbContext;
    File file = new File("C:\\file.xml");

    try {
        jaxbContext = JAXBContext.newInstance(Document.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        Document nroot = new Document();


        nroot.setFIToFICstmrCdtTrf(new FIToFICustomerCreditTransferV02());
        nroot.getFIToFICstmrCdtTrf().setGrpHdr(new GroupHeader33());
        nroot.getFIToFICstmrCdtTrf().getGrpHdr().setMsgId("MsgId");
        CreditTransferTransactionInformation11 CdtTrfTxInf = new CreditTransferTransactionInformation11();
        CdtTrfTxInf.setPmtId(new PaymentIdentification3());
        CdtTrfTxInf.getPmtId().setEndToEndId("End2End");
        CdtTrfTxInf.getPmtId().setInstrId("InstrId");
        CdtTrfTxInf.getPmtId().setTxId("TxId");

        nroot.getFIToFICstmrCdtTrf().getCdtTrfTxInf().add(CdtTrfTxInf);



        jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf SCTSIBIBlkCredTrf.xsd");
        jaxbMarshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
            @Override
            public String[] getPreDeclaredNamespaceUris() {
                return new String[] { WellKnownNamespace.XML_SCHEMA_INSTANCE };
            }

            @Override
            public String[] getPreDeclaredNamespaceUris2() {
                // TODO Auto-generated method stub
                return super.getPreDeclaredNamespaceUris2();
            }

            @Override
            public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
                if (namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE))
                    return "xsi";
                if (namespaceUri.equals(WellKnownNamespace.XML_SCHEMA))
                    return "xs";
                if (namespaceUri.equals(WellKnownNamespace.XML_MIME_URI))
                    return "xmime";
                if (namespaceUri.equals("urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf"))
                    return "EESCTSIBI";
                if (namespaceUri.equals("urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"))
                    return "sw8";
                return suggestion;

            }
            @Override
            public String[] getContextualNamespaceDecls() {
                // TODO Auto-generated method stub
                return super.getContextualNamespaceDecls();
            }
        });

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        //jaxbMarshaller.marshal(nroot, file);
        jaxbMarshaller.marshal(nroot, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

生成的xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EESCTSIBI:SCTSIBIBlkCredTrf xmlns:sw8="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02" xmlns:EESCTSIBI="urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf SCTSIBIBlkCredTrf.xsd">
<sw8:FIToFICstmrCdtTrf>
    <sw8:GrpHdr>
        <sw8:MsgId>MsgId</sw8:MsgId>
    </sw8:GrpHdr>
    <sw8:CdtTrfTxInf>
        <sw8:PmtId>
            <sw8:InstrId>InstrId</sw8:InstrId>
            <sw8:EndToEndId>End2End</sw8:EndToEndId>
            <sw8:TxId>TxId</sw8:TxId>
        </sw8:PmtId>
    </sw8:CdtTrfTxInf>
</sw8:FIToFICstmrCdtTrf>

xml 预期:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EESCTSIBI:SCTSIBIBlkCredTrf xmlns:sw8="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02" xmlns:EESCTSIBI="urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:EESCTSIBI:xsd:$SCTSIBIBlkCredTrf SCTSIBIBlkCredTrf.xsd">
<EESCTSIBI:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
    <sw8:GrpHdr>
        <sw8:MsgId>MsgId</sw8:MsgId>
    </sw8:GrpHdr>
    <sw8:CdtTrfTxInf>
        <sw8:PmtId>
            <sw8:InstrId>InstrId</sw8:InstrId>
            <sw8:EndToEndId>End2End</sw8:EndToEndId>
            <sw8:TxId>TxId</sw8:TxId>
        </sw8:PmtId>
    </sw8:CdtTrfTxInf>
</EESCTSIBI:FIToFICstmrCdtTrf>

我应该如何更改我的 xjb 文件?

谢谢 4 回复

4

0 回答 0