Java里面对XML文档的处理【经典例子分析】

17 篇文章 0 订阅
2 篇文章 0 订阅

        Java里面对XML文档的处理,网络上有很多,最近因为用到,翻看了JBuilde里面的处理例子,觉得蛮好的,说一些使用心得分析。

       例子的地址位于%Builder2006%/samples/Tutorials/XML/databinding/fromSchema下面,可以直接打开工程文件Castor.jpx。刚打开的时候,只有一个xsd文件和一个xml文件。这两个分别是定义XML文档的样式,还有例子的,如果看不太明白,可以从晚上搜索一下XML的相关介绍,了解这两个文件的作用,在此就不赘述了。

      JBuilder有很多自动的功能,下面就是一个很好的应用:照着下图一步步来。

 

按下按钮,会出现选择下一步的对话框,一路默认下去好了。点击完成之后,就会自动生成几个Java文件。

这就是自动生成的对XML文件进行处理的Java类了。这几个类也没什么特别,主要是应用了JBuilder 的一个XML处理类,这个类的地址在%JBuilder%/lib/castor-xml.jar这个文件里面的方法。

Employees.xsd文件内容
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 elementFormDefault="qualified">
 <xsd:element name="DeptNo" type="xsd:string" />
 <xsd:element name="EmpNo" type="xsd:string" />
 <xsd:element name="FirstName" type="xsd:string" />
 <xsd:element name="FullName" type="xsd:string" />
 <xsd:element name="HireDate" type="xsd:string" />
 <xsd:element name="JobCode" type="xsd:string" />
 <xsd:element name="JobCountry" type="xsd:string" />
 <xsd:element name="JobGrade" type="xsd:string" />
 <xsd:element name="LastName" type="xsd:string" />
 <xsd:element name="PhoneExt" type="xsd:string" />
 <xsd:element name="Salary" type="xsd:string" />
 <xsd:complexType name="XmlEmployeeType">
  <xsd:sequence>
   <xsd:element ref="EmpNo" />
   <xsd:element ref="FirstName" />
   <xsd:element ref="LastName" />
   <xsd:element ref="PhoneExt" />
   <xsd:element ref="HireDate" />
   <xsd:element ref="DeptNo" />
   <xsd:element ref="JobCode" />
   <xsd:element ref="JobGrade" />
   <xsd:element ref="JobCountry" />
   <xsd:element ref="Salary" />
   <xsd:element ref="FullName" />
  </xsd:sequence>
 </xsd:complexType>
 <xsd:element name="XmlEmployees">
  <xsd:complexType>
   <xsd:sequence>
    <xsd:element name="XmlEmployee" type="XmlEmployeeType"
     maxOccurs="unbounded" />
   </xsd:sequence>
  </xsd:complexType>
 </xsd:element>
</xsd:schema>
由此生成的Employees.xml文件:

<?xml version="1.0"?>
<XmlEmployees>
    <XmlEmployee>
        <EmpNo>2</EmpNo>
        <FirstName>Robert</FirstName>
        <LastName>Nelson</LastName>
        <PhoneExt>250</PhoneExt>
        <HireDate>1988-12-28</HireDate>
        <DeptNo>600</DeptNo>
        <JobCode>VP</JobCode>
        <JobGrade>2</JobGrade>
        <JobCountry>USA</JobCountry>
        <Salary>105900.000000</Salary>
        <FullName>Nelson, Robert</FullName>
    </XmlEmployee>
    <XmlEmployee>
        <EmpNo>4</EmpNo>
        <FirstName>Bruce</FirstName>
        <LastName>Young</LastName>
        <PhoneExt>233</PhoneExt>
        <HireDate>1988-12-28</HireDate>
        <DeptNo>621</DeptNo>
        <JobCode>Eng</JobCode>
        <JobGrade>2</JobGrade>
        <JobCountry>USA</JobCountry>
        <Salary>97500.000000</Salary>
        <FullName>Young, Bruce</FullName>
    </XmlEmployee>
    <XmlEmployee>
        <EmpNo>5</EmpNo>
        <FirstName>Kim</FirstName>
        <LastName>Lambert</LastName>
        <PhoneExt>22</PhoneExt>
        <HireDate>1989-02-06</HireDate>
        <DeptNo>130</DeptNo>
        <JobCode>Eng</JobCode>
        <JobGrade>2</JobGrade>
        <JobCountry>USA</JobCountry>
        <Salary>102750.000000</Salary>
        <FullName>Lambert, Kim</FullName>
    </XmlEmployee>
