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

CDATA within Javascript function

Hi,

I am having a problem retrieving the html tags from my XML document
when it's being loaded into a DOM object.

For example, my xml contains the following:

<my:InsideView>
..
..
..
<my:Body>
<b>Guess what happened today?</b>
<p>This is example text</p>
<p>I want to select all content within the body tags but the html tags
are also getting interpreted as xml tags!</p>
</my:Body>
..
..
..
</my:InsideView>

My javascript function is as follows
..
..
..

strTemp = "";

xmlObj = (xmlDocs.selectNodes("//my:InsideView//my:Body"));

strTemp = xmlObj.item(0).text;

if (strTemp.length > 0) {
document.all.mainText.innerHTML = strTemp;
} else {
document.all.mainText.innerHTML = "";
}
..
..
..

The problem though, is that when the content is displayed in the <div>
tag, all the HTML <b> and <p> tags within <my:Body> have been
interpreted as xml and not reflected in my <div>

Is there a way to select the <my:Body> node in Javascript and tell it
to ignore any other tags within <my:Body> - in affect using a CDATA
type function???

Any suggestions would be most appreciated!!
Jul 23 '05 #1
2 1514
Leila wrote:
I am having a problem retrieving the html tags from my XML document
Which HTML tags? It is *XML* (eXtensible Markup Language),
not HTML (HyperText Markup Language).
[...]
strTemp = xmlObj.item(0).text; ^^^^ if (strTemp.length > 0) {
document.all.mainText.innerHTML = strTemp; ^^^^ } else {
document.all.mainText.innerHTML = ""; ^^^^ }

The problem though, is that when the content is displayed in the <div>
tag, all the HTML <b> and <p> tags within <my:Body> have been
interpreted as xml and not reflected in my <div>
See the problem?
Is there a way to select the <my:Body> node in Javascript and tell it
to ignore any other tags within <my:Body> - in affect using a CDATA
type function???


I don't even know which object model you are using. What is xmlDocs?

You could possibly use the standard getElementsByTagName() method of DOM
Level 2+ Core's Node interface to get a reference to an element object.
You would then either need to serialize the element's content or use the
textContent property of DOM Level 3. Maybe this quick hack (not thoroughly
tested) will help with the former:

/**
* Strips <code>&lt;tags&gt;</code> and optionally the
* content between start and respective end tags from
* a string. Uses RegExp if supported.
*
* @author
* (C) 2001-2004 Thomas Lahn &lt;js@PointedEars.de&gt;,
* Advanced RegExp parsing (C) 2003 Dietmar Meier
* &lt;me***@innoline-systemtechnik.de&gt;
* @optional string s
* String where all tags should be stripped from. If not
* provided or <code>false</code>, it is assumed that the
* function is used as method of the String prototype,
* applied to a String object or literal. Note that in
* this case the method will not modify the String object
* either, but return a second String object.
* @optional boolean bStripContent = false
* If <code>true</code>, the content between a start tag and
* a corresponding end tag is also removed.
* If <code>false</code> (default), only start and end tags
* are removed.
* @optional boolean bCaseSensitive = false
* <code>true</code> for case-sensitive matches,
* <code>false</code> (default) otherwise.
* @optional string|Array of string tags
* String or array of values that can be evaluated as string to
* specify the tag(s) to be stripped. If omitted, all tags are
* stripped.
* @optional boolean bElements = false
* If <code>true</code>, strip elements, i.e. start and end tags.
* @returns
* String where all tags are stripped from.
* @see
* String.replace()
*/
function stripTags(s, bStripContent, bCaseSensitive, tags, bElements)
{
if (!s)
{
s = this;
}
else
{
s = s.toString();
}

var sUntagged = s;

if (s.match && s.replace)
{
// sUntagged = s.replace(/<[^>]*>/g, "");
var sRxTags = "", i;
if (tags)
{
if (!tags.constructor || tags.constructor == Array)
{
if (tags.join)
{
if (bElements)
{
for (i = 0, len = tags.length; i < len; i++)
{
tags[tags.length] = "/" + tags[i];
}
}

sRxTags = tags.join("|");
}
else if (tags.length)
{
for (i = 0, len = tags.length; i < len; i++)
{
sRxTags += tags[i];
if (bElements)
{
sRxTags += "/" + tags[i];
}

if (i < tags.length - 1)
{
sRxTags += "|";
}
}
}

if (sRxTags)
{
sRxTags = "(" + sRxTags + ")";
}
}
else
{
sRxTags = tags;
}
}

var sRx = "";
if (bStripContent)
{
sRx = "<(" + (sRxTags ? sRxTags : "[^<>]*") + ")(<[^<>]*>)*>.*</\\1>";
}
else
{
sRx = "<" + (sRxTags ? sRxTags : "[^<>]*") + "(<[^<>]*>)*[^<>]*>";
}

var rx = new RegExp(sRx, (bCaseSensitive ? "i" : "") + "g");
if (rx)
{
while (s.match(rx))
{
s = s.replace(rx, "");
}
sUntagged = s;
}
}
else
{
var a = "";
var bOutOfTag = true;
var l = s.length;
sUntagged = "";

for (i = 0; i < l; i++)
{
a = s.charAt(i);

if (bOutOfTag && (a == "<"))
{
bOutOfTag = false;
}

if (bOutOfTag)
{
sUntagged += a;
}

if ((!bOutOfTag) && (a == ">"))
{
bOutOfTag = true;
}
}
}

return sUntagged;
}
F'up2 comp.lang.javascript

