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><tags></code> and optionally the
* content between start and respective end tags from
* a string. Uses RegExp if supported.
*
* @author
* (C) 2001-2004 Thomas Lahn <js@PointedEars.de>,
* Advanced RegExp parsing (C) 2003 Dietmar Meier
* <me***@innoline-systemtechnik.de>
* @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