</XmlEmployees>

对应处理类:
Employees.java
/**
 * Usage:
 *
 * FileName   : XmlEmployees.java
 * PackageName: org.ahpo.xml
 * Author     : Ahpo Yang
 * CreateDate : 2006-4-24 10:59:29
 */
package org.ahpo.xml;
//---------------------------------/
//- Imported classes and packages -/
//---------------------------------/

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Vector;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.ContentHandler;

/**
* Class XmlEmployees.
*
* @version $Revision$ $Date$
*/
public class XmlEmployees implements java.io.Serializable {


     //--------------------------/
    //- Class/Member Variables -/
   //--------------------------/

   /**
    * Field _xmlEmployeeList
    */
   private java.util.Vector _xmlEmployeeList;


     //----------------/
    //- Constructors -/
   //----------------/

   public XmlEmployees() {
       super();
       _xmlEmployeeList = new Vector();
   } //-- XmlEmployees()


     //-----------/
    //- Methods -/
   //-----------/

   /**
    * Method addXmlEmployee
    *
    *
    *
    * @param vXmlEmployee
    */
   public void addXmlEmployee(XmlEmployee vXmlEmployee)
       throws java.lang.IndexOutOfBoundsException
   {
       _xmlEmployeeList.addElement(vXmlEmployee);
   } //-- void addXmlEmployee(XmlEmployee)

   /**
    * Method addXmlEmployee
    *
    *
    *
    * @param index
    * @param vXmlEmployee
    */
   public void addXmlEmployee(int index, XmlEmployee vXmlEmployee)
       throws java.lang.IndexOutOfBoundsException
   {
       _xmlEmployeeList.insertElementAt(vXmlEmployee, index);
   } //-- void addXmlEmployee(int, XmlEmployee)

   /**
    * Method enumerateXmlEmployee
    *
    *
    *
    * @return Enumeration
    */
   public java.util.Enumeration enumerateXmlEmployee()
   {
       return _xmlEmployeeList.elements();
   } //-- java.util.Enumeration enumerateXmlEmployee()

   /**
    * Method getXmlEmployee
    *
    *
    *
    * @param index
    * @return XmlEmployee
    */
   public XmlEmployee getXmlEmployee(int index)
       throws java.lang.IndexOutOfBoundsException
   {
       //-- check bounds for index
       if ((index < 0) || (index > _xmlEmployeeList.size())) {
           throw new IndexOutOfBoundsException();
       }
      
       return (XmlEmployee) _xmlEmployeeList.elementAt(index);
   } //-- XmlEmployee getXmlEmployee(int)

   /**
    * Method getXmlEmployee
    *
    *
    *
    * @return XmlEmployee
    */
   public XmlEmployee[] getXmlEmployee()
   {
       int size = _xmlEmployeeList.size();
       XmlEmployee[] mArray = new XmlEmployee[size];
       for (int index = 0; index < size; index++) {
           mArray[index] = (XmlEmployee) _xmlEmployeeList.elementAt(index);
       }
       return mArray;
   } //-- XmlEmployee[] getXmlEmployee()

   /**
    * Method getXmlEmployeeCount
    *
    *
    *
    * @return int
    */
   public int getXmlEmployeeCount()
   {
       return _xmlEmployeeList.size();
   } //-- int getXmlEmployeeCount()

   /**
    * Method isValid
    *
    *
    *
    * @return boolean
    */
   public boolean isValid()
   {
       try {
           validate();
       }
       catch (org.exolab.castor.xml.ValidationException vex) {
           return false;
       }
       return true;
   } //-- boolean isValid()

   /**
    * Method marshal
    *
    *
    *
    * @param out
    */
   public void marshal(java.io.Writer out)
       throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
   {
      
       Marshaller.marshal(this, out);
   } //-- void marshal(java.io.Writer)

