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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML and XPATH with Javascript 1

Status
Not open for further replies.

NavekRednam

Technical User
Oct 2, 2008
2
GB
I'm trying to build a piece of Javascript that will display a series of tables by pulling data from a single xml file.

I currently have this piece of code.

Code:
var xmlDoc=null;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
}
else
{
alert('Your browser cannot handle this script');
}
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("books.xml");

var x=xmlDoc.getElementsByTagName("company");
    
document.write("<table border='1'>");

document.write("<thead><tr><th id='providername' abbr='provider'>Provider</th><th id='contactname' abbr='name'>Contact</th><th id='telephone' abbr='tel'>Tel No</th><th id='emailaddress' abbr='email'>Email</th><th id='websiteaddress' abbr='web'>Website</th></tr></thead><tbody>")

for (i=0;i<x.length;i++)

{
document.write("<tr>");
document.write("<td>");
document.write(
x[i].getElementsByTagName("provider")[0].childNodes[0].nodeValue);
document.write("</td>");

and so on and so on, until the table is completed.

This works fine in IE and Firefox.

But when I add an xpath expression to the following line, I get nothing from Firefox.

Code:
var x=xmlDoc.getElementsByTagName( "company[location/nw='1']" );

which should check this xml file:

Code:
<list>
<company>
	<provider>Company</provider>
	<contact>Contact</contact>
	<telno>123456789</telno>
	<email>blah.d@blahblah.co.zk</email>
	<website>[URL unfurl="true"]www.company.co.uk</website>[/URL]
	<location><nw>1</nw><sw>0</sw></location>
</company>
<company>
	<provider>CompanyII</provider>
	<contact>Tcatnoc</contact>
	<telno>987654321</telno>
	<email>tcat@noc.coz</email>
	<website>[URL unfurl="true"]www.IIcompany.coz</website>[/URL]
	<location><nw>0</nw><sw>1</sw></location>
</company>
</list>

and create a list of businesses that have locations in the nw, which it does in IE; but Firefox refuses to play ball.

Can someone point me in the right direction to where I can get some information on fixing this?
 
If you want to use xpath, it is not that trial.

[0] The easier solution would be to continue to use the getElementsByTagName and apply an additional test to see if location/nw is 1, like this.
[tt]
var x=xmlDoc.getElementsByTagName("company");
document.write("<table border='1'>");
//etc etc
for (var i=0;i<x.length;i++)
{
[blue]if (x.getElementsByTagName("nw")[0].childNodes[0].nodeValue=='1')
{[/blue]
//etc etc
[blue]}[/blue]
}
[/tt]
[1] To use xpath proper, more versatile and technically more to-date, is not that trial. Even for ie, I would like to see it applied through ie-proprietary SelectNodes() method rather than potentially misleading getElementsByTagName() as it won't extend the same way in moz. For moz, you've to use w3c dom level3 api. The whole biz is like this.
[tt]
[red]//[/red]var x=xmlDoc.getElementsByTagName("company");
[blue]var x;
if (window.ActiveXObject) {
x=xmlDoc.SelectNodes("//company[location/nw='1']");
} else if (document.implementation && document.implementation.createDocument) {
var resultset=xmlDoc.evaluate("//company[location/nw='1']",xmlDoc,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
x=new Array();
var node;
while ((node=resultset.iterateNext())!=null) {
x.push(node);
}
}[/blue]
//etc etc
[blue]if (!!x) {[/blue]
for (var i=0;i<x.length;i++)
{
//etc etc
}
[blue]}[/blue][/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top