CDATA within Javascript function

L

Leila

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!!
 
T

Thomas 'PointedEars' Lahn

Leila said:
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;[email protected]&gt;,
* Advanced RegExp parsing (C) 2003 Dietmar Meier
* &lt;[email protected]&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;
}
}

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

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
 
M

Martin Honnen

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.
 
L

Leila Allen

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 :)
 
L

Leila Allen

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top