   /**
    * Method marshal
    *
    *
    *
    * @param handler
    */
   public void marshal(org.xml.sax.ContentHandler handler)
       throws java.io.IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
   {
      
       Marshaller.marshal(this, handler);
   } //-- void marshal(org.xml.sax.ContentHandler)

   /**
    * Method removeAllXmlEmployee
    *
    */
   public void removeAllXmlEmployee()
   {
       _xmlEmployeeList.removeAllElements();
   } //-- void removeAllXmlEmployee()

   /**
    * Method removeXmlEmployee
    *
    *
    *
    * @param index
    * @return XmlEmployee
    */
   public XmlEmployee removeXmlEmployee(int index)
   {
       java.lang.Object obj = _xmlEmployeeList.elementAt(index);
       _xmlEmployeeList.removeElementAt(index);
       return (XmlEmployee) obj;
   } //-- XmlEmployee removeXmlEmployee(int)

   /**
    * Method setXmlEmployee
    *
    *
    *
    * @param index
    * @param vXmlEmployee
    */
   public void setXmlEmployee(int index, XmlEmployee vXmlEmployee)
       throws java.lang.IndexOutOfBoundsException
   {
       //-- check bounds for index
       if ((index < 0) || (index > _xmlEmployeeList.size())) {
           throw new IndexOutOfBoundsException();
       }
       _xmlEmployeeList.setElementAt(vXmlEmployee, index);
   } //-- void setXmlEmployee(int, XmlEmployee)

   /**
    * Method setXmlEmployee
    *
    *
    *
    * @param xmlEmployeeArray
    */
   public void setXmlEmployee(XmlEmployee[] xmlEmployeeArray)
   {
       //-- copy array
       _xmlEmployeeList.removeAllElements();
       for (int i = 0; i < xmlEmployeeArray.length; i++) {
           _xmlEmployeeList.addElement(xmlEmployeeArray[i]);
       }
   } //-- void setXmlEmployee(XmlEmployee)

   /**
    * Method unmarshal
    *
    *
    *
    * @param reader
    * @return Object
    */
   public static java.lang.Object unmarshal(java.io.Reader reader)
       throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
   {
       return (XmlEmployees) Unmarshaller.unmarshal(XmlEmployees.class, reader);
   } //-- java.lang.Object unmarshal(java.io.Reader)

   /**
    * Method validate
    *
    */
   public void validate()
       throws org.exolab.castor.xml.ValidationException
   {
       org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
       validator.validate(this);
   } //-- void validate()
}
Employee.java:
/**
 * Usage:
 *
 * FileName   : XmlEmployee.java
 * PackageName: org.ahpo.xml
 * Author     : Ahpo Yang
 * CreateDate : 2006-4-24 11:05:30
 */
package org.ahpo.xml;
/*
 * This class was automatically generated with
 * <a href="http://www.castor.org">Castor 0.9.6</a>, using an XML
 * Schema.
 * $Id$
 */

  //---------------------------------/
 //- Imported classes and packages -/
//---------------------------------/

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.ContentHandler;

/**
 * Class XmlEmployee.
 *
 * @version $Revision$ $Date$
 */
public class XmlEmployee extends XmlEmployeeType
implements java.io.Serializable
{


      //----------------/
     //- Constructors -/
    //----------------/

    public XmlEmployee() {
        super();
    } //-- com.borland.samples.xml.databinding.castor.XmlEmployee()


      //-----------/
     //- Methods -/
    //-----------/

    /**
     * Method isValid
     *
     *
     *
     * @return boolean
     */
    public boolean isValid()
    {
        try {
            validate();
        }
        catch (org.exolab.castor.xml.ValidationException vex) {
            return false;
        }
        return true;
    } //-- boolean isValid()

    /**
     * Method marshal
     *
     *
     *
     * @param out
     */
    public void marshal(java.io.Writer out)
        throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
    {
       
        Marshaller.marshal(this, out);
    } //-- void marshal(java.io.Writer)

    /**
     * Method marshal
     *
     *
     *
     * @param handler
     */
    public void marshal(org.xml.sax.ContentHandler handler)
        throws java.io.IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
    {
       
        Marshaller.marshal(this, handler);
    } //-- void marshal(org.xml.sax.ContentHandler)

    /**
     * Method unmarshal
     *
     *
     *
     * @param reader
     * @return Object
     */
    public static java.lang.Object unmarshal(java.io.Reader reader)
        throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
    {
        return (XmlEmployee) Unmarshaller.unmarshal(XmlEmployee.class, reader);
    } //-- java.lang.Object unmarshal(java.io.Reader)

    /**
     * Method validate
     *
     */
    public void validate()
        throws org.exolab.castor.xml.ValidationException
    {
        org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
        validator.validate(this);
    } //-- void validate()

}

