易截截图软件、单文件、免安装、纯绿色、仅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 ......

超级简单:使用FOR XML AUTO控制XML输出(译转)

这篇文章描述如何通过使用FOR XML AUTO更好的控制XML输出格式。例如添加XML标记。用这个来替代难于理解的FOR XML EXPLICIT 语句。如果你在应用程序中即将反序列化输出的XML,你就会觉得这个信息对你有用。
    在For XML从句中,您通常使用下列方式之一:
    RAW
    AUT ......

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

使用DOM4J和xpath解析XML(二)

在使用Dom4j解析xml文档时,我们很希望有一种类似正则表达式的东西来规范查询条件,而xpath正是这样一种很便利的规则吧.
    以下是本人用写的一个类,摘取部分代码;
Java代码
Java代码
String xmlName = path + "/" + userName + ".xml";  &n ......

flex学习 flex读取xml文件

源xml文件
 <?xml version="1.0" encoding="iso-8859-1"?>
<books>
<stock>
<name>The Picasso Code</name>
<author>Dan Blue</author>
<category>Fiction</category>
<description>Cubist paintings reveal a secret society of people ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号