CDATA within Javascript function

Discussion in 'XML' started by Leila, Aug 12, 2004.

  1. Leila

    Leila Guest

    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!!
    Leila, Aug 12, 2004
    #1
    1. Advertising

  2. 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;
    }
    }

    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
    Thomas 'PointedEars' Lahn, Aug 12, 2004
    #2
    1. Advertising

  3. 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/
    Martin Honnen, Aug 12, 2004
    #3
  4. Leila

    Leila Allen Guest

    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!
    Leila Allen, Aug 12, 2004
    #4
  5. Leila

    Leila Allen Guest

    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!
    Leila Allen, Aug 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Davison
    Replies:
    1
    Views:
    580
    Hal Rosser
    Jul 7, 2004
  2. Replies:
    3
    Views:
    747
    Joe Kesselman
    Mar 6, 2006
  3. Max
    Replies:
    7
    Views:
    863
  4. raga
    Replies:
    3
    Views:
    494
    Peter Flynn
    Jul 31, 2007
  5. Leila

    CDATA within Javascript function

    Leila, Aug 12, 2004, in forum: Javascript
    Replies:
    2
    Views:
    122
    Martin Honnen
    Aug 12, 2004
Loading...

Share This Page