473,395 Members | 1,948 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,395 software developers and data experts.

Question about createTextNode in xml files

DKM
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str));

I expected to see "Hello World!!!" in bold letters, but instead I see
"<b>Hello World!!!</b>".

I tried replacing / with \/, but that did not help. Then I tried
replacing < with & lt ; and > with & gt ;, but that did not work, then
I tried replacing < with \u 003e and > with \u 003c, but that did not
work too. The exact string is being displayed instead of "Hello
World!!!" in bold letters.

There is no problem with html files. Both FireFox and IE display in
bold letters.

Any tip will be appreciated.

Thanks very much in advance.

D.K. Mishra

Jul 23 '05 #1
11 1710
DKM wrote:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str));


This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text inside
it:

<script type="text/javascript">
function sayHelloWorld(){
var str = 'Hello World!!!';
var ele = document.getElementById('pid');
var b = document.createElement('B');
b.appendChild(document.createTextNode(str));
ele.appendChild(b);
}
</script>
<input type="button" value="Add 'Hello World'" onclick="
sayHelloWorld();
">
<div id="pid"></div>

[...]

--
Rob
Jul 23 '05 #2
DKM


RobG wrote:
DKM wrote:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str));
This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text inside
it:

<script type="text/javascript">
function sayHelloWorld(){
var str = 'Hello World!!!';
var ele = document.getElementById('pid');
var b = document.createElement('B');
b.appendChild(document.createTextNode(str));
ele.appendChild(b);
}
</script>


I was doing that from inside a java applet, but due to buggy nature of
LiveConenct, I am creating a html formated string in the java applet
and passing it to javascript. That has been working robustly without
any crash. Now, the problem is to insert that html formatted string
into the document.

Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?

Thanks very much in advance.

D.K. Mishra
<input type="button" value="Add 'Hello World'" onclick="
sayHelloWorld();
">
<div id="pid"></div>

[...]

--
Rob


Jul 23 '05 #3
DKM wrote:

RobG wrote:
DKM wrote:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str)) ;
This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text inside
it:

[...] I was doing that from inside a java applet, but due to buggy nature of
LiveConenct, I am creating a html formated string in the java applet
and passing it to javascript. That has been working robustly without
any crash. Now, the problem is to insert that html formatted string
into the document.

Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?

Then I guess you're stuck with innerHTML, but be aware that it is not
part of the W3C DOM.
<input type="button" value="Add 'Hello World'" onclick="
sayHelloWorld();
">
<div id="pid"></div>


<input type="button" value="Add 'Hello World'" onclick="

var s = '<b>Hello World!!!</b>';
document.getElementById('pid').innerHTML = s;

">
<div id="pid"></div>

--
Rob
Jul 23 '05 #4

A text node is just that, a text node, NOT an ELEMENT node, <b> is an
element node, you've have to
s=document.createElement('b').appenChild(document. createTextNode('STUFF
HERE')); or, as suggested already, use .innerHTML, text nodes is just the
string, no formatting, just text/plain, .innerHTML is a defacto standard
property which does take html as data, works fine, and you do need to
escape reserved chars s.innerHTML='<b> GRASS ISN\'T YELLOW<\/b>';
Danny
On Wed, 15 Jun 2005 17:18:14 -0700, RobG <rg***@iinet.net.auau> wrote:
DKM wrote:
RobG wrote:
DKM wrote:

I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str));

This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text
inside
it:

[...]
I was doing that from inside a java applet, but due to buggy nature of
LiveConenct, I am creating a html formated string in the java applet
and passing it to javascript. That has been working robustly without
any crash. Now, the problem is to insert that html formatted string
into the document.
Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?


Then I guess you're stuck with innerHTML, but be aware that it is not
part of the W3C DOM.
<input type="button" value="Add 'Hello World'" onclick="
sayHelloWorld();
">
<div id="pid"></div>


<input type="button" value="Add 'Hello World'" onclick="

var s = '<b>Hello World!!!</b>';
document.getElementById('pid').innerHTML = s;

">
<div id="pid"></div>



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #5
DKM


RobG wrote:
DKM wrote:

RobG wrote:
DKM wrote:

I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str)) ;

This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text inside
it:
[...]
I was doing that from inside a java applet, but due to buggy nature of
LiveConenct, I am creating a html formated string in the java applet
and passing it to javascript. That has been working robustly without
any crash. Now, the problem is to insert that html formatted string
into the document.

Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?


Then I guess you're stuck with innerHTML, but be aware that it is not
part of the W3C DOM.