EmployeeType.java
/**
 * Usage:
 *
 * FileName   : XmlEmployeeType.java
 * PackageName: org.ahpo.xml
 * Author     : Ahpo Yang
 * CreateDate : 2006-4-24 11:06:11
 */
package org.ahpo.xml;

/*
 * This class was automatically generated with
 * <a href="http://www.castor.org">Castor 0.9.6</a>, using an XML
 * Schema.
 * $Id$
 */

  //---------------------------------/
 //- Imported classes and packages -/
//---------------------------------/

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.ContentHandler;

/**
 * Class XmlEmployeeType.
 *
 * @version $Revision$ $Date$
 */
public class XmlEmployeeType implements java.io.Serializable {


      //--------------------------/
     //- Class/Member Variables -/
    //--------------------------/

    /**
     * Field _empNo
     */
    private java.lang.String _empNo;

    /**
     * Field _firstName
     */
    private java.lang.String _firstName;

    /**
     * Field _lastName
     */
    private java.lang.String _lastName;

    /**
     * Field _phoneExt
     */
    private java.lang.String _phoneExt;

    /**
     * Field _hireDate
     */
    private java.lang.String _hireDate;

    /**
     * Field _deptNo
     */
    private java.lang.String _deptNo;

    /**
     * Field _jobCode
     */
    private java.lang.String _jobCode;

    /**
     * Field _jobGrade
     */
    private java.lang.String _jobGrade;

    /**
     * Field _jobCountry
     */
    private java.lang.String _jobCountry;

    /**
     * Field _salary
     */
    private java.lang.String _salary;

    /**
     * Field _fullName
     */
    private java.lang.String _fullName;


      //----------------/
     //- Constructors -/
    //----------------/

    public XmlEmployeeType() {
        super();
    } //-- com.borland.samples.xml.databinding.castor.XmlEmployeeType()


      //-----------/
     //- Methods -/
    //-----------/

    /**
     * Returns the value of field 'deptNo'.
     *
     * @return String
     * @return the value of field 'deptNo'.
     */
    public java.lang.String getDeptNo()
    {
        return this._deptNo;
    } //-- java.lang.String getDeptNo()

    /**
     * Returns the value of field 'empNo'.
     *
     * @return String
     * @return the value of field 'empNo'.
     */
    public java.lang.String getEmpNo()
    {
        return this._empNo;
    } //-- java.lang.String getEmpNo()

    /**
     * Returns the value of field 'firstName'.
     *
     * @return String
     * @return the value of field 'firstName'.
     */
    public java.lang.String getFirstName()
    {
        return this._firstName;
    } //-- java.lang.String getFirstName()

    /**
     * Returns the value of field 'fullName'.
     *
     * @return String
     * @return the value of field 'fullName'.
     */
    public java.lang.String getFullName()
    {
        return this._fullName;
    } //-- java.lang.String getFullName()

    /**
     * Returns the value of field 'hireDate'.
     *
     * @return String
     * @return the value of field 'hireDate'.
     */
    public java.lang.String getHireDate()
    {
        return this._hireDate;
    } //-- java.lang.String getHireDate()

    /**
     * Returns the value of field 'jobCode'.
     *
     * @return String
     * @return the value of field 'jobCode'.
     */
    public java.lang.String getJobCode()
    {
        return this._jobCode;
    } //-- java.lang.String getJobCode()

    /**
     * Returns the value of field 'jobCountry'.
     *
     * @return String
     * @return the value of field 'jobCountry'.
     */
    public java.lang.String getJobCountry()
    {
        return this._jobCountry;
    } //-- java.lang.String getJobCountry()

