转自:http://blog.sina.com.cn/s/blog_7f865faf01014qrs.html

一、使用最原始的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。

String 和 document 的相互转换总结的更多相关文章

  1. C++语法小记---string和int的相互转换

    string和int的相互转换 string转int istringstream is(""); //构造输入字符串流,流的内容初始化为“12”的字符串 int i; is > ...

  2. C# Enum Name String Description之间的相互转换

    最近工作中经常用到Enum中Value.String.Description之间的相互转换,特此总结一下. 1.首先定义Enum对象 public enum Weekday { [Descriptio ...

  3. jsoup方法string转document

    //Document doc2 = Jsoup.parseBodyFragment(element.text());                    //String FieldName=doc ...

  4. delphi char数组、string和Pchar的相互转换

    因为要调用windows的api或者给vc++写接口,很多地方都要用到pchar,现在将char数组.string和pchar之间的相互转换都列出来,都是网上找的资料,我总结一下,先直接上代码,再讲原 ...

  5. string与int的相互转换C++(转)

    string与int之间的相互转换C++(转) #include<iostream> #include<string> #include<sstream> usin ...

  6. Java中String和Int的相互转换

    一.将字串 String 转换成整数 intA. 有2个方法:1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([Strin ...

  7. Array(数组)与Json String (Json字符串) 的相互转换

    1.Array转换成Json String             function jsonToString(arr) {             var s = "";     ...

  8. c++中char*\wchar_t*\string\wstring之间的相互转换

    string U2A(const wstring& str)//Unicode字符转Ascii字符 { string strDes; if ( str.empty() ) goto __end ...

  9. C#中List〈string〉和string[]数组之间的相互转换

    1,从System.String[]转到List<System.String> System.String[] str={"str","string" ...

随机推荐

  1. dede5.7前台插入恶意JS代码

    这个问题应该很久了 最近发现有用这个的蠕虫,dede 前台提交友情链接 只用htmlspecialchars简单处理了一下 可以插入代码plus/flink_add.php 提交: 表单中提交 图片地 ...

  2. DataGridView设置不自动显示数据库中未绑定的列

    项目中将从数据库查出来的数据绑定到DataGridView,但是不想显示所有的字段.此功能可以通过sql语句控制查出来的字段数目,但是DataGridView有属性可以控制不显示未绑定的数据,从UI层 ...

  3. NoClassDefFoundError:aspectj/weaver/reflect/ReflectionWorld$Reflection

    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: ...

  4. [Angularjs]angular ng-repeat与js特效加载先后导致的问题

    写在前面 最近在项目中遇到这样的一个前端的bug,在ng-repeat中绑定的图片,有一个晃动的特效,在手机端浏览的时候,图片有时候会正常展示,有时就展示不出来.当时猜测是因为angularjs与特效 ...

  5. this prototype 闭包 总结

    this对象 整理下思路: 一般用到this中的情景: 1.构造方法中 function A(){ this.name="yinshen"; } var a=new A(); co ...

  6. 移动端富文本编辑器artEditor

    摘要: 由于手机上打字比较慢,并不适合长篇大论的文章,所以移动端的富文本编辑器很少.artEditor是一款基于jQuery的移动端富文本编辑器,支持插入图片,后续完善其他功能. 插件地址:https ...

  7. BI就是报表?

    实际上,报表只是BI的一部分,虽然BI应用的结果通常需要通过报表来展示,但是,BI绝对不仅仅是报表.其实,大家对这些概念的理解,如同15年前的ERP一样.1998年,国内两大巨头金蝶与用友都开始宣称从 ...

  8. Y2K Accounting Bug(贪心)

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10945   Accepted: 54 ...

  9. codeforces 258div2C Predict Outcome of the Game

    题目链接:http://codeforces.com/contest/451/problem/C 解题报告:三个球队之间一共有n场比赛,现在已经进行了k场,不知道每个球队的胜场是多少,如三个球队的胜场 ...

  10. ruby : Exception Notification

    https://github.com/smartinez87/exception_notification#sections Add the following line to your applic ...