Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

XStream xml json ת»»

1¡¢Ê×ÏÈÏÂÔØ xstream.jarºÍjettison.jar£¨×ª»»ÎªjsonʱÓõ½£©£¬²¢ÒýÈë¸Ã°ü¡£
2¡¢¿´ÈçÏ´úÂë°É£º
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class Student {
private String name;
private String sex;
/*
* ¹¹Ô캯Êý
*/
public Student(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
/*
* ¶ÔÏóת»»Îª xml
* @return String
*/
public String toXml() {
XStream xStream = new XStream(new DomDriver());
return xStream.toXML(this);
}
/*
* ¶ÔÏóת»»Îªjson
* @return String
*/
public String toJson() {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
return xStream.toXML(this);
}
/*
* xmlÀàת»»Îª¶ÔÏó
* @param String
* @return Student
*/
public Student toXmlObject(String xml) {
XStream xStream = new XStream(new DomDriver());
return (Student) xStream.fromXML(xml);
}
/*
* jsonת»»Îª¶ÔÏó
* @param String
* @return Student
*/
public Student toJsonObject(String xml) {
XStream xStream = new XStream(new JettisonMappedXmlDriver());
return (Student) xStream.fromXML(xml);
}
public static void main(String[] arge) throws IOException {
Student student = new Student("ÕÅÈý", "ÄÐ");
File file = new File("d:/student.xml");
// xml: Êä³ö ת»»Îª xml
FileUtils.writeStringToFile(file, student.toXml());
// xml: ÊäÈëת»»Îª¶ÔÏó
String in = FileUtils.readFileToString(file);
Student st = student.toXmlObject(in);
// json: Êä³ö ת»»Îªjson
FileUtils.writeStringToFile(file, student.toJson());
// json: ÊäÈëת»»Îª¶ÔÏó
in = FileUtils.re


Ïà¹ØÎĵµ£º

¼òµ¥µÄXMLÎļþ¶Áд £¨×ª£©

×¼±¸
¿ª·¢¹¤¾ß£ºVS2005£¬
Ç°Ì᣺°²×°msxml6½âÎöÆ÷£¬msÍøÕ¾¿ÉÏÂÔØ
ͨ¹ýÏòµ¼´´½¨VC++ MFC ¹¤³Ì£¬»ùÓÚ¶Ô»°¿ò
Ìí¼ÓÒýÓÃ:
//ÔÚStdAfx.hÖÐÒýÈ붯̬Á´½Ó¿â MSXML.DLL(C:\windows\system32\msxml6.dll)
#import <msxml6.dll>
±à¼­×ÊÔ´£¬Ìí¼Ó¿Ø¼þ
ÔÚbookid¶ÔÓ¦µÄÎı¾¿òÓÒ¼ü£¬Ìí¼Ó±äÁ¿°ó¶¨£¨ÈçÏÂͼ£¬°ó¶¨¿Ø¼þÖµ£¬±äÁ¿Ã ......

xml½âÎö¶àÖؽڵã

xmlΪ£º
<friendShares>
<totalCount>352</totalCount>
<friendShare>
<code>XXXXXXX</code>
<date>2010-01-15T00:00:00+08:00</date>
<friendId>499</friendId>
<movie>
<code>XXXXXX</code>
<contentId>89718</content ......

C# ²Ù×÷XML£¬×Ô¼ºÓùýµÄ´úÂ룬¿ÉÄÜÖ»ÓÐÎÒÄÜ¿´¶®

public string WriteXML(string[] values, int flag)
{

//Èç¹ûflag==0ÔòΪµÚÒ»´ÎÔËÐÐÐèÒª³õʼ»¯XMLÎļþ
if (flag == 0)
{
//ÉúÔÚËæ»úÎļþÃû
string dateName = System.DateTime.Now.ToString("yyyyMMddHHmmss");
......

³¬¼¶¼òµ¥£ºÊ¹ÓÃFOR XML AUTO¿ØÖÆXMLÊä³ö(Òëת)

ÕâƪÎÄÕÂÃèÊöÈçºÎͨ¹ýʹÓÃFOR XML AUTO¸üºÃµÄ¿ØÖÆXMLÊä³ö¸ñʽ¡£ÀýÈçÌí¼ÓXML±ê¼Ç¡£ÓÃÕâ¸öÀ´Ìæ´úÄÑÓÚÀí½âµÄFOR XML EXPLICIT Óï¾ä¡£Èç¹ûÄãÔÚÓ¦ÓóÌÐòÖм´½«·´ÐòÁл¯Êä³öµÄXML£¬Äã¾Í»á¾õµÃÕâ¸öÐÅÏ¢¶ÔÄãÓÐÓá£
    ÔÚFor XML´Ó¾äÖУ¬Äúͨ³£Ê¹ÓÃÏÂÁз½Ê½Ö®Ò»£º
    RAW
    AUT ......

VS ¡°XML Îĵµ¡± ˵Ã÷

VS “XML Îĵµ” ˵Ã÷
1¡¢ÈçºÎÉú³É
£¨1£©¡¢ÔÚ Visual Studio ÖÐÉú³É“XML Îĵµ”ʾÀý
ÔÚ“½â¾ö·½°¸×ÊÔ´¹ÜÀíÆ÷”ÖÐÓÒ»÷¸ÃÏîÄ¿£¬ÔÙµ¥»÷“ÊôÐÔ”¡£
´ò¿ª“ÅäÖÃÊôÐÔ”Îļþ¼Ð£¬È»ºóµ¥»÷“Éú³É”¡£
½«“XML ÎĵµÎļþ”ÊôÐÔÉèÖÃΪ XMLsample.xml¡£
Ô ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