All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
SQL表连接方式详解_mssql学习_编程技术
ASP.NET中常用的优化性能方法_.net资料_编程技术
C#箴言之用属性来访问类的私有成员_[Asp.Net教程]
在ASP.NET,Atlas中调用Web,Service—处理错误_[Asp.Net教程]
PHP实例-PHP分页代码_[PHP教程]
全面接触SQL语法(5)_mssql学习_编程技术
ASP.NET中的File类和Directory类的相关知识_.net资料_编程技术
在ASP.NET里得到网站的域名_[Asp.Net教程]
ASP.NET2.0中Gridview的使用技巧_[Asp.Net教程]
XML学习笔记(三)进阶篇_[XML教程]
Reading number is top 10 pictures
人美胸美腿更美1
代沟,真好
Go to the national museum1
On the verge of extinction of the beach1
29 the belle stars after bath figure1
Kim jong il's mistress, national beauty JinYuJi actor3
China's first snake village3
大四女生借债隆胸成功
Sora aoi in China4
鸡蛋的新玩法
Download software ranking
C++编程教程第三版
美女写真2
Kung.Fu.Panda.2
Sora aoi, the maid, students' uniforms
卡丁车单机版
实战黑客不求人
徐若瑄成名作“魔鬼天使”
Tram sex maniac 2 (H) rar bag5
Wild things 2
圣殿祭司的ASP.NET.2.0.开发详解-使用C#
归海一刀 published in(发表于) 2014/3/20 5:13:10 Edit(编辑)
XML字符串和XML DOCUMENT的相互转换_[XML教程]

XML字符串和XML DOCUMENT的相互转换_[XML教程]

XML字符串和XML DOCUMENT的相互转换_[XML教程]

在做一般的XML数据交换过程中,我更乐意传递XML字符串,而不是格式化的XML Document。这就涉及到XML字符串和Xml Document的转换问题,说白了这是个很简单的问题,本文就各种XML解析器分别列举如下,以方便自己今后查阅。

  一、使用最原始的javax.xml.parsers,标准的jdk api


// 字符串转XML
String xmlStr = \"......\";
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.parse(is);


//XML转字符串
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(\"encoding\",\"GB23121\");//解决中文问题,试过用GBK不行
ByteArrayOutputStream bos = new ByteArrayOutputStream();
t.transform(new DOMSource(doc), new StreamResult(bos));
String xmlStr = bos.toString();


这里的XML DOCUMENT为org.w3c.dom.Document


  二、使用dom4j后程序变得更简单


// 字符串转XML
String xmlStr = \"......\";
Document document = DocumentHelper.parseText(xmlStr);


// XML转字符串
Document document = ...;
String text = document.asXML();


这里的XML DOCUMENT为org.dom4j.Document


  三、使用JDOM


JDOM的处理方式和第一种方法处理非常类似


//字符串转XML
String xmlStr = \".....\";
StringReader sr = new StringReader(xmlStr);
InputSource is = new InputSource(sr);
Document doc = (new SAXBuilder()).build(is);


//XML转字符串
Format format = Format.getPrettyFormat();
format.setEncoding(\"gb2312\");//设置xml文件的字符为gb2312,解决中文问题
XMLOutputter xmlout = new XMLOutputter(format);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
xmlout.output(doc,bo);
String xmlStr = bo.toString();


这里的XML DOCUMENT为org.jdom.Document


  四、JAVASCRIPT中的处理


//字符串转XML
var xmlStr = \".....\";
var xmlDoc = new ActiveXObject(\"Microsoft.XMLDOM\");
xmlDoc.async=false;
xmlDoc.loadXML(xmlStr);
//可以处理这个xmlDoc了
var name = xmlDoc.selectSingleNode(\"/person/name\");
alert(name.text);


//XML转字符串
var xmlDoc = ......;
var xmlStr = xmlDoc.xml


这里的XML DOCUMENT为javascript版的XMLDOM







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.