易截截图软件、单文件、免安装、纯绿色、仅160KB

String和Xml之间的转换、String转InputStream

通常在操作xml的时候,都是通过inputstream(很多情况下是FileInputStream)来读入xml并转为dom的,很多人会遇到这种情况数据不是从文件读入的而是从String中取得的
于是会使用
InputStream in = new ByteArrayInputStream (str.getBytes());来取得inputstream ,但是这种InputStream中数据被转成了byte数组,所以转dom的时候就会报错
可以通过一下思路来解决
// 字符串转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读取游戏配置信息或保存

 /*
* 主要作用;
* 从xml读取游戏配置信息或保存
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Reflection;
namespace Game
{
class Config
{
Ke ......

检查xml错误

把xml拖到IE里就找到错在哪里了
如下:
The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
文档的顶层无效。处理资源 'file:///D:/Tomcat 5.5/webapps/myapp/WEB-INF/web.xml' 时出错。第 1 行,位置: 44 ......

用 PHP 读取和编写 XML DOM


用 PHP 读取和编写 XML DOM
使用 DOM 库、SAX 解析器和正则表达式
文档选项
打印本页
将此页作为电子邮件发送
级别: 中级
Jack Herrington (jack_d_herrington@codegeneration.net), 高级软件工程师, "Code Generation Network"
2006 年 2 月 06 日
有许多技术可用于用 PHP 读取和编写 XML。本文提供了三种 ......

解读PHP DOMDocument在解析XML文件中的作用

解读PHP DOMDocument在解析XML文件中的作用
http://developer.51cto.com  2009-12-02 10:39  佚名  柳城博客  我要评论(0)
PHP DOMDocument的功能非常强大,我们在这篇文章中将介绍如何正确的运用PHP DOMDocument来进行XML文件的解析。希望对又需要的朋友有所帮助。
在使用PHP对XML文件进行解析的时 ......

javascript解析返回的xml各式的字符串

<script>
var flags ;
  if(window.XMLHttpRequest) { 
   XMLHttpReq = new XMLHttpRequest(); //firefox下执行此语句
   }
   else if(window.ActiveXObject) {
    try{
    XMLHttpReq = new Acti ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号