Its an xhtml application in a xml file, and innerHTML does not work in
xml. I think IE treats xhtml file as html file and I can use innerHTML
in IE and have the desired result. However, FireFox errors out on
innerHTML.

I was thinking how hard will it be to create a function that would take
a formatted html string and create a document fragment out of it by
calling standard DOM functions like createElement, appendChild and
such. I bet a lot of folks must have thought about that and may have
created such a function.

What is that MSXML parser that one uses in IE? Is there such a beast
for FireFox? Could that be used to "parse" the html formated string
into a valid xml document?

Thanks very much in advance.

D.K. Mishra
<input type="button" value="Add 'Hello World'" onclick="
sayHelloWorld();
">
<div id="pid"></div>


<input type="button" value="Add 'Hello World'" onclick="

var s = '<b>Hello World!!!</b>';
document.getElementById('pid').innerHTML = s;

">
<div id="pid"></div>

--
Rob


Jul 23 '05 #6
On 15 Jun 2005 16:51:56 -0700, "DKM" <de**************@hotmail.com>
wrote:
Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?


A "compleex html formated string" might be HTML, but that doesn't mean
it's a well-formed XML fragment. Your string _must_ also be a balanced
well-formed XML fragment to work with it like this - if it's coming from
an external source, you might not be able to guarantee this (this is a
problem for HTML-transporting XML protocols like RSS)
To try and solve your immediate problem though, create a new, empty XML
document as an object in your script, then use the .load() method to
load and parse the string you have (yes, it's a slow parsing operation -
no way to avoid it). Then copy this new fragment into the main DOM
document.
Jul 23 '05 #7
"DKM" <de**************@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...


RobG wrote:
DKM wrote:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
script as foolows:

str = "<b>Hello World!!!</b>";
ele = documeny.getElementById("pid");
ele.appendChild(document.createTextNode(str));


This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text inside it:

<script type="text/javascript">
function sayHelloWorld(){
var str = 'Hello World!!!';
var ele = document.getElementById('pid');
var b = document.createElement('B');
b.appendChild(document.createTextNode(str));
ele.appendChild(b);
}
</script>


I was doing that from inside a java applet, but due to buggy nature of
LiveConenct, I am creating a html formated string in the java applet
and passing it to javascript. That has been working robustly without
any crash. Now, the problem is to insert that html formatted string
into the document.

Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?

Thanks very much in advance.


Rob's code should work, but not with an uppercase <B>, an long as you are
using xhtml. All html-tags in XHtml are *lowercase*.
Maybe thats your problem?

--
Dag.
Jul 23 '05 #8
DKM


Dag Sunde wrote:
"DKM" <de**************@hotmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...


RobG wrote:
DKM wrote:
> I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
> script as foolows:
>
> str = "<b>Hello World!!!</b>";
> ele = documeny.getElementById("pid");
> ele.appendChild(document.createTextNode(str));

This will create a text node with a value of the literal string
"<b>Hello World!!!</b>".

If you are expecting an HTML <B> element containing the text "Hello
World!!!" then you need to create the B element and put the text inside it:

<script type="text/javascript">
function sayHelloWorld(){
var str = 'Hello World!!!';
var ele = document.getElementById('pid');
var b = document.createElement('B');
b.appendChild(document.createTextNode(str));
ele.appendChild(b);
}
</script>
I was doing that from inside a java applet, but due to buggy nature of
LiveConenct, I am creating a html formated string in the java applet
and passing it to javascript. That has been working robustly without
any crash. Now, the problem is to insert that html formatted string
into the document.

Any idea as to how one can dynamically insert a long and compleex html
formated string into the document?

Thanks very much in advance.


Rob's code should work, but not with an uppercase <B>, an long as you are
using xhtml. All html-tags in XHtml are *lowercase*.
Maybe thats your problem?

Thank you to everyone. I have now figured a solution to the problem.
Instead of inserting the string as a textNode, what I am doing now is
parsing the string into a document and appending the documentElement of
the document as follows:

str = "<b>Hello World!!!</b>";
doc = new DOMParser().parseFromString(str, "text/xml");
tmpEle = doc.DocumentElement;
ele.appendChild(tmpEle);

earlier I was doing the following:

str = "<b>Hello World!!!</b>";
tmpEle = document.createTextNode(str);
ele.appendChild(tmpEle);

The above works fine in FireFix 1.0.4 where I was unable to set the
innerHTML. I suppose I will use the Microsoft's XML parser instead of
the DOMParser to achive the same, but fortunately, ele.innerHTMl = str
does it for IE 6.0.

