Spring Object/XML-Mapping-Beispiel

Beispiel für eine Spring Object / XML-Zuordnung

Die Objekt- / XML-Zuordnung von Spring konvertiert Objekt in XML oder umgekehrt. Dieser Vorgang ist auch bekannt als

  1. XML Marshalling - Objekt in XML konvertieren.

  2. XML UnMarshalling - XML ​​in Objekt konvertieren.

In diesem Tutorial zeigen wir Ihnen, wie Sie mit Spring's oxm die KonvertierungObject XML durchführen.

Note
Kein Unsinn, warum und welche Vorteile die Verwendung von Spring's oxm hat, lesen Sie diesenofficial Spring Object/XML mappingArtikel.

1. Projektabhängigkeit

Abhängigkeiten in diesem Beispiel.

Note
Spring's Oxm selbst verarbeitet das XML-Marshalling oder UnMarshalling nicht. Es hängt vom Entwickler ab, das bevorzugte XML-Bindungsframework einzufügen. In diesem Fall verwenden Sie das Castor-Bindungsframework.

    
        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. Einfaches Objekt

Ein einfaches Objekt, konvertieren Sie es später in eine XML-Datei.

package com.example.core.model;

public class Customer {

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

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

3. Marshaller und Unmarshaller

Diese Klasse übernimmt die Konvertierung über die Oxm-Schnittstellen von Spring:Marshaller undUnmarshaller.

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. Federkonfiguration

Fügen Sie in der Bean-Konfigurationsdatei von SpringCastorMarshaller als XML-Bindungsframework ein.



    
        
        
    
    

5. Test

Starte es.

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");

    }
}

Ausgabe

Convert Object to XML!
Done

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

Die folgende XML-Datei "customer.xml" wird in Ihrem Projektstammordner generiert.

Datei: customer.xml


    
This is address
example

Castor XML-Zuordnung

Warten Sie, warum Flagge und Alter als Attribut konvertiert werden? Ist dies eine Möglichkeit, um zu steuern, welches Feld als Attribut oder Element verwendet werden soll? Natürlich können SieCastor XML mapping verwenden, um die Beziehung zwischen Objekt und XML zu definieren.

Erstellen Sie die folgende Zuordnungsdatei und fügen Sie sie in Ihren Projektklassenpfad ein.

Datei: Mapping.xml


    

        

        
            
        

        
            
        

        
            
        

        
            
        
    

Fügen Sie in der Spring Bean-Konfigurationsdatei übermapping.xml über „mappingLocation“ mehr alsmapping.xml in CastorMarshaller ein.



    
        
        
    
    
        
    

Testen Sie es erneut, die XML-Datei "customer.xml" wird aktualisiert.

Datei: customer.xml


    true
    example
    
This is address

Quellcode herunterladen

Laden Sie es herunter -Spring3-Object-XML-Mapping-Example.zip (7 KB)