jrey
jrey

Reputation: 2183

How to work with XML/XPath with asp-classic and parse String to xml Document

What is the best way to read XML Xpath with asp-classic from String Request Param.

  <%    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);
 }

%>

Thanks, a lot.

Upvotes: 0

Views: 5348

Answers (1)

Rafael
Rafael

Reputation: 3111

i don't know what do you want to say with parse String to xml Document but i can help you with the part of using Xpath

you can load a single node using XPath with SelectSingleNode({XPath Expression})

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);
}

or using 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);
}

you can see this thread too:

StackOverflow - How can I get the XML nodes from this XML in classic ASP (MSXML)?

Edit: refering to how to parse a xml string into a MSXML dom document there is a way like you denoted in your comments boolValue = oXMLDOMDocument.loadXML(bstrXML); it's a simple way to create your own XML string and manipulate

Example:

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);
}

Upvotes: 1

Related Questions