Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Document using XPath

Status
Not open for further replies.

chrigil

Programmer
Sep 23, 2003
178
GB
I have loaded an XML document into the JavaScript DOM using the following (imaginary XML doc for sake of example):

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>10.90</price>
</cd>
<cd country="UK">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<price>9.90</price>
</cd>
<cd country="USA">
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<price>9.90</price>
</cd>
</catalog>

function importXML()
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("..\ASPX\myXmlDoc.xml");
}


I now want to get all the elements where price is more than £10. Presumbly it is:

/catalog/cd[price=10.90]

Firstly how do I actually implement this using JavaScript and secondly I assume it is easy from this point to use the information i.e. Artist, Title and Price within html tags such as Img, Alt tags etc?

Also any advice for how to make sure the XML doc has loaded before trying to proceed?




Thanks in advance,

Chris
 
Price more than £10:
Code:
/catalog/cd[price&gt;10.90]
JavaScript implementation:
Code:
node = xmlDoc.selectSingleNode(/catalog/cd[.='Empire Burlesque']);
//Display price
document.write("<p>" + node.childNodes[2].text); + "</p>");

or

//select multiple nodes
nodes = xmlDoc.selectNode(/catalog/cd[price&gt;10.90]);
Error check XML doc (see
Code:
if (xmlDoc.parseError.errorCode != 0) {
  //Do something
  return false;
}

Hope this helps,

Jon
 
Ok thanks for the help.

I think I have managed to pick the correct node from the XML file. I say think because I simply cannot manage to get the value of the nodes. As the script is at the moment, the alert(node) simply says Null. What does this mean? By doing what the previous writer says regarding the node.childNodes[2].text simply doesn't work.

Below is my complete script:

function importXML()
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.validateOnParse=false;
xmlDoc.async = false;
xmlDoc.load("..\ASPX\myXmlDoc2.xml");

if (xmlDoc.parseError.errorCode != 0 )
{
xmlDoc.setProperty("SelectionLanguage", "XPath");

var node = xmlDoc.selectSingleNode("/catalog/cd/price[price='10.90']");
//var nodeValue = node.nodeValue("price").nodeTypedValue;

alert(node);
//alert(node.childNodes[2].text);
//alert(nodeValue);
}
else
{
alert("Error with parser")
}

}
importXML();


Any ideas?



Thanks in advance,

Chris
 
First, error is if errorcode != 0.

Second, your xpath is trying to reference a price element within the price element, ie:

<catalog>
<cd country="USA">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<price>
<price>**TRYING TO GET HERE**</price>
</price>
</cd>
</catalog>

That wont pick anything up. Try this:

Code:
function importXML()
{
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.validateOnParse = true;
  xmlDoc.async = false;
  xmlDoc.load("..\ASPX\myXmlDoc2.xml");
    
  if (xmlDoc.parseError.errorCode != 0 )
  {
    //This means there's an error!
  }

  xmlDoc.setProperty("SelectionLanguage", "XPath");

  var node = xmlDoc.selectSingleNode("/catalog/cd[price='10.90']");
  alert(node.childNodes[2].text);
}
importXML();

Shout if you need more help.

Jon
 

Thanks for the help, I now get a:

The System Cannot Locate the Object Specified

message. Does this mean I have failed instansiating the XMLDOM object?



Thanks in advance,

Chris
 
Yes. It worked for me. You must have the wrong path "..\ASPX\myXmlDoc2.xml" or wrong permissions or something.

Try putting the XML file where the html page is and just use xmlDoc.load("myXmlDoc2.xml");
 
I just wondering whether or not I have the XMLDOM object installed. Is it something I would have to install manually or would it be there by default? I have MSXML installed but perhaps this is the problem. It would help me work out whether or not the problem is the script or the system.

I`m used to instantiating objects in ASP/ASP.Net where if the object doesn`t exist you get a clear error message telling you the problem. Unfortunately JavaScript is the most annoying language in the world for error tolerance ;-(

Thanks in advance,

Chris
 
As far as I know, internet explorer has XML DOM/MSXML as an integral part of it. Post the complete html page and xml file you are using.
 
Problem solved it was the file path problem. Appartently XMLDom has problems grabbing files that aren't local to the site using it. Security feature perhaps???



Thanks in advance,

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top