ʹÓà 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;
private String prodName;
private double prodPrice;
//setter/getter ºÍ¹¹Ôì·½·¨ÂÔ
}
Customer/Order/Product£¬ËüÃÇÖ®¼äµÄ¹Øϵ£¬ÒÀ´ÎÊÇÒ»¶Ô¶à¡¢Ò»¶Ô¶àµÄ£¬ÎªÑÝʾĿµÄ£¬·Ö±ðÓÃÁË List ºÍÊý×é×÷Ϊ¾ÛºÏÊôÐÔ¡£
1. JavaBean -> XML
public static void main(String[] args) {
//¹¹Ôì½Ó½üʵ¼ÊµÄ Customer ¶ÔÏó
Product p1 = new Product(1001,"µçÄÔ",4000);
Product p2 = new Product(1002,"¿Õµ÷",2000);
Product[] prods1 = new Product[]{p1,p2};
Order order1 =
Ïà¹ØÎĵµ£º
XmlDocument doc = new XmlDocument();
string strtxt = "";
doc.Load(Server.MapPath("XMLFile1.xml"));
&nbs ......
Îĵµ¼°´úÂëÏÂÔØ£ºhttp://www.dingos.cn/index.php?topic=1899.0
½éÉÜ
µ±²Ëµ¥ÏîÊǾ³£¸Ä±äʱ£¬´ÓXMLÎĵµÖд´½¨TreeView²Åµ±ÊǷdz£ÓÐÓõġ£ÀýÈ磬ʹÓÃXML×÷ΪÊý¾Ý¿â´æ´¢¼Ç¼¡£
ÕâÀïÓиö¼òµ¥Ê¾Àý¡£ÒÔ·À»ìÏýÕâ¸öÀý×ӱȽϼòµ¥¡£
±³¾°
¶ÔXML¡¢TreeView¿Ø¼þµÄÓ¦Óã¬ÔÚVisual StudioÖÐÓÐÖúÓÚÀí½âÕâЩ²½Öè¡£
ʹÓôúÂë
×¢Ò⣠......
´Ë²¿·ÖÔ´ÂëΪ×îºËÐIJ¿·Ö´úÂ룡
package com.etong.allen;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
impor ......
--±¾ÎÄÕª×Ôhttp://www.moandroid.com/?p=508
By: º£ÊÐò×Â¥ | In: Android¿ª·¢
3 ¾Å 2009
XML ¾³£ÓÃ×÷ Internet ÉϵÄÒ»ÖÖÊý¾Ý¸ñʽ£¬ÆäÎļþ¸ñʽÏë±Ø´ó¼Ò¶¼±È½ÏÇå³þ£¬ÔÚÕâÀïÎÒ½áºÏAndroidƽ̨£¬À´ËµÃ÷Android SDKÌṩµÄ¶ÁдXMLµÄpackage¡£
Ê×ÏȽéÉÜÏÂAndroid SDKÓëJava SDKÔÚ¶ÁдXMLÎļþ·½Ã棬Êý¾Ý°üÖ®¼äµÄ¹Øϵ¡£Android ......
´´½¨ÎĵµÀàÐÍÉùÃ÷
Ò»°ã¶øÑÔ£¬XMLÉùÃ÷·ÅÔÚÎĵµ¶¥²¿¡£ÔÚPHPÖÐÉùÃ÷Ê®·Ö¼òµ¥£ºÖ»ÐèʵÀý»¯Ò»¸öDOMÎĵµÀàµÄ¶ÔÏ󲢸³ÓèËüÒ»¸ö°æ±¾ºÅ¡£²é¿´³ÌÐòÇåµ¥A£º
³ÌÐòÇåµ¥ A
<?php
// create doctype
$dom = new DOMDocument("1.0");
// display document in browser as plain text
// display document in browser as plain text ......