Springオブジェクト/XMLマッピングの例

Springオブジェクト/ XMLマッピングの例

Springのオブジェクト/ XMLマッピングは、オブジェクトをXMLに、またはその逆に変換しています。 このプロセスは、

  1. XML Marshalling –オブジェクトをXMLに変換します。

  2. XML UnMarshalling –XMLをオブジェクトに変換します。

このチュートリアルでは、Springのoxmを使用して変換を行う方法Object XMLを示します。

Note
意味がありません。Springのoxmを使用する理由と利点については、このofficial Spring Object/XML mappingの記事をお読みください。

1. プロジェクトの依存関係

この例の依存関係。

Note
Springのoxm自体はXMLマーシャリングまたはUnMarshallingを処理しません。開発者は、好みのXMLバインディングフレームワークを挿入する必要があります。 この場合、Castorバインディングフレームワークを使用します。

    
        3.0.5.RELEASE
    

    

        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
            org.springframework
            spring-context
            ${spring.version}
        

        
        
            org.springframework
            spring-oxm
            ${spring.version}
        

        
        
            org.codehaus.castor
            castor
            1.2
        

        
        
            xerces
            xercesImpl
            2.8.1
        

    

2. 単純なオブジェクト

単純なオブジェクト。後でXMLファイルに変換します。

package com.example.core.model;

public class Customer {

    String name;
    int age;
    boolean flag;
    String address;

    //standard getter, setter and toString() methods.
}

3. マーシャラーとアンマーシャラー

このクラスは、Springのoxmインターフェイス(MarshallerおよびUnmarshaller)を介して変換を処理します。

package com.example.core;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;

public class XMLConverter {

    private Marshaller marshaller;
    private Unmarshaller unmarshaller;

    public Marshaller getMarshaller() {
        return marshaller;
    }

    public void setMarshaller(Marshaller marshaller) {
        this.marshaller = marshaller;
    }

    public Unmarshaller getUnmarshaller() {
        return unmarshaller;
    }

    public void setUnmarshaller(Unmarshaller unmarshaller) {
        this.unmarshaller = unmarshaller;
    }

    public void convertFromObjectToXML(Object object, String filepath)
        throws IOException {

        FileOutputStream os = null;
        try {
            os = new FileOutputStream(filepath);
            getMarshaller().marshal(object, new StreamResult(os));
        } finally {
            if (os != null) {
                os.close();
            }
        }
    }

    public Object convertFromXMLToObject(String xmlfile) throws IOException {

        FileInputStream is = null;
        try {
            is = new FileInputStream(xmlfile);
            return getUnmarshaller().unmarshal(new StreamSource(is));
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

}

4. スプリング構成

SpringのBean構成ファイルで、XMLバインディングフレームワークとしてCastorMarshallerを挿入します。



    
        
        
    
    

5. Test

それを実行します。

package com.example.core;

import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.core.model.Customer;

public class App {
    private static final String XML_FILE_NAME = "customer.xml";

    public static void main(String[] args) throws IOException {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("App.xml");
        XMLConverter converter = (XMLConverter) appContext.getBean("XMLConverter");

        Customer customer = new Customer();
        customer.setName("example");
        customer.setAge(30);
        customer.setFlag(true);
        customer.setAddress("This is address");

        System.out.println("Convert Object to XML!");
        //from object to XML file
        converter.convertFromObjectToXML(customer, XML_FILE_NAME);
        System.out.println("Done \n");

        System.out.println("Convert XML back to Object!");
        //from XML to object
        Customer customer2 = (Customer)converter.convertFromXMLToObject(XML_FILE_NAME);
        System.out.println(customer2);
        System.out.println("Done");

    }
}

出力

Convert Object to XML!
Done

Convert XML back to Object!
Customer [name=example, age=30, flag=true, address=This is address]
Done

次のXMLファイル「customer.xml」がプロジェクトのルートフォルダに生成されます。

ファイル:customer.xml


    
This is address
example

Castor XMLマッピング

待って、なぜフラグと年齢が属性として変換されるのですか? それは、属性または要素として使用するフィールドを制御する方法ですか? もちろん、Castor XML mappingを使用して、オブジェクトとXMLの間の関係を定義できます。

次のマッピングファイルを作成し、プロジェクトクラスパスに配置します。

ファイル:mapping.xml


    

        

        
            
        

        
            
        

        
            
        

        
            
        
    

Spring Bean構成ファイルで、「mappingLocation」を介して上記のmapping.xmlをCastorMarshallerに注入します。



    
        
        
    
    
        
    

もう一度テストすると、XMLファイル「customer.xml」が更新されます。

ファイル:customer.xml


    true
    example
    
This is address

ソースコードをダウンロード

ダウンロード–Spring3-Object-XML-Mapping-Example.zip(7 KB)