    /**
     * Returns the value of field 'jobGrade'.
     *
     * @return String
     * @return the value of field 'jobGrade'.
     */
    public java.lang.String getJobGrade()
    {
        return this._jobGrade;
    } //-- java.lang.String getJobGrade()

    /**
     * Returns the value of field 'lastName'.
     *
     * @return String
     * @return the value of field 'lastName'.
     */
    public java.lang.String getLastName()
    {
        return this._lastName;
    } //-- java.lang.String getLastName()

    /**
     * Returns the value of field 'phoneExt'.
     *
     * @return String
     * @return the value of field 'phoneExt'.
     */
    public java.lang.String getPhoneExt()
    {
        return this._phoneExt;
    } //-- java.lang.String getPhoneExt()

    /**
     * Returns the value of field 'salary'.
     *
     * @return String
     * @return the value of field 'salary'.
     */
    public java.lang.String getSalary()
    {
        return this._salary;
    } //-- java.lang.String getSalary()

    /**
     * Method isValid
     *
     *
     *
     * @return boolean
     */
    public boolean isValid()
    {
        try {
            validate();
        }
        catch (org.exolab.castor.xml.ValidationException vex) {
            return false;
        }
        return true;
    } //-- boolean isValid()

    /**
     * Method marshal
     *
     *
     *
     * @param out
     */
    public void marshal(java.io.Writer out)
        throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
    {
       
        Marshaller.marshal(this, out);
    } //-- void marshal(java.io.Writer)

    /**
     * Method marshal
     *
     *
     *
     * @param handler
     */
    public void marshal(org.xml.sax.ContentHandler handler)
        throws java.io.IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
    {
       
        Marshaller.marshal(this, handler);
    } //-- void marshal(org.xml.sax.ContentHandler)

    /**
     * Sets the value of field 'deptNo'.
     *
     * @param deptNo the value of field 'deptNo'.
     */
    public void setDeptNo(java.lang.String deptNo)
    {
        this._deptNo = deptNo;
    } //-- void setDeptNo(java.lang.String)

    /**
     * Sets the value of field 'empNo'.
     *
     * @param empNo the value of field 'empNo'.
     */
    public void setEmpNo(java.lang.String empNo)
    {
        this._empNo = empNo;
    } //-- void setEmpNo(java.lang.String)

    /**
     * Sets the value of field 'firstName'.
     *
     * @param firstName the value of field 'firstName'.
     */
    public void setFirstName(java.lang.String firstName)
    {
        this._firstName = firstName;
    } //-- void setFirstName(java.lang.String)

    /**
     * Sets the value of field 'fullName'.
     *
     * @param fullName the value of field 'fullName'.
     */
    public void setFullName(java.lang.String fullName)
    {
        this._fullName = fullName;
    } //-- void setFullName(java.lang.String)

    /**
     * Sets the value of field 'hireDate'.
     *
     * @param hireDate the value of field 'hireDate'.
     */
    public void setHireDate(java.lang.String hireDate)
    {
        this._hireDate = hireDate;
    } //-- void setHireDate(java.lang.String)

    /**
     * Sets the value of field 'jobCode'.
     *
     * @param jobCode the value of field 'jobCode'.
     */
    public void setJobCode(java.lang.String jobCode)
    {
        this._jobCode = jobCode;
    } //-- void setJobCode(java.lang.String)

    /**
     * Sets the value of field 'jobCountry'.
     *
     * @param jobCountry the value of field 'jobCountry'.
     */
    public void setJobCountry(java.lang.String jobCountry)
    {
        this._jobCountry = jobCountry;
    } //-- void setJobCountry(java.lang.String)

    /**
     * Sets the value of field 'jobGrade'.
     *
     * @param jobGrade the value of field 'jobGrade'.
     */
    public void setJobGrade(java.lang.String jobGrade)
    {
        this._jobGrade = jobGrade;
    } //-- void setJobGrade(java.lang.String)

    /**
     * Sets the value of field 'lastName'.
     *
     * @param lastName the value of field 'lastName'.
     */
    public void setLastName(java.lang.String lastName)
    {
        this._lastName = lastName;
    } //-- void setLastName(java.lang.String)

