Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > CDATA within Javascript function

Reply
Thread Tools

CDATA within Javascript function

 
 
Leila
Guest
Posts: n/a
 
      08-12-2004
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!!
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      08-12-2004
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;&gt;,
* Advanced RegExp parsing (C) 2003 Dietmar Meier
* &lt;&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
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-12-2004


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/

 
Reply With Quote
 
Leila Allen
Guest
Posts: n/a
 
      08-12-2004
Thanks Mark, that is exactly what I wanted. This gives me the xml in a
string and I can then insert that into my div tag and the HTML tags in
<my:Body> are preserved and are displayed on the HTML page!

Many thanks for taking the time to answer this question!

Leila



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Leila Allen
Guest
Posts: n/a
 
      08-12-2004
Thanks Martin, that is exactly what I wanted. This gives me the xml in
a string and I can then insert that into my div tag and the HTML tags in
<my:Body> are preserved and are displayed on the HTML page!

Many thanks for taking the time to answer this question!

Leila



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Special characters within CDATA raga XML 3 07-31-2007 07:53 PM
Convert CDATA expression to Javascript RegExp Max XML 7 02-14-2007 03:29 PM
Can I un-CDATA my CDATA section and elaborate a transformation for the contained data? troppfigo@excite.it XML 3 03-06-2006 03:01 AM
CDATA within Javascript function Leila Javascript 2 08-12-2004 01:13 PM
Extracting CDATA Text without CDATA Tags??? John Davison Java 1 07-06-2004 11:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57