在经过JavaScript处理XML时,一般只使用参数root,由于这个参数指定的是XML DOM文档元素的标签名javascript
var xmldom = document.implementation.createDocument("", "root", null); alert(xmldom.documentElement.tagName); //"root" var child = xmldom.createElement("child"); xmldom.documentElement.appendChild(child);
在解析XML以前,首先必须建立一个DOMParser的实例,而后再调用parseFromString()方法,这个方法接收两个参数,要解析的XML字符串和内容类型,返回的值是一个Document的实例java
var parser = new DOMParser(); var xmldom = parser.parseFromString("<root><child/></root>", "text/xml"); alert(xmldom.documentElement.tagName); //"root" alert(xmldom.documentElement.firstChild.tagName); //"child" var anotherChild = xmldom.createElement("child"); xmldom.documentElement.appendChild(anotherChild); var children = xmldom.getElementsByTagName("child"); alert(children.length); //2
要序列化DOM文档,首先必须建立XMLSerializer的实例,而后将文档传入其serializeToString()方法node
var serializer = new XMLSerializer(); var xml = serializer.serializeToString(xmldom); alert(xml);
6种不一样的XML文档版本能够供选择express
若是解析过程当中出错,能够在parseError属性中找到错误信息,这个属性自己是一个包含多个属性的对象,每一个属性都保存着有关解析错误的某一方面信息浏览器
对解析XML而言,下面函数能够在全部四种主要浏览器中使用安全
function parseXml(xml){ var xmldom = null; if (typeof DOMParser != "undefined"){ xmldom = (new DOMParser()).parseFromString(xml, "text/xml"); var errors = xmldom.getElementsByTagName("parsererror"); if (errors.length){ throw new Error("XML parsing error:" + errors[0].textContent); } } else if (typeof ActiveXObject != "undefined"){ xmldom = createDocument(); xmldom.loadXML(xml); if (xmldom.parseError != 0){ throw new Error("XML parsing error: " + xmldom.parseError.reason); } } else { throw new Error("No XML parser available."); } return xmldom; }
XPathEvaluator用于在特定的上下文中对XPath表达式求值app
evaluate(expression, context, nsresolver, type, result) :在给定的上下文中,基于特定的命名空间信息来对 XPath 表达式求值。剩下的参数指定如何返回结果。dom
其中第三个参数只在XML代码中使用了XML命名空间时有必要指定,若是XML代码中没有使用命名空间,这个参数应该指定为null。第四个参数的取值类型是下列常量之一函数
命名空间对象应该是下面这种字面量形式编码
{ prefix1:"uri1", prefix2:"uri2", prefix3:"uri3" }
selectSingleNode()函数的完整代码
function selectSingleNode(context, expression, namespaces){ var doc = (context.nodeType != 9 ? context.ownerDocument : context); if (typeof doc.evaluate != "undefined"){ var nsresolver = null; if (namespaces instanceof Object){ nsresolver = function(prefix){ return namespaces[prefix]; }; } var result = doc.evaluate(expression, context, nsresolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null); return (result !== null ? result.singleNodeValue : null); } else if (typeof context.selectSingleNode != "undefined"){ //建立命名空间字符串 if (namespaces instanceof Object){ var ns = ""; for (var prefix in namespaces){ if (namespaces.hasOwnProperty(prefix)){ ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "' "; } } doc.setProperty("SelectionNamespaces", ns); } return context.selectSingleNode(expression); } else { throw new Error("No XPath engine found."); } }
使用XSLT样式表转换XML文档的最简方式,就是将它们分别加到一个DOM文档中,而后再使用transformNode()方法,这个方法存在于文档的全部节点中,它接收一个参数,即包含XSLT样式表的文档,调用transformNode()方法会返回一个包含转换信息的字符串。
//加载 XML 和 XSLT(仅限于 IE) xmldom.load("employees.xml"); xsltdom.load("employees.xslt"); //转换 var result = xmldom.transformNode(xsltdom);
命名空间URI都是null,而内部名称就是参数的名称,另外,必须在调用transformToDocument()或transformToFragment()以前调用这个方法
var processor = new XSLTProcessor() processor.importStylesheet(xsltdom); processor.setParameter(null, "message", "Hello World! "); var result = processor.transformToDocument(xmldom);
还有两个与参数相关的方法,getParameter()和removeParameter(),分别用于取得和移除当前参数的值,这两个方法都要接受命名空间参数和参数的内部名称
var processor = new XSLTProcessor() processor.importStylesheet(xsltdom); processor.setParameter(null, "message", "Hello World! "); alert(processor.getParameter(null, "message")); //输出"Hello World!" processor.removeParameter(null, "message"); var result = processor.transformToDocument(xmldom);
重置处理器时要调用reset()方法,这个方法会从处理器中移除全部参数和样式,而后就能够再次调用importStyleSheet(),以加载不一样的XSLT样式表
var processor = new XSLTProcessor() processor.importStylesheet(xsltdom); //执行转换 processor.reset(); processor.importStylesheet(xsltdom2); //再执行转换
function transform(context, xslt){ if (typeof XSLTProcessor != "undefined"){ var processor = new XSLTProcessor(); processor.importStylesheet(xslt); var result = processor.transformToDocument(context); return (new XMLSerializer()).serializeToString(result); } else if (typeof context.transformNode != "undefined") { return context.transformNode(xslt); } else { throw new Error("No XSLT processor available."); } }