473,799 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="JavaS cript" type="text/javascript">

function linksXML()
{
if (document.imple mentation && document.implem entation.create Document)
{
xmlDoc = document.implem entation.create Document("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveX Object)
{
xmlDoc = new ActiveXObject(" Microsoft.XMLDO M");
xmlDoc.onreadys tatechange = function () {if (xmlDoc.readySt ate == 4)
createTable()};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("go ogle.xml");
}

function createTable()

{

var x = xmlDoc.getEleme ntsByTagName('W ebsites');
var newEl = document.create Element('TABLE' );
newEl.setAttrib ute('cellPaddin g',5);
var tmp = document.create Element('TBODY' );
newEl.appendChi ld(tmp);
var row = document.create Element('TR');
for (j=0;j<x[0].childNodes.len gth;j++)
{
if (x[0].childNodes[j].nodeType != 1) continue;
var container = document.create Element('TH');
var theData = document.create TextNode(x[0].childNodes[j].nodeName);
container.appen dChild(theData) ;
row.appendChild (container);
}
tmp.appendChild (row);
for (i=0;i<x.length ;i++)
{
var row = document.create Element('TR');
for (j=0;j<x[i].childNodes.len gth;j++)
{
if (x[i].childNodes[j].nodeType != 1) continue;
var container = document.create Element('TD');
var theData =
document.create TextNode(x[i].childNodes[j].firstChild.nod eValue);
container.appen dChild(theData) ;
row.appendChild (container);
}
tmp.appendChild (row);
}
document.getEle mentById('write root1').appendC hild(newEl);

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

<a href="javascrip t:linksXML()">T est</a>

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

</body>
</html>

Sep 29 '05 #1
3 1584


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.create Element('a');
link.href = 'http://example.com/';
link.appendChil d(document.crea teTextNode('htt p://example.com/'));
// now append the link element to some other element e.g.
document.body.a ppendChild(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.create Element('a');
link.href = 'http://example.com/';
link.appendChil d(document.crea teTextNode('htt p://example.com/'));
// now append the link element to some other element e.g.
document.body.a ppendChild(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.create Element('TR');
// Keep j local
for (var j=0; j<x[i].childNodes.len gth; j++) {
var theNode = x[i].childNodes[j];

if (theNode.nodeTy pe == 1) {
if ('URL' == theNode.nodeNam e) {
var theData = document.create Element('a');
theData.href = theNode.firstCh ild.nodeValue;
theData.appendC hild(
document.create TextNode(theNod e.firstChild.no deValue));
} else {
var theData =
document.create TextNode(theNod e.firstChild.no deValue);
}
var container = document.create Element('td');
container.appen dChild(theData) ;
row.appendChild (container);
}
}
tmp.appendChild (row);
}
document.getEle mentById('write root1').appendC hild(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.c om/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
3514
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 trying to extract the stock information after having downloaded the HTML and converted to XML (well-formed HTML) using XSL and XPATH and am unable. I am basing what I have done so far off of...
0
2366
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: http://www.westeros.org/ASoWS/ASoWS-All.css (the relevant IDs are towards the bottom) JS: http://www.westeros.org/ASoWS/SetHeight.js
4
3031
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: /mypage.asp?value1=1&amp;value2=4&amp; ... which of course means nothing to asp.
4
3230
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 that I could externalize my JavaScript, but that will not be practical throughout this application. Is there any way to get around this issue? Xalan processor. Stripped down stylesheet below along with XHTML output. <?xml version='1.0'?>...
8
2819
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
14
5937
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
12
10122
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) ..... // xsl contents abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
3
3238
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 representation of &amp; which causes my JavaScript not to work. What can I do to prevent the Add() method from modifying my value? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.