使用String Request Param中的asp-classic读取XML Xpath的最佳方法是什么。
<% function FReadXml(pStringXml)
{
var xDom = new ActiveXObject("MSXML2.DOMDocument");
var a = xDom.LoadXML(pStringXml)
return a;
}
var xml = Request.QueryString("xml").item;
try{
var dom = FReadXml(xml);
//work with xpath
}catch(ex0){
Response.Write("problems when read a xml: " + ex0.message);
}
%GT;
非常感谢。
答案 0 :(得分:1)
我不知道你想用解析字符串到xml文档想说什么,但我可以帮助你使用Xpath
您可以使用带有SelectSingleNode({XPath Expression})
的XPath加载单个节点var xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0");
var currNode;
xmlDoc.async = false;
xmlDoc.load("books.xml");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
Response.write("You have error " + myErr.reason);
} else {
xmlDoc.setProperty("SelectionLanguage", "XPath");
currNode = xmlDoc.selectSingleNode("//book/author");
Response.write(currNode.text);
}
或使用SelectNodes({XPath Expression})
var xmlDoc = Server.CreateObject("Msxml2.DOMDocument.3.0");
var objNodeList;
xmlDoc.async = false;
xmlDoc.load("hello.xsl");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
Response.write("You have error " + myErr.reason);
} else {
xmlDoc.setProperty("SelectionNamespaces", "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'");
xmlDoc.setProperty("SelectionLanguage", "XPath");
objNodeList = xmlDoc.documentElement.selectNodes("//xsl:template");
Response.write(objNodeList.length);
}
你也可以看到这个帖子:
StackOverflow - How can I get the XML nodes from this XML in classic ASP (MSXML)?
编辑:指的是如何将xml字符串解析为MSXML dom文档,有一种方式就像你在评论boolValue = oXMLDOMDocument.loadXML(bstrXML);中表达的那样,这是一种创建自己的XML字符串的简单方法和操纵
示例:
var xmlDoc = Server.CreateObject("Msxml2.DOMDocument.3.0");
xmlDoc.async = false;
xmlDoc.loadXML("<customer><first_name>Joe</first_name><last_name>Smith</last_name></customer>");
if (xmlDoc.parseError.errorCode != 0) {
var myErr = xmlDoc.parseError;
Response.Write("You have error " + myErr.reason);
} else {
Response.Write(xmlDoc.xml);
}