ʹÓà XStreamʵÏÖJavaBean Óë XML/JSON Ö®¼äÏ໥ת»»
XML ºÍ JSON Êǵ±½ñ³£ÓõÄÁ½ÖÖÊý¾ÝÃèÊöÓë´«ÊäµÄ¸ñʽ£¬ÌرðÊÇÉæ¼°µ½ JS ʱʹÓà JSON ÆÄΪƵ·±¡£×ÔÈ»£¬ÔÚ Java µÄÊÀ½çÀïÉÙ²»ÁËÍê³É JavaBean ÓëÕâÁ½ÖÖ¸ñʽÏ໥ת»»µÄ×é¼þ£¬ÄǾÍÊÇ XStream ºÍ JSON-lib¡£ÕâÀïÎÒ¼òµ¥¼ÇÏ XStream µÄÓ÷¨¡£
ÆäʵÏàÀàËƵŤ¾ßÔçÒÑÓÐÖ®¡£Èç¹ûÓùý DWR µÄͬ־£¬Ò»¶¨ÓÐÓ¡Ïñ£¬DWR ½øÐÐÔ¶³Ì·½·¨µ÷ÓÃʱҲÄÜΪÄãÍê³É JavaBean ºÍ JSON ¸ñʽµÄË«Ïòת»»µÄ£¬ËùÒÀÀµµÄÊÇËüµÄ¸÷ÖÖ Converter¡£ÔÙÒªÊÇ¶Ô Struts1 µÄϸ½ÚÓÐËù×¢ÒâµÄ»°£¬Struts1 µÄ ActionServlet ÔÚ³õʼ»¯ struts-config.xml ʱÊÇͨ¹ý commons-digester À´Íê³É XML µ½ JavaBean ת»»µÄ¡£ÏàÓ¦µÄ Apache Ò²ÓÐÒ»¸ö commons-betwixt ʵÏÖÁË JavaBean µ½ XML µÄÉú³É¡£
¶øÎÒÕâÀïҪ˵µÄ XStream(http://xstream.codehaus.org) °Ñ JavaBean Óë XML/JSON ¼äµÄË«Ïòת»»Í³Í³ÊµÏÖÁË£¬¶ø JSON-lib ÔòÈçÆäÃû£¬¹¦ÄÜÌ«ÏÔ¼òªÁË¡£ÒªÊ¹Óà XStream£¬ÐèÏÂÔص½ xstream°ü£¬µ±Ç°°æ±¾ÊÇ 1.3.1¡£È»ºó°Ñ xstream-1.x.x.jar Ìí¼Óµ½ÏîÄ¿µÄ Classpath ÉÏ£¬¿É²»ÒÀÀµÓÚÆäËû°ü¡£ÔÚijЩÓÐÒªÇóʱºò²ÅÐèÒªÓõ½ lib Ŀ¼ÖеÄÆäËû°ü£¬ÏÂÃæ»áÌáµ½¡£
¼òµ¥ËµÃ÷ XStream µÄʹÓðɣ¬·ÖΪ JavaBean -> XML¡¢JavaBean -> JSON¡¢ XML -> JavaBean¡¢JSON -> JavaBean ¼¸²¿·ÖÄÚÈÝ¡£ÔÚ¿ªÊ¼Àý×Ó֮ǰ£¬Ïȶ¨ÒåÈý¸öÀà(¶¼ÔÚ com.unmi.model °üÖÐ)£º
public class Customer {
private int custId;
private String custName;
private List<Order> orders;
//setter/getter ºÍ¹¹Ôì·½·¨ÂÔ
}
public class Order {
private int orderId;
private String orderName;
private Product[] products;
//setter/getter ºÍ¹¹Ôì·½·¨ÂÔ
}
public class Product {
private int prodId;  
Ïà¹ØÎĵµ£º
Ò»¸ö¶¨ÒåEmailµÄÀý×Ó
<?xml version="1.0">
<!DOCTYPE message[
<!ELEMENT message(header,body,signature,footer)> --¶¨ÒåÁËmessageµÄ×ÓÔªËØ
<!ELEMENT header(date,from,to,subject,banner)> --header»¹ÓÐ×ÓÔªËØ
<!ELEMENT ......
1£®Ê²Ã´ÊÇXML£¿
XML ¿ª·¢ÕßÃÇ»á¸æËßÄ㣬ÊÂʵÉÏXML²¢²»ÊÇÒ»ÖÖÓïÑÔ£¬¶øÊÇÒ»ÖÖÓÃÀ´¶¨ÒåÆäËüÓïÑÔµÄϵͳ¡£¿ÉÄÜÄãÒѾÌý¹ý£¬»òÐíÉõÖÁ×Ô¼º³¢ÊÔ¹ýһЩÕâÑùµÄÓïÑÔ??±ÈÈç΢ÈíΪÍÆ£¨push£©¼¼ÊõËùÍƳöµÄƵµÀ¶¨Òå¸ñʽ£¨Channel Definition Format£©¡£
W3CÊÇ»¥Á¬ÍøÉÏһЩ¹«Óñê×¼µÄ¿ª·¢Õߣ¬ËûÍƳöÁËһϵÁкÍXMLÏà¹ØµÄ±ê× ......
web.xmlÎļþ¶ÔÓÚÅäÖÃÈκÎJava WebÓ¦Óö¼±ØÐèµÄ¡£
£¨1£©ÅäÖÃStrutsµÄActionServlet
<servlet><!--ÔªËØÀ´ÉùÃ÷ActionServlet-->
<servlet-name><!--ÔªËØ£ºÓÃÀ´¶¨ÒåServletµÄÃû³Æ-->
<servlet-class><!--ÔªËØ:ÓÃÀ´Ö¸¶¨ServletµÄÍêÕûÀàÃû-->
<servlet>
<servlet-name& ......
1¡¢³õʼ»¯
Load µ¼ÈëÒ»¸öXMLÎļþµ½CMarkupµÄ¶ÔÏóÖУ¬²¢¶ÔËü½øÐнâÎö¡£ÀàËÆC#µÄLoad¡£
SetDoc ´Ó×Ö·û´®Öе¼ÈëXMLÊý¾Ý£¬²¢¶ÔËü½âÎö¡£ÀàËÆC#µÄLoadXml¡£
2¡¢Êä³ö
Save ½«XMLÊý¾ÝдÈëÎļþÖС£ÀàËÆC#µÄSave¡£
GetDoc ½«Õû¸öXMLÊý¾ÝÎĵµ×÷Ϊ×Ö·û´®·µ»Ø¡£
3¡¢¸Ä±äµ±Ç°Î»ÖÃ
FindElem ¶¨Î»µ½ÏÂÒ»¸öÔªËØ£¬¿ÉÄܺÍÒ»¸ö±êÇ©Ãû»ò· ......