如何在Element jaxb xjc ann上注释名称空间

时间:2013-01-08 16:08:14

标签: java xml jaxb annotations xjc

我需要创建包含付款信息的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文件?

thx 4回复

0 个答案:

没有答案