Castor javaBean 与 xml 映射

http://redwave.iteye.com/blog/163913
http://cache.baidu.com/c?m=9d78d513d99212ee1eb0d4690d6788355b16db3f69c3975521dbc90ed5264c40347bfefe62670704a4942f2647f2094bea876b32621e6ae8c5df883c82e8d06a74d47223706bdc074d8d&p=8e77d716d9c41bff57ea913a7f53&user=baidu&fm=sc&query=Castor+Mapping&qid=d267970c057eaf60&p1=1
Castor和数据库 http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f7386c88c7150885ce178e320c051038bef970625559939c3d7a50f35416b5ae2b38200356fdc39bcc198ce8d36e72c821296459db0144dc48fc8a00788137902bfedf1bf0ccf32593df8d809d16089606127af1e7fb5f1765cd7880132693d08e49654861c9fa4413e829003ee95557b737ee9046797081e1dc2c5bb52ec71713&p=c3769a4386cc42ad42fcce655c52&user=baidu&fm=sc&query=Castor+%B0%D1+Mapping+%B7%C5%B5%BD%CA%FD%BE%DD%BF%E2%D6%D0&qid=85c908a405941d5b&p1=1
Mapping: http://tech.ddvip.com/2009-01/1232391830106397_16.html

http://bluelzx.iteye.com/?page=7&show_full=true
类型转换 http://wangrui.iteye.com/category/24492?show_full=true

package test;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Date;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
public class Test {
	public static void main1(String[] args) {   
        David bean = new David();   
        bean.setAge(24);   
        bean.setName("David");   
        bean.setBorndate(new Date());   
        try {   
            // write it out as XML   
            File file = new File("D:/David.xml");   
            Writer writer = new FileWriter(file);   
            Marshaller.marshal(bean, writer);
            // now restore the value and list what we get   
            Reader reader = new FileReader(file);   
            David read = (David) Unmarshaller.unmarshal(David.class, reader);   
            System.out.println("David: " + read.getName() + read.getBorndate()   
                    + read.getAge());   
        } catch (IOException ex) {   
            ex.printStackTrace(System.err);   
        } catch (MarshalException ex) {   
            ex.printStackTrace(System.err);   
        } catch (ValidationException ex) {   
            ex.printStackTrace(System.err);   
        }   
    }
	
	public static void main(String[] args) throws MarshalException, ValidationException, IOException {
		QQ  qq = new QQ();
		qq.setId(1);
		qq.setName("QQ");
		qq.setStrName("BB");
		
		Marshaller.marshal(qq,new FileWriter("D:/QQ.xml"));
		Reader reader = new FileReader("D:/QQ.xml");
		QQ qq2 = (QQ) Unmarshaller.unmarshal(QQ.class,reader);
		System.out.println("QQ2:"+qq2.getId()+"--"+qq2.getName()+"---"+qq2.getStrName());
		reader.close();
	}

}




public static void main(String[] args) {
		QQ  qq = new QQ();
		qq.setId(1);
		qq.setName("123");
		qq.setStrName("abc");
		
		try {
			FileWriter writer =new FileWriter("D:/q.xml");
			/*Marshaller.marshal(qq,new DocumentHandler() {
				public void startElement(String name, AttributeList atts)
						throws SAXException {
					System.out.println("元素:"+name);
				}
				
				public void startDocument() throws SAXException {
					System.out.println("开始解析");
				}
				
				public void setDocumentLocator(Locator locator) {
					System.out.println("Locator");
				}
				
				public void processingInstruction(String target, String data)
						throws SAXException {
					System.out.println("processingInstrucion");
				}
				
				public void ignorableWhitespace(char[] ch, int start, int length)
						throws SAXException {
					System.out.println("ignorableWhitespace");
				}
				
				public void endElement(String name) throws SAXException {
					System.out.println("end"+name);
				}
				
				public void endDocument() throws SAXException {
					System.out.println("解析完毕");
				}
				
				public void characters(char[] ch, int start, int length)
						throws SAXException {
					System.out.println("---"+new String(ch,start,length));
				}
			});*/
			
			Writer writer2 = new StringWriter();
			Marshaller.marshal(qq,writer2);
			System.out.println(writer2.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



总结:
对象必须是javaBean,用集合测试是不可以

//通过Map 文件
	public static void main(String[] args) {
//InputStream in = new ByteArrayInputStream(str.getBytes());
//	InputSource inputSource = new InputSource(in);
		QQ   qq = new QQ();
		qq.setId(1);
		qq.setName("123");
		try {
			Mapping mapping =new Mapping();
			mapping.loadMapping("D:/map.xml");
			Writer writer = new FileWriter("D:/qq.xml");
			Marshaller  marshaller =new Marshaller(writer);
			marshaller.setMapping(mapping);
			marshaller.marshal(qq);
			
			
			Reader reader  = new FileReader("D:/qq.xml");
			Unmarshaller unmarshaller =new Unmarshaller(mapping);
			QQ qq1=(QQ) unmarshaller.unmarshal(QQ.class,reader);
			System.out.println("id="+qq.getId()+"--"+qq.getName());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


<!DOCTYPE databases PUBLIC
"-//EXOLAB/Castor Mapping DTD Version 1.0//EN"
"http://castor.exolab.org/mapping.dtd">
<mapping>
	<description>Collection mapping example</description>	
<class name="test.QQ" auto-complete="true">
	<field name="id">
	<bind-xml name="id" node="attribute"/>
	</field>
	<field name="name">
		<bind-xml name="strName" node="attribute"/>
	</field>
</class>

<class name="test.QQ" auto-complete="true">
	<field name="id">
	<bind-xml name="id" node="attribute"/>
	</field>
	<field name="name">
		<bind-xml name="strName" node="attribute"/>
	</field>
</class>
</mapping> 


你可能感兴趣的:(java,xml,bean,qq,Blog)