关于xml Schema,有篇好文章:
http://gis.pku.edu.cn/Resources/TR/XML_Schema.doc
通过xml数据绑定可以省下很多写Javabean代码的时间。
一个使用自定义命名空间的Schema文件,xml文件和castor生成的java代码例子。
平时只使用到了最简单的xml数据绑定,现在测试一个稍微复杂点的例子。
关于xml Schema,有篇好文章:
http://gis.pku.edu.cn/Resources/TR/XML_Schema.doc
xsd文件:
http://www.w3.org/2001/XMLSchema" xmlns:userinfo="user" targetNamespace="user" elementFormDefault="qualified" attributeFormDefault="unqualified">
Comment describing your root element
登陆信息
用户名
密码
邮件地址
Email Address
收件人名字
xml文件:
http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="user
C:/ntest.xsd">
lizongbo
lizongbopass
lizongbo @ gamil.com
lzb
java文件Userinfo.java:
(Loginifo.java略去)
package com.lizongbo.;
//---------------------------------/
//- 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;
/**
* Comment describing your root element
*
* @version $Revision$ $Date$
*/
public class Userinfo implements java.io.Serializable {
//--------------------------/
//- Class/Member Variables -/
//--------------------------/
/**
* 登陆信息
*/
private com.lizongbo..Loginifo _loginifo;
/**
* Field _mailinfo
*/
private com.lizongbo..Mailinfo _mailinfo;
//----------------/
//- Constructors -/
//----------------/
public Userinfo() {
super();
} //-- com.lizongbo..Userinfo()
//-----------/
//- Methods -/
//-----------/
/**
* Returns the value of field 'loginifo'. The field 'loginifo'
* has the following description: 登陆信息
*
* @return the value of field 'loginifo'.
*/
public com.lizongbo..Loginifo getLoginifo()
{
return this._loginifo;
} //-- com.lizongbo..Loginifo getLoginifo()
/**
* Returns the value of field 'mailinfo'.
*
* @return the value of field 'mailinfo'.
*/
public com.lizongbo..Mailinfo getMailinfo()
{
return this._mailinfo;
} //-- com.lizongbo..Mailinfo getMailinfo()
/**
* Method isValid
*/
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 'loginifo'. The field 'loginifo' has
* the following description: 登陆信息
*
* @param loginifo the value of field 'loginifo'.
*/
public void setLoginifo(com.lizongbo..Loginifo loginifo)
{
this._loginifo = loginifo;
} //-- void setLoginifo(com.lizongbo..Loginifo)
/**
* Sets the value of field 'mailinfo'.
*
* @param mailinfo the value of field 'mailinfo'.
*/
public void setMailinfo(com.lizongbo..Mailinfo mailinfo)
{
this._mailinfo = mailinfo;
} //-- void setMailinfo(com.lizongbo..Mailinfo)
/**
* Method unmarshal
*
* @param reader
*/
public static java.lang.Object unmarshal(java.io.Reader reader)
throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
{
return (com.lizongbo..Userinfo) Unmarshaller.unmarshal(com.lizongbo..Userinfo.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()
}
--------------------------------------------------
package com.lizongbo.;
//---------------------------------/
//- 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 Mailinfo.
*
* @version $Revision$ $Date$
*/
public class Mailinfo implements java.io.Serializable {
//--------------------------/
//- Class/Member Variables -/
//--------------------------/
/**
* 邮件地址
*/
private java.lang.String _addr = "lizongbo @ gamil.com";
/**
* 收件人名字
*/
private java.lang.String _person = "lzb";
//----------------/
//- Constructors -/
//----------------/
public Mailinfo() {
super();
setAddr("lizongbo @ gamil.com");
setPerson("lzb");
} //-- com.lizongbo..Mailinfo()
//-----------/
//- Methods -/
//-----------/
/**
* Returns the value of field 'addr'. The field 'addr' has the
* following description: 邮件地址
*
* @return the value of field 'addr'.
*/
public java.lang.String getAddr()
{
return this._addr;
} //-- java.lang.String getAddr()
/**
* Returns the value of field 'person'. The field 'person' has
* the following description: 收件人名字
*
* @return the value of field 'person'.
*/
public java.lang.String getPerson()
{
return this._person;
} //-- java.lang.String getPerson()
/**
* Method isValid
*/
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 'addr'. The field 'addr' has the
* following description: 邮件地址
*
* @param addr the value of field 'addr'.
*/
public void setAddr(java.lang.String addr)
{
this._addr = addr;
} //-- void setAddr(java.lang.String)
/**
* Sets the value of field 'person'. The field 'person' has the
* following description: 收件人名字
*
* @param person the value of field 'person'.
*/
public void setPerson(java.lang.String person)
{
this._person = person;
} //-- void setPerson(java.lang.String)
/**
* Method unmarshal
*
* @param reader
*/
public static java.lang.Object unmarshal(java.io.Reader reader)
throws org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException
{
return (com.lizongbo..Mailinfo) Unmarshaller.unmarshal(com.lizongbo..Mailinfo.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()
}