Again, thanks to all.

D.K. Mishra

--
Dag.


Jul 23 '05 #9
HELLO
Why don't use a program using a compiler and writing files to do so ?

Java Javascript Xml Html ......

Where is the goo ole time of Turbo Pascal 6 and 7 ?
I could even acces dBASE files in write append rewrite and so on, with
Turbo Pascal ....

Regards.

HALLES

Jul 23 '05 #10
Zif
DKM wrote:
[...]
Thank you to everyone. I have now figured a solution to the problem.
Instead of inserting the string as a textNode, what I am doing now is
parsing the string into a document and appending the documentElement of
the document as follows:

str = "<b>Hello World!!!</b>";
doc = new DOMParser().parseFromString(str, "text/xml");
tmpEle = doc.DocumentElement;
ele.appendChild(tmpEle);

earlier I was doing the following:

str = "<b>Hello World!!!</b>";
tmpEle = document.createTextNode(str);
ele.appendChild(tmpEle);

The above works fine in FireFix 1.0.4 where I was unable to set the
innerHTML. I suppose I will use the Microsoft's XML parser instead of
the DOMParser to achive the same, but fortunately, ele.innerHTMl = str
ele.innerHTML
------------^
does it for IE 6.0.


innerHTML works in Firefox (and most others) too - perhaps the typo was
an issue?
--
Zif
Jul 23 '05 #11
DKM


Zif wrote:
DKM wrote:
[...]

Thank you to everyone. I have now figured a solution to the problem.
Instead of inserting the string as a textNode, what I am doing now is
parsing the string into a document and appending the documentElement of
the document as follows:

str = "<b>Hello World!!!</b>";
doc = new DOMParser().parseFromString(str, "text/xml");
tmpEle = doc.DocumentElement;
ele.appendChild(tmpEle);

earlier I was doing the following:

str = "<b>Hello World!!!</b>";
tmpEle = document.createTextNode(str);
ele.appendChild(tmpEle);

The above works fine in FireFix 1.0.4 where I was unable to set the
innerHTML. I suppose I will use the Microsoft's XML parser instead of
the DOMParser to achive the same, but fortunately, ele.innerHTMl = str


ele.innerHTML
------------^


That was a typo in my message. I was using innerHTML not innerHTMl.
does it for IE 6.0.

innerHTML works in Firefox (and most others) too - perhaps the typo was
an issue?


I know innerHTML works in FireFox, but only if you are doing something
like ele.innerHTML = "Hello World!!!", butif you try ele.innerHTML =
"<b>Hello World!!!</b>", it will not show "Hello World!!" in bold
letters. It will display the string "<b>"Hello World!!!"</b>".

Anyway, as I had posted in one of the earlier message, I had found a
solution for this problem by using the DomParser object in FireFox.

Thanks.

D.K. Mishra



--
Zif


Jul 23 '05 #12

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

Similar topics

11
by: DKM | last post by:
I have an empty tag like <p id="pid"></p> in a xml file. And, I have a script as foolows: str = "<b>Hello World!!!</b>"; ele = documeny.getElementById("pid");...
8
by: Frank Bishop | last post by:
I don't know very much yet and I'm looking to do some form validation on 20 form fields that accept numbers. I would like to alert the user if the combined values of any of the fields is greater...
2
by: bg | last post by:
sorry this is way off topic. using _vb6_: i'm trying to add/replace nodes to an xml document via CreateTextNode: xml = "<item>floogle</item>" set newNode = oDom.createTextNode(xml) call...
3
by: stephen.cunliffe | last post by:
I hope the subject doesn't get escaped, but I'll try to clarify here... I have: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> in my head (and yes, I need to keep it...
6
by: Daz | last post by:
Hi everyone. I am trying to figure out if it's possible to dynamically change the value of a textNode. I seem to remember experiementing before I implemented them into my page, and I was able...
50
by: Randy Webb | last post by:
When running this code in IE7: function insertScript() { var newScript = document.createElement('script'); newScript.type = "text/javascript"; var s = document.createTextNode("alert('hi');");...
1
by: eggie5 | last post by:
I have the following function that build a div and adds it to the page. function setDataJSON(req) { var data = eval('(' + req.responseText + ')'); console.log(data.news.length); for (var...
4
by: Dan Andrews | last post by:
Hello, I was wondering what is the correct way to handle special characters via javascript and the DOM. I would like to avoid document.write and innerHTML. What I am doing is dynamically...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.