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

JS解析XML文件和字符串的跨浏览器实现


大多数浏览器都内建了供读取和操作 XML 的 XML 解析器。
解析器把 XML 转换为 JavaScript 可存取的对象。
但是IE和其它浏览器是有很大区别的
解析 XML 文件 - 跨浏览器实现
<html>
<body>
<mce:script type="text/javascript"><!--
try //针对IE和基于IE内核的浏览器
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //针对Firefox, Opera等其它浏览器.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load("books.xml");
}
catch(e) {alert(e.message)}
// --></mce:script>
</body>
</html>
解析 XML 字符串 - 跨浏览器实现
<html>
<body>
<mce:script type="text/javascript"><!--
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Title</title>";
text=text+"<author>Author</author>";
text=text+"<year>2010</year>";
text=text+"</book>";
text=text+"</bookstore>";
try //针对IE和基于IE内核的浏览器
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(text);
}
catch(e)
{
try //针对FireFox,Opera等其它浏览器
{
parser=new DOMParser();
xmlDoc=parser.parsefromString(text,"text/xml");
}
catch(e) {alert(e.message)}
}
// --></mce:script>
</body>
</html>

注意

Internet Explorer 使用 loadXML() 方法来解析 XML 字符串,而其他浏览器使用 DOMParser 对象。


相关文档:

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 ......

使用TinyXml解析Xml示例

// TestXml.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include "tinyxml.h"
#include "lang.h"
#include <string>
using namespace std;
using namespace std;
#pragma comment(lib, "tinyxmld.lib")
B ......

Spring XML配置的12个技巧

Spring是一个强有力的java程序框架,其被广泛应用于java的程序中。它用POJO提供了企业级服务。Spring利用依赖注入可以获得简单而有效的测试能力。Spring beans,依赖关系,以及服务所需要的bean都将在配置文件中予以描述,配置文件一般采用XML格式。然而XML配置文件冗长而不易使用,在你进行一个使用了大量bean的大项目中它 ......

获取某个文件夹信息,并生成XML文件,按树形显示

private XmlDocument xmlDoc;
        private void btnCreateXml_Click(object sender, EventArgs e)
        {
            //自 ......

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 c ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号