使用JAXB在XML中附加Default元素

时间:2018-08-29 14:04:18

标签: xml jaxb

我最近开始研究JAXB,在脑海中呆了两天,在互联网上进行了一些研究,但没有找到所需的答案。需要帮忙。 (PS:这个问题可能太愚蠢了,请裸露)

每当我们使用JABX创建XML时,它都会自动附加XML标头,例如输出可能是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CstmrCdtTrfInitn>
.
.
.
</CstmrCdtTrfInitn>

因此,这里添加了XML版本行。 我想要的是,在程序生成的所有XML中都存在默认标记,例如:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.08">
<CstmrCdtTrfInitn>
.
.
.
</CstmrCdtTrfInitn>
</Document>

默认情况下,所生成的任何XML都应带有Document标记。

以下是我的实用类,用于创建JAXBContext对象,创建marshall对象和生成XML,这在主驱动程序函数中被调用。

public class JaxbUtil {

    public static <E> JAXBContext create_context_obj(E obj) 
    {
        System.out.println("Creating JAXB Context Object......");
        try {
            return JAXBContext.newInstance(obj.getClass());
        } catch (JAXBException e) {
            System.out.println("JAXBException: Exception thrown in create_context_obj function");
            e.printStackTrace();
        }
        return null;
    } 

    public static Marshaller create_marshall_obj(JAXBContext contextObj)
    {
        System.out.println("Generating JAXB Marshaller......");
        try {
            return contextObj.createMarshaller();
        } catch (JAXBException e) {
            System.out.println("JAXBException: Exception thrown in create_marshall_obj function");
            e.printStackTrace();
        }
        return null;
    }

    public static <E> void generate_xml(Marshaller marshallerObj,String fileName,E obj)
    {
        System.out.println("Generating "+fileName+"......");
        try {
            marshallerObj.marshal(obj, new FileOutputStream(fileName));
        } catch (FileNotFoundException e) {
            System.out.println("FileNotFoundException: Exception thrown in generate_xml_obj function ");            
            e.printStackTrace();
            System.exit(1);
        } catch (JAXBException e) {
            System.out.println("JAXBException: Exception thrown in generate_xml function ");                    
            e.printStackTrace();
            System.exit(1);
        }
    }
}

以下是我的驱动程序主类:

public class Driver{
    public static void main(String[] args) 
    {       
        String file_name="pain.001.001.08.xml";
        JAXBContext contextObj= JaxbUtil.create_context_obj(new Document());             
        if(contextObj == null)
        {
            System.out.println("Process terminated due to JAXBException");
            System.exit(1);
        }

        /*2.    Create Marshaler object*/
        Marshaller marshallerObj = JaxbUtil.create_marshall_obj(contextObj);
        if(marshallerObj==null)
        {
            System.out.println("Process terminated due to JAXBException");
            System.exit(1);
        }
        else
        {
            try {               
                marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            } catch (PropertyException e) {
                System.out.println("JAXBException: Exception thrown in marshallerObj.setProperty function");
                System.out.println("Process terminated due to JAXBException");
                e.printStackTrace();
            }
        }
        /* --- Create XML objects - START --- */
        String id1="8d0bf39a-dc8a-4e38-8cdd-28eabbd79e8";
        String AnyBIC="ONPJUR9JN26";
        String MsgId="3f93b34c-8b69-4b0c-bcee-d181881cc95";
        String CreDtTm="2017-03-31T18:51:44.018+03:00";
        String xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.08";

        Othr othr_Info=new Othr(id1);
        OrgId OrglId_Info=new OrgId(AnyBIC,othr_Info);
        Id id_info=new Id(OrglId_Info);
        InitgPty InitgPty_info=new InitgPty(id_info);
        CstmrCdtTrfInitn crdrInfo=new CstmrCdtTrfInitn(MsgId,CreDtTm,InitgPty_info);

        /* --- Create XML objects - END  ---  */

        /*3.    GENERATE XML */
        JaxbUtil.generate_xml(marshallerObj,file_name,crdrInfo);        
        System.out.println( file_name + " : XML GENERATED, PROCESS COMPLETED SUCCESSFULLY");
    }
}

0 个答案:

没有答案