PointedEars
Jul 23 '05 #2


Leila wrote:

I am having a problem retrieving the html tags from my XML document
when it's being loaded into a DOM object.

For example, my xml contains the following:

<my:InsideView>
.
.
.
<my:Body>
<b>Guess what happened today?</b>
<p>This is example text</p>
<p>I want to select all content within the body tags but the html tags
are also getting interpreted as xml tags!</p>
</my:Body>
.
.
.
</my:InsideView>

My javascript function is as follows
.
.
.

strTemp = "";

xmlObj = (xmlDocs.selectNodes("//my:InsideView//my:Body"));

strTemp = xmlObj.item(0).text;


I guess you want
strTemp = xmlObj.item(0).xml
then, but of course that gives you the <my:Body> as well so consider
using a HTML <div> around you contents in the XML then you can import
the XML.

--

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

Jul 23 '05 #3

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

Similar topics

4
by: alainpoint | last post by:
I am experimenting with ElementTree and i came accross some (apparently) weird behaviour. I would expect a piece of XML to be read, parsed and written back without corruption (except for the...
4
by: Leila | last post by:
Hi, I am having a problem retrieving the html tags from my XML document when it's being loaded into a DOM object. For example, my xml contains the following: <my:InsideView> .. ..
6
by: Cade Perkins | last post by:
How can the CDATA ending delimiter "]]>" be represented within a CDATA section itself? Consider an XML document that is intended to contain an embedded, uninterpreted XML example. Generally,...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
4
by: Jake Barnes | last post by:
I'm reading over this page: http://wiki.script.aculo.us/scriptaculous/show/Usage I'm struck by this code example +++++++++++++++++++++++++++++++ 3. Use
12
by: Peter Michaux | last post by:
Hi, I am experimenting with some of the Ruby on Rails JavaScript generators and see something I haven't before. Maybe it is worthwhile? In the page below the script is enclosed in //<!]> ...
18
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml version="1.0"?>\n<Data><!]></Data>\n' #After i...
1
by: Tarik Monem | last post by:
I have been able to successfully retrieve data from an xml file, where the data has been massaged a little bit, to create a table to be retrieved and it is displayed via a document.writeln within a...
9
by: shapper | last post by:
Hello, Why do some pages I have seen have //<![CDATA[ in the beginning of a script tag before the script itself? Do I need this? Thanks, Miguel
6
by: test9991014 | last post by:
While visiting the apple.com page, I noticed an unusual form behavior that seems pretty nice, but looking at the source (below) it is unclear how it works. Is there a tutorial that explains it?...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.