aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Using XPath to do an XML lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Using XPath to do an XML lookup" Watch "Using XPath to do an XML lookup" New topic
Author

Using XPath to do an XML lookup

ian bert
Greenhorn

Joined: Jun 08, 2007
Posts: 18

~Ok so this may seem like deja vu to some, but i need to find a way to use XPath to do an XML Look up. My server can only do Javascritp (duh) or PHP.

~I want to be able to click on a link in an HTML or PHP page and get two dialogues:
1. the Download window
2. the Information Window

~The Information window is my problem. I need to have just one HTML (or PHP0 page for the window and just one XML page. This XML page would have all the information on the products.

~The Information window needs to access the XML page, get the correct entry, load it and display it.

~Does anyone have any idea how to do this, or where I could learn how to do this?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It doesn't matter what language you use on the server-side as long as it can spit out XML. I'm using jQuery for doing something very similar on the client-side.

Basically, I have the following jQuery code:


The prog.xml file contains XML similar as the following one:


You can see that jQuery lets you specify an XPath expression (prog/entry) for selecting DOM nodes in the XML retrieved from the server. From there, everything else is basic stuff.

Hope this helps.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
ian bert
Greenhorn

Joined: Jun 08, 2007
Posts: 18

Ok ill try this
ian bert
Greenhorn

Joined: Jun 08, 2007
Posts: 18

I dont think it will work
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Why not ?
Can you share your code ?
ian bert
Greenhorn

Joined: Jun 08, 2007
Posts: 18

All the parts of the code the poster thing wont allow is seperated by a space

Script.js
var xmlDoc;
products.function(id, name, file_size, summary)
{
this.id=id;
this.name=name;
this.file_size=file_size;
this.summary=summary;
}
function loadXML(){
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("product_info.xml");
var id=attrs.getNamedItem("id").value;
var name=attrs.getNamedItem("name").value;
var version=parseFloat(attrs.getNamedItem("file_size").value)
var file_size=attrs.getNamedItem("summary").value;
getmessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("product_info.xml");
var id=attrs.getNamedItem("id").value;
var name=attrs.getNamedItem("name").value;
var version=parseFloat(attrs.getNamedItem("file_size").value)
var file_size=attrs.getNamedItem("summary").value;
xmlDoc.o nload=getmessage;
}
else
{
alert('Your browser cannot handle this script');
}
}
// Actual message maker
function getmessage()
{
document.getElementById("name").innerHTML=xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("file_size").innerHTML=xmlDoc.getElementsByTagName("file_size")[0].childNodes[0].nodeValue;
document.getElementById("summary").innerHTML=xmlDoc.getElementsByTagName("summary")[0].childNodes[0].nodeValue;
document.getElementById("version").innerHTML=xmlDoc.getElementsByTagName("version")[0].childNodes[0].nodeValue;
}

function dio(){
window.open("dio.html");

}

Programs.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="SHORTCUT ICON" href="images/logo_orig/bp.ico" />
<script src="script.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Bertolacci Productions~Programs</title>
</head>
<body bgcolor="#fffacd" text="#000000" link="FF4500">
<center>
<h1>Programs</h1>
</center>
<hr color="#CC9900" size=2 width="100%"/>
<center>
<a href="http://www.bertolacciproductions.com"><img src="images/buttons/home.bmp" alt="Home" border="none"/></a>
<a href="new.html"><img src="images/buttons/new.bmp" alt="New" border="none"/></a>
<a href="programs.html"><img src="images/buttons/programs.bmp" alt="Programs" border="none"/></a>
<a href="add-ins.html"><img src="images/buttons/add-ins.bmp" alt="Add-Ins" border="none"/></a>
<a href="box-sets.html"><img src="images/buttons/boxset.bmp" alt="Box Sets" border="none"/></a>
<a href="service.html"><img src="images/buttons/service.bmp" alt="Services" border="none"/></a>
<a href="about.html"><img src="images/buttons/about.bmp" alt="About" border="none"/></a>
<a href="code/codebook.html"><img src="images/buttons/code.bmp" alt="Code Cook Book" border="none"/></a>
</center>
<hr color="#CC9900" size=2 width="100%"/>
<center>
<table border="0">
<tr>
<td width="121">Programs</td>
<td width="121">Games</td>
<td width="121">Other</td>
</tr>
<tr>
<td width="121"></td>
<td width="121"><a o nclick="dio(001)" href="Downloads/programs/games/bustin'_blox.exe">Bustin' Blox</a></td>
<td width="121"></td>

<tr>
</table>
</center>
</body>
</html>

product_info.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<products>
<download id="001" name="Bustin' Blox" file_size"2 Mb" summary"Our version of Brick. Only One Level (Kinda Sucks)" version="1.0"/>
<download id="002" id="001" name="ASDF" file_size"ASDF" summary"ASDF" version="1.0"/>
</products>

dio.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="SHORTCUT ICON" href="images/logo_orig/info.ico" />
<script src="script.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Product Info</title>
</head>
<body o nload="loadXML(id)" bgcolor="#fffacd" text="#000000" link="FF4500">
<center>
<h1>Product Information</h1>
</center>
<p>
<b><u>File Name: </u></b><span id="name"></span><br />
<b><u>File Size: </u></b><span id="file_size"></span><br />
<b><u>File Summary: </u></b><span id="summary"></span><br />
<b><u>File Version: </u></b><span id="version"></span><br />
</p>
</body>
</html>
ian bert
Greenhorn

Joined: Jun 08, 2007
Posts: 18

Hello?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Could you explain which problems you're experiencing?

Run your code in Firefox using the Firebug extension and you'll see a couple errors
[ September 08, 2007: Message edited by: Valentin Crettaz ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by ian bert:
[QB My server can only do Javascritp (duh) ....[/QB]


Server and Javascript ?? Am I missing something.


Rahul Bhattacharjee
LinkedIn - Blog
ian bert
Greenhorn

Joined: Jun 08, 2007
Posts: 18

No you arent
 
jQuery in Action, 2nd edition
 
subject: Using XPath to do an XML lookup