一个使用自定义命名空间的Schema文件,xml文件和castor生成的java代码例子。
平时只使用到了最简单的xml数据绑定,现在测试一个稍微复杂点的例子。
关于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文件:
<?xml version="1.0" encoding="GB2312"?>
<!-- edited by lizongbo -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:userinfo="user" targetNamespace="user" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="userinfo">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element ref="userinfo:loginifo"/>
<xs:element ref="userinfo:mailinfo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="loginifo">
<xs:annotation>
<xs:documentation>登陆信息</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="username" type="xs:string" default="lizongbo">
<xs:annotation>
<xs:documentation>用户名</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="userpass" type="xs:string" default="lizongbopass">
<xs:annotation>
<xs:documentation>密码</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="mailinfo">
<xs:complexType>
<xs:sequence>
<xs:element name="addr" type="xs:string" default="lizongbo @ gamil.com">
<xs:annotation>
<xs:documentation xml:lang="zh">邮件地址</xs:documentation>
<xs:documentation xml:lang="en">Email Address</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="person" type="xs:string" default="lzb">
<xs:annotation>
<xs:documentation>收件人名字</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
xml文件:
<?xml version="1.0" encoding="GB2312"?>
<!--Sample XML file edited by lizongbo-->
<userinfo xmlns="user" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="user
C:\ntest.xsd">
<loginifo>
<username>lizongbo</username>
<userpass>lizongbopass</userpass>
</loginifo>
<mailinfo>
<addr>lizongbo @ gamil.com</addr>
<person>lzb</person>
</mailinfo>
</userinfo>
java文件Userinfo.java:
(Loginifo.java略去)
/*
* This class was automatically generated with
* <a href="%20castor/'http://www.castor.org" > Castor 0.9.5.3</a>, using an XML
* Schema.
* $Id$
*/
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()
}
--------------------------------------------------
/*
* This class was automatically generated with
* <a href="castor/'http://www.castor.org">Castor 0.9.5.3</a>, using an XML
* Schema.
* $Id$
*/
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()
}