473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Problems with XML & JavaScript

I am developing a page for Mozilla/IE which reads xml data and when the
link on the page is clicked it displays the data as a table.
The display is controlled by a Javascript. Everything works fine. But
in the table one of the sections is a URL. I want this to be clickable
so that it takes me to the corresponding webpage. I am new to this and
tried hard to fix this but cudnt find my way around. I tried adding an
xsl sheet but still it doesnt seem to fix the problem.
Could anyone help me out. I have pasted the code below

Thanks a lot

***XML*****

<?xml version="1.0" encoding="ISO-8859-1"?>
<logs>
<Websites>
<title>Google</title>
<country>US</country>
<URL>http://www.google.com</URL>
</Websites>

<Websites>
<title>CNN</title>
<country>US</country>
<URL>http://www.cnn.com</URL>
</Websites>

</logs>
***HTML*******
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="JavaScript" type="text/javascript">

function linksXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {if (xmlDoc.readyState == 4)
createTable()};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("google.xml");
}

function createTable()

{

var x = xmlDoc.getElementsByTagName('Websites');
var newEl = document.createElement('TABLE');
newEl.setAttribute('cellPadding',5);
var tmp = document.createElement('TBODY');
newEl.appendChild(tmp);
var row = document.createElement('TR');
for (j=0;j<x[0].childNodes.length;j++)
{
if (x[0].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TH');
var theData = document.createTextNode(x[0].childNodes[j].nodeName);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
for (i=0;i<x.length;i++)
{
var row = document.createElement('TR');
for (j=0;j<x[i].childNodes.length;j++)
{
if (x[i].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TD');
var theData =
document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
}
document.getElementById('writeroot1').appendChild( newEl);

}
</script>
</head>
<body>

<a href="javascript:linksXML()">Test</a>

<p id="writeroot1" style="background-color: green;">
</p>

</body>
</html>

Sep 29 '05 #1
3 1554


ar*******@yahoo.com wrote:
I am developing a page for Mozilla/IE which reads xml data and when the
link on the page is clicked it displays the data as a table.
The display is controlled by a Javascript. Everything works fine. But
in the table one of the sections is a URL. I want this to be clickable
so that it takes me to the corresponding webpage.


"clickable"? So you want a link in the HTML document?
var link = document.createElement('a');
link.href = 'http://example.com/';
link.appendChild(document.createTextNode('http://example.com/'));
// now append the link element to some other element e.g.
document.body.appendChild(link);
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 29 '05 #2
I guess I wasnt being clear. My Xml file has a URL tag. For example say
http://www.google.com. And in the html page when the page loads up
there is a linked text called 'Test'. So when the link 'Test' is
clicked the JavaScript takes the XML table and displays in the html
page. And the URL is simply displayed as a text and not as a link. Its
this link that i want it to be clicked . So when i click the 'Test'
link on the page and when it displays the table on the page, the tag
URL from the XML page needs to be clickable..

Thanks a lot Martin. I appreciate your help


Martin Honnen wrote:
ar*******@yahoo.com wrote:
I am developing a page for Mozilla/IE which reads xml data and when the
link on the page is clicked it displays the data as a table.
The display is controlled by a Javascript. Everything works fine. But
in the table one of the sections is a URL. I want this to be clickable
so that it takes me to the corresponding webpage.


"clickable"? So you want a link in the HTML document?
var link = document.createElement('a');
link.href = 'http://example.com/';
link.appendChild(document.createTextNode('http://example.com/'));
// now append the link element to some other element e.g.
document.body.appendChild(link);
--

Martin Honnen
http://JavaScript.FAQTs.com/


Sep 29 '05 #3
ar*******@yahoo.com wrote:
I guess I wasnt being clear. My Xml file has a URL tag. For example say
http://www.google.com. And in the html page when the page loads up
there is a linked text called 'Test'. So when the link 'Test' is
clicked the JavaScript takes the XML table and displays in the html
page. And the URL is simply displayed as a text and not as a link. Its
this link that i want it to be clicked . So when i click the 'Test'
link on the page and when it displays the table on the page, the tag
URL from the XML page needs to be clickable..


Don't top-post, it discourages replies - reply below quoted text.

As you loop through the elements of your XML file, test to see if you
are handling a URL element. If so, add it as an A element using the
method demonstrated by Martin. Otherwise, add text element.

You may want to create separate functions to do that, say toss a
'createA' function a URL and label and get back a ready to go element.

Something like the following should work:

...
tmp.appendChild(row);
// Keep i local
for (var i=0; i<x.length; i++)
{
var row = document.createElement('TR');
// Keep j local
for (var j=0; j<x[i].childNodes.length; j++) {
var theNode = x[i].childNodes[j];

if (theNode.nodeType == 1) {
if ('URL' == theNode.nodeName) {
var theData = document.createElement('a');
theData.href = theNode.firstChild.nodeValue;
theData.appendChild(
document.createTextNode(theNode.firstChild.nodeVal ue));
} else {
var theData =
document.createTextNode(theNode.firstChild.nodeVal ue);
}
var container = document.createElement('td');
container.appendChild(theData);
row.appendChild(container);
}
}
tmp.appendChild(row);
}
document.getElementById('writeroot1').appendChild( newEl);
...

A few tips:
- declare variables at the start, don't initialise them on every loop
- use variables to hold values rather than make repeated calls to the
document
- make sure counters (i, j, etc.) are *always* local variables,
otherwise you may get very strange results.
- don't use 'continue' (it's removed from the snippet above) - it makes
the logic less clear and hence less maintainable. Use a normal if
block.

If you are using this purely for your own web page, consider JSON
instead of XML. It puts a lot more power in the hands of whoever writes
the data file so you just mess with the data file rather than the file
and the parser.

<URL:http://www.crockford.com/JSON/index.html>

Stay away from XSL/XSLT on the client.

[...]
--
Rob
Sep 30 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Fork | last post by:
Note: I pasted the code the attachments as plain text after the message (I wasn't able to post it with an attachment...) Attached are the XSL and XML files that I am having problems with. I am...
0
by: Linda Antonsson | last post by:
Hi, I am trying to put together a CSS-based layout consisting of a header, a sidebar and a main content area. Page: http://www.westeros.org/ASoWS/ CSS:...
4
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
3
by: Nathan Sokalski | last post by:
I am adding an onmouseover attribute using the Attributes.Add() method, and the String I am using for the value contains the & character. However, when rendered the & is converted to the HTML...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.