    /**
     * Sets the value of field 'phoneExt'.
     *
     * @param phoneExt the value of field 'phoneExt'.
     */
    public void setPhoneExt(java.lang.String phoneExt)
    {
        this._phoneExt = phoneExt;
    } //-- void setPhoneExt(java.lang.String)

    /**
     * Sets the value of field 'salary'.
     *
     * @param salary the value of field 'salary'.
     */
    public void setSalary(java.lang.String salary)
    {
        this._salary = salary;
    } //-- void setSalary(java.lang.String)

    /**
     * Method unmarshal
     *
     *
     *
     * @param reader
     * @return Object
     */
    public static java.lang.Object unmarshal(java.io.Reader reader)
        throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
    {
        return (XmlEmployeeType) Unmarshaller.unmarshal(XmlEmployeeType.class, reader);
    } //-- java.lang.Object unmarshal(java.io.Reader)

    /**
     * Method validate
     *
     */
    public void validate()
        throws org.exolab.castor.xml.ValidationException
    {
        org.exolab.castor.xml.Validator validator = new org.exolab.castor.xml.Validator();
        validator.validate(this);
    } //-- void validate()

}

测试类:
/**
 * Copyright (c) 1996-2004 Borland Software Corporation.  All Rights Reserved.
 *
 * This SOURCE CODE FILE, which has been provided by Borland Software as part
 * of a Borland Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland Software. 
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */
package org.ahpo.xml;

/**
 * Title:        XML Tutorial
 * Description:  XML Tutorial for JBuilder
 * Company:      Borland Software Coporation
 * @author
 * @version 1.0
 */
import java.io.*;
import org.exolab.castor.xml.*;
public class DB_Castor {

  public DB_Castor() {
  }

  public static void main(String[] args) {
    try {
       String fileName = "E://javawork//ahpo//src//java//org//ahpo//xml//Employees.xml";
       System.out.println("== unmarshalling /"" + fileName + "/" ==");
       //Unmarshal XML file.
       XmlEmployees xmlEmployees = (XmlEmployees)XmlEmployees.unmarshal(new FileReader(fileName));
       System.out.println("Total Number of XmlEmployees read = " + xmlEmployees.getXmlEmployeeCount());
       System.out.println("First XmlEmployee's Full Name is " + xmlEmployees.getXmlEmployee(0).getFullName());
       System.out.println("Last XmlEmployee's Full Name is " + xmlEmployees.getXmlEmployee(xmlEmployees.getXmlEmployeeCount()-1).getFullName());

       // Add an XmlEmployee.
       xmlEmployees.addXmlEmployee(getXmlEmployee("8000","400","Charlie","Castor","3/3/2001","VP","USA","2","1993","155000.00"));

       // Modify the last XmlEmployee.
       xmlEmployees.setXmlEmployee(xmlEmployees.getXmlEmployeeCount()-1, getXmlEmployee("8000","600","Peter","Castor","3/3/2001","VP","USA","3","2096","125000.00"));

       // Marshal out the data to the same XML file.
       xmlEmployees.marshal(new java.io.FileWriter(fileName));
      }
    catch (Exception ex) {
      ex.printStackTrace();
    }

  }


  private static XmlEmployee getXmlEmployee(String XmlEmployeeNumber,String departmentNumber,String firstName,String lastName,
                            String hireDate,String jobCode,String jobCountry,String jobGrade,
                            String phoneExt,String salary){


    XmlEmployee xmlEmployee = new XmlEmployee();
    xmlEmployee.setEmpNo(XmlEmployeeNumber);
    xmlEmployee.setDeptNo(departmentNumber);
    xmlEmployee.setFirstName(firstName);
    xmlEmployee.setLastName(lastName);
    xmlEmployee.setFullName(lastName+", "+firstName);
    xmlEmployee.setHireDate(hireDate);
    xmlEmployee.setJobCode(jobCode);
    xmlEmployee.setJobCountry(jobCountry);
    xmlEmployee.setJobGrade(jobGrade);
    xmlEmployee.setPhoneExt(phoneExt);
    xmlEmployee.setSalary(salary);
    return xmlEmployee;
  }
}

上面程序的运行,还需要几个公用包的支持,如:


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值