【问题标题】:How to work with XML/XPath with asp-classic and parse String to xml Document如何使用 asp-classic 使用 XML/XPath 并将字符串解析为 xml 文档
【发布时间】:2012-01-09 20:37:38
【问题描述】:

从字符串请求参数中使用 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);
 }

%>

非常感谢。

【问题讨论】:

  • 这个问题有点太笼统了。提供您需要“解析”的 XML 的简短示例,并指明您希望从中提取哪些数据。
  • 我的问题是,如何从字符串参数加载文档,而不是从文件名参数加载!谢谢。
  • 你似乎已经这样做了 LoadXML 是使用将包含 XML 的字符串加载到 dom 中的方法。
  • 是的,LoadXML 是从字符串加载的方法,不用“加载”谢谢

标签: xpath asp-classic xml-parsing


【解决方案1】:

我不知道你想对 parse String to xml Document 说什么,但我可以帮助你处理使用 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 文档,有一种方法,就像您在 cmets 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);
}

【讨论】:

  • 我的问题真的是如何从字符串而不是从文件中加载,我可以运行,并发送给我.. and pStringXML = " ... xml>" var a = xmlDoc.loadXML (pStringXml)
  • 我的意思是字符串到文档,当我不想从字符串中的文件名加载但在我的情况下可能是请求参数时。
猜你喜欢
  • 2013-01-29
  • 2012-01-08
  • 1970-01-01
  • 2018-03-12
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多