English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Beispiel für Spring und Castor

Mit CastorMarshaller Klassen, wir können Java-Objekte mit Castor in xml serialisieren und umgekehrt. Es ist eine Implementierungsklasse für die Marshaller- und Unmarshaller-Interfaces. Standardmäßig ist keine zusätzliche Konfiguration erforderlich.

Beispiel für die Integration von Spring und Castor (Java-Objekte in XML serialisieren)

Sie müssen die folgenden Dateien erstellen, um mit Castor Java-Objekte in XML zu serialisieren, die mit Spring verwendet werden:

Employee.java applicationContext.xml mapping.xml Client.java

Erforderliche Jar-Dateien

Um dieses Beispiel auszuführen, müssen Sie laden:

Spring Core jar-Datei Spring Web jar-Datei castor-1.3.jar castor-1.3-core.jar

Laden Sie alle jar-Dateien von Spring herunter, einschließlich core, web, aop, mvc und j2ee, remoting, oxm, jdbc, orm usw. herunter.

Laden Sie castor-1.3.jar

Laden Sie castor -1.3-core.jar


Employee.java

Es werden drei Attribute id, Name und Gehalt durch Setter und Getter definiert.

package com.w;3codebox;
public class Employee {
private int id;
private String name;
private float salary;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public float getSalary() {
    return salary;
}
public void setSalary(float salary) {
    this.salary = salary;
}
}

applicationContext.xml

Es wird ein Bean namens castorMarshallerBean definiert, bei dem die Klasse Employee mit dem OXM-Framework verknüpft ist.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="castorMarshallerBean" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="targetClass" value="com.w3codebox.Employee"></property>
        <property name="mappingLocation" value="mapping.xml"></property>
    </bean>
</beans>

mapping.xml

<?xml version="1.0">
<!DOCTYPE mapping public "-//EXOLAB/EXOLAB 1Castor Mapping DTD Version//.0
                         EN"//"http:/castor.org
 mapping.dtd">
    <mapping3<class name="com.w-codebox.Employee" auto
        complete="true">-<map-to xml="Employee" ns//www.w3uri="http:-codebox.com" ns/prefix="dp"
        >
            <bind-<field name="id" type="integer">/bind-xml>
        </field>
        <field name="id" node="attribute"><
            <bind-<field name="name">/bind-xml>
        </field>
        <field name="salary">
            <bind-<bind xml name="salary" type="float"></bind-xml>
        </field>
        
    </class>
    
 </mapping>

Client.java

Es holt die Instanz des Marshaller aus der Datei applicationContext.xml und ruft die Methode marshal auf.

package com.w;3codebox;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.oxm.Marshaller;
public class Client{
 public static void main(String[] args) throws IOException{
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  Marshaller marshaller = (Marshaller)context.getBean("castorMarshallerBean");
        
  Employee employee=new Employee();
  employee.setId(101);
  employee.setName("Sonoo Jaiswal");
  employee.setSalary(100000);
        
  marshaller.marshal(employee, new StreamResult(new FileWriter("employee.xml")));
  
  System.out.println("XML Created Sucessfully");
 }
}

AusgabedesBeispiels

employee.xml

<?xml version="1.0" encoding="UTF-8"?>
dp:Employee xmlns:dp="http://de.oldtoolbag.com" id="101>
<dp:name>Sonoo Jaiswal</dp:name>
<dp:salary>100000.0</dp:salary>
</dp:Employee>