CDATA within Javascript function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Leila

    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:InsideVie w>
    ..
    ..
    ..
    <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.select Nodes("//my:InsideView//my:Body"));

    strTemp = xmlObj.item(0). text;

    if (strTemp.length > 0) {
    document.all.ma inText.innerHTM L = strTemp;
    } else {
    document.all.ma inText.innerHTM L = "";
    }
    ..
    ..
    ..

    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!!
  • Thomas 'PointedEars' Lahn

    #2
    Re: CDATA within Javascript function

    Leila wrote:
    [color=blue]
    > I am having a problem retrieving the html tags from my XML document[/color]

    Which HTML tags? It is *XML* (eXtensible Markup Language),
    not HTML (HyperText Markup Language).
    [color=blue]
    > [...]
    > strTemp = xmlObj.item(0). text;[/color]
    ^^^^[color=blue]
    > if (strTemp.length > 0) {
    > document.all.ma inText.innerHTM L = strTemp;[/color]
    ^^^^[color=blue]
    > } else {
    > document.all.ma inText.innerHTM L = "";[/color]
    ^^^^[color=blue]
    > }
    >
    > 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>[/color]

    See the problem?
    [color=blue]
    > 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???[/color]

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

    You could possibly use the standard getElementsByTa gName() 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@PointedE ars.de&gt;,
    * Advanced RegExp parsing (C) 2003 Dietmar Meier
    * &lt;meier@innol ine-systemtechnik.d e&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.construc tor || tags.constructo r == 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.javas cript

    PointedEars

    Comment

    • Martin Honnen

      #3
      Re: CDATA within Javascript function



      Leila wrote:

      [color=blue]
      > 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:InsideVie w>
      > .
      > .
      > .
      > <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.select Nodes("//my:InsideView//my:Body"));
      >
      > strTemp = xmlObj.item(0). text;[/color]

      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


      Comment

      Working...