Question about createTextNode in xml files

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

    Question about createTextNode in xml files

    I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
    script as foolows:

    str = "<b>Hello World!!!</b>";
    ele = documeny.getEle mentById("pid") ;
    ele.appendChild (document.creat eTextNode(str)) ;

    I expected to see "Hello World!!!" in bold letters, but instead I see
    "<b>Hello World!!!</b>".

    I tried replacing / with \/, but that did not help. Then I tried
    replacing < with & lt ; and > with & gt ;, but that did not work, then
    I tried replacing < with \u 003e and > with \u 003c, but that did not
    work too. The exact string is being displayed instead of "Hello
    World!!!" in bold letters.

    There is no problem with html files. Both FireFox and IE display in
    bold letters.

    Any tip will be appreciated.

    Thanks very much in advance.

    D.K. Mishra

  • RobG

    #2
    Re: Question about createTextNode in xml files

    DKM wrote:[color=blue]
    > I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
    > script as foolows:
    >
    > str = "<b>Hello World!!!</b>";
    > ele = documeny.getEle mentById("pid") ;
    > ele.appendChild (document.creat eTextNode(str)) ;[/color]

    This will create a text node with a value of the literal string
    "<b>Hello World!!!</b>".

    If you are expecting an HTML <B> element containing the text "Hello
    World!!!" then you need to create the B element and put the text inside
    it:

    <script type="text/javascript">
    function sayHelloWorld() {
    var str = 'Hello World!!!';
    var ele = document.getEle mentById('pid') ;
    var b = document.create Element('B');
    b.appendChild(d ocument.createT extNode(str));
    ele.appendChild (b);
    }
    </script>
    <input type="button" value="Add 'Hello World'" onclick="
    sayHelloWorld() ;
    ">
    <div id="pid"></div>

    [...]

    --
    Rob

    Comment

    • DKM

      #3
      Re: Question about createTextNode in xml files



      RobG wrote:[color=blue]
      > DKM wrote:[color=green]
      > > I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
      > > script as foolows:
      > >
      > > str = "<b>Hello World!!!</b>";
      > > ele = documeny.getEle mentById("pid") ;
      > > ele.appendChild (document.creat eTextNode(str)) ;[/color]
      >
      > This will create a text node with a value of the literal string
      > "<b>Hello World!!!</b>".
      >
      > If you are expecting an HTML <B> element containing the text "Hello
      > World!!!" then you need to create the B element and put the text inside
      > it:
      >
      > <script type="text/javascript">
      > function sayHelloWorld() {
      > var str = 'Hello World!!!';
      > var ele = document.getEle mentById('pid') ;
      > var b = document.create Element('B');
      > b.appendChild(d ocument.createT extNode(str));
      > ele.appendChild (b);
      > }
      > </script>[/color]

      I was doing that from inside a java applet, but due to buggy nature of
      LiveConenct, I am creating a html formated string in the java applet
      and passing it to javascript. That has been working robustly without
      any crash. Now, the problem is to insert that html formatted string
      into the document.

      Any idea as to how one can dynamically insert a long and compleex html
      formated string into the document?

      Thanks very much in advance.

      D.K. Mishra


      [color=blue]
      > <input type="button" value="Add 'Hello World'" onclick="
      > sayHelloWorld() ;
      > ">
      > <div id="pid"></div>
      >
      > [...]
      >
      > --
      > Rob[/color]

      Comment

      • RobG

        #4
        Re: Question about createTextNode in xml files

        DKM wrote:[color=blue]
        >
        > RobG wrote:
        >[color=green]
        >>DKM wrote:
        >>[color=darkred]
        >>>I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
        >>>script as foolows:
        >>>
        >>>str = "<b>Hello World!!!</b>";
        >>>ele = documeny.getEle mentById("pid") ;
        >>>ele.appendCh ild(document.cr eateTextNode(st r));[/color]
        >>
        >> This will create a text node with a value of the literal string
        >> "<b>Hello World!!!</b>".
        >>
        >> If you are expecting an HTML <B> element containing the text "Hello
        >> World!!!" then you need to create the B element and put the text inside
        >> it:[/color][/color]
        [...][color=blue]
        > I was doing that from inside a java applet, but due to buggy nature of
        > LiveConenct, I am creating a html formated string in the java applet
        > and passing it to javascript. That has been working robustly without
        > any crash. Now, the problem is to insert that html formatted string
        > into the document.
        >
        > Any idea as to how one can dynamically insert a long and compleex html
        > formated string into the document?
        >[/color]

        Then I guess you're stuck with innerHTML, but be aware that it is not
        part of the W3C DOM.
        [color=blue]
        >[color=green]
        >><input type="button" value="Add 'Hello World'" onclick="
        >> sayHelloWorld() ;
        >> ">
        >><div id="pid"></div>
        >>[/color][/color]

        <input type="button" value="Add 'Hello World'" onclick="

        var s = '<b>Hello World!!!</b>';
        document.getEle mentById('pid') .innerHTML = s;

        ">
        <div id="pid"></div>





        --
        Rob

        Comment

        • Danny

          #5
          Re: Question about createTextNode in xml files


          A text node is just that, a text node, NOT an ELEMENT node, <b> is an
          element node, you've have to
          s=document.crea teElement('b'). appenChild(docu ment.createText Node('STUFF
          HERE')); or, as suggested already, use .innerHTML, text nodes is just the
          string, no formatting, just text/plain, .innerHTML is a defacto standard
          property which does take html as data, works fine, and you do need to
          escape reserved chars s.innerHTML='<b > GRASS ISN\'T YELLOW<\/b>';


          Danny


          On Wed, 15 Jun 2005 17:18:14 -0700, RobG <rgqld@iinet.ne t.auau> wrote:
          [color=blue]
          > DKM wrote:[color=green]
          >> RobG wrote:
          >>[color=darkred]
          >>> DKM wrote:
          >>>
          >>>> I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
          >>>> script as foolows:
          >>>>
          >>>> str = "<b>Hello World!!!</b>";
          >>>> ele = documeny.getEle mentById("pid") ;
          >>>> ele.appendChild (document.creat eTextNode(str)) ;
          >>>
          >>> This will create a text node with a value of the literal string
          >>> "<b>Hello World!!!</b>".
          >>>
          >>> If you are expecting an HTML <B> element containing the text "Hello
          >>> World!!!" then you need to create the B element and put the text
          >>> inside
          >>> it:[/color][/color]
          > [...][color=green]
          >> I was doing that from inside a java applet, but due to buggy nature of
          >> LiveConenct, I am creating a html formated string in the java applet
          >> and passing it to javascript. That has been working robustly without
          >> any crash. Now, the problem is to insert that html formatted string
          >> into the document.
          >> Any idea as to how one can dynamically insert a long and compleex html
          >> formated string into the document?
          >>[/color]
          >
          > Then I guess you're stuck with innerHTML, but be aware that it is not
          > part of the W3C DOM.
          >[color=green]
          >>[color=darkred]
          >>> <input type="button" value="Add 'Hello World'" onclick="
          >>> sayHelloWorld() ;
          >>> ">
          >>> <div id="pid"></div>
          >>>[/color][/color]
          >
          > <input type="button" value="Add 'Hello World'" onclick="
          >
          > var s = '<b>Hello World!!!</b>';
          > document.getEle mentById('pid') .innerHTML = s;
          >
          > ">
          > <div id="pid"></div>
          >
          >
          >
          >
          >[/color]



          --
          Using Opera's revolutionary e-mail client: http://www.opera.com/mail/

          Comment

          • DKM

            #6
            Re: Question about createTextNode in xml files



            RobG wrote:[color=blue]
            > DKM wrote:[color=green]
            > >
            > > RobG wrote:
            > >[color=darkred]
            > >>DKM wrote:
            > >>
            > >>>I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
            > >>>script as foolows:
            > >>>
            > >>>str = "<b>Hello World!!!</b>";
            > >>>ele = documeny.getEle mentById("pid") ;
            > >>>ele.appendCh ild(document.cr eateTextNode(st r));
            > >>
            > >> This will create a text node with a value of the literal string
            > >> "<b>Hello World!!!</b>".
            > >>
            > >> If you are expecting an HTML <B> element containing the text "Hello
            > >> World!!!" then you need to create the B element and put the text inside
            > >> it:[/color][/color]
            > [...][color=green]
            > > I was doing that from inside a java applet, but due to buggy nature of
            > > LiveConenct, I am creating a html formated string in the java applet
            > > and passing it to javascript. That has been working robustly without
            > > any crash. Now, the problem is to insert that html formatted string
            > > into the document.
            > >
            > > Any idea as to how one can dynamically insert a long and compleex html
            > > formated string into the document?
            > >[/color]
            >
            > Then I guess you're stuck with innerHTML, but be aware that it is not
            > part of the W3C DOM.[/color]

            Its an xhtml application in a xml file, and innerHTML does not work in
            xml. I think IE treats xhtml file as html file and I can use innerHTML
            in IE and have the desired result. However, FireFox errors out on
            innerHTML.

            I was thinking how hard will it be to create a function that would take
            a formatted html string and create a document fragment out of it by
            calling standard DOM functions like createElement, appendChild and
            such. I bet a lot of folks must have thought about that and may have
            created such a function.

            What is that MSXML parser that one uses in IE? Is there such a beast
            for FireFox? Could that be used to "parse" the html formated string
            into a valid xml document?

            Thanks very much in advance.

            D.K. Mishra
            [color=blue]
            >[color=green]
            > >[color=darkred]
            > >><input type="button" value="Add 'Hello World'" onclick="
            > >> sayHelloWorld() ;
            > >> ">
            > >><div id="pid"></div>
            > >>[/color][/color]
            >
            > <input type="button" value="Add 'Hello World'" onclick="
            >
            > var s = '<b>Hello World!!!</b>';
            > document.getEle mentById('pid') .innerHTML = s;
            >
            > ">
            > <div id="pid"></div>
            >
            >
            >
            >
            >
            > --
            > Rob[/color]

            Comment

            • Andy Dingley

              #7
              Re: Question about createTextNode in xml files

              On 15 Jun 2005 16:51:56 -0700, "DKM" <debashishkmish ra@hotmail.com>
              wrote:
              [color=blue]
              >Any idea as to how one can dynamically insert a long and compleex html
              >formated string into the document?[/color]

              A "compleex html formated string" might be HTML, but that doesn't mean
              it's a well-formed XML fragment. Your string _must_ also be a balanced
              well-formed XML fragment to work with it like this - if it's coming from
              an external source, you might not be able to guarantee this (this is a
              problem for HTML-transporting XML protocols like RSS)


              To try and solve your immediate problem though, create a new, empty XML
              document as an object in your script, then use the .load() method to
              load and parse the string you have (yes, it's a slow parsing operation -
              no way to avoid it). Then copy this new fragment into the main DOM
              document.

              Comment

              • Dag Sunde

                #8
                Re: Question about createTextNode in xml files

                "DKM" <debashishkmish ra@hotmail.com> wrote in message
                news:1118879516 .587333.63770@f 14g2000cwb.goog legroups.com...[color=blue]
                >
                >
                > RobG wrote:[color=green]
                > > DKM wrote:[color=darkred]
                > > > I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
                > > > script as foolows:
                > > >
                > > > str = "<b>Hello World!!!</b>";
                > > > ele = documeny.getEle mentById("pid") ;
                > > > ele.appendChild (document.creat eTextNode(str)) ;[/color]
                > >
                > > This will create a text node with a value of the literal string
                > > "<b>Hello World!!!</b>".
                > >
                > > If you are expecting an HTML <B> element containing the text "Hello
                > > World!!!" then you need to create the B element and put the text[/color][/color]
                inside[color=blue][color=green]
                > > it:
                > >
                > > <script type="text/javascript">
                > > function sayHelloWorld() {
                > > var str = 'Hello World!!!';
                > > var ele = document.getEle mentById('pid') ;
                > > var b = document.create Element('B');
                > > b.appendChild(d ocument.createT extNode(str));
                > > ele.appendChild (b);
                > > }
                > > </script>[/color]
                >
                > I was doing that from inside a java applet, but due to buggy nature of
                > LiveConenct, I am creating a html formated string in the java applet
                > and passing it to javascript. That has been working robustly without
                > any crash. Now, the problem is to insert that html formatted string
                > into the document.
                >
                > Any idea as to how one can dynamically insert a long and compleex html
                > formated string into the document?
                >
                > Thanks very much in advance.[/color]

                Rob's code should work, but not with an uppercase <B>, an long as you are
                using xhtml. All html-tags in XHtml are *lowercase*.
                Maybe thats your problem?

                --
                Dag.


                Comment

                • DKM

                  #9
                  Re: Question about createTextNode in xml files



                  Dag Sunde wrote:[color=blue]
                  > "DKM" <debashishkmish ra@hotmail.com> wrote in message
                  > news:1118879516 .587333.63770@f 14g2000cwb.goog legroups.com...[color=green]
                  > >
                  > >
                  > > RobG wrote:[color=darkred]
                  > > > DKM wrote:
                  > > > > I have an empty tag like <p id="pid"></p> in a xml file. And, I have a
                  > > > > script as foolows:
                  > > > >
                  > > > > str = "<b>Hello World!!!</b>";
                  > > > > ele = documeny.getEle mentById("pid") ;
                  > > > > ele.appendChild (document.creat eTextNode(str)) ;
                  > > >
                  > > > This will create a text node with a value of the literal string
                  > > > "<b>Hello World!!!</b>".
                  > > >
                  > > > If you are expecting an HTML <B> element containing the text "Hello
                  > > > World!!!" then you need to create the B element and put the text[/color][/color]
                  > inside[color=green][color=darkred]
                  > > > it:
                  > > >
                  > > > <script type="text/javascript">
                  > > > function sayHelloWorld() {
                  > > > var str = 'Hello World!!!';
                  > > > var ele = document.getEle mentById('pid') ;
                  > > > var b = document.create Element('B');
                  > > > b.appendChild(d ocument.createT extNode(str));
                  > > > ele.appendChild (b);
                  > > > }
                  > > > </script>[/color]
                  > >
                  > > I was doing that from inside a java applet, but due to buggy nature of
                  > > LiveConenct, I am creating a html formated string in the java applet
                  > > and passing it to javascript. That has been working robustly without
                  > > any crash. Now, the problem is to insert that html formatted string
                  > > into the document.
                  > >
                  > > Any idea as to how one can dynamically insert a long and compleex html
                  > > formated string into the document?
                  > >
                  > > Thanks very much in advance.[/color]
                  >
                  > Rob's code should work, but not with an uppercase <B>, an long as you are
                  > using xhtml. All html-tags in XHtml are *lowercase*.
                  > Maybe thats your problem?[/color]


                  Thank you to everyone. I have now figured a solution to the problem.
                  Instead of inserting the string as a textNode, what I am doing now is
                  parsing the string into a document and appending the documentElement of
                  the document as follows:

                  str = "<b>Hello World!!!</b>";
                  doc = new DOMParser().par seFromString(st r, "text/xml");
                  tmpEle = doc.DocumentEle ment;
                  ele.appendChild (tmpEle);

                  earlier I was doing the following:

                  str = "<b>Hello World!!!</b>";
                  tmpEle = document.create TextNode(str);
                  ele.appendChild (tmpEle);

                  The above works fine in FireFix 1.0.4 where I was unable to set the
                  innerHTML. I suppose I will use the Microsoft's XML parser instead of
                  the DOMParser to achive the same, but fortunately, ele.innerHTMl = str
                  does it for IE 6.0.

                  Again, thanks to all.

                  D.K. Mishra


                  [color=blue]
                  >
                  > --
                  > Dag.[/color]

                  Comment

                  • HALLES

                    #10
                    Re: Question about createTextNode in xml files

                    HELLO
                    Why don't use a program using a compiler and writing files to do so ?

                    Java Javascript Xml Html ......

                    Where is the goo ole time of Turbo Pascal 6 and 7 ?
                    I could even acces dBASE files in write append rewrite and so on, with
                    Turbo Pascal ....

                    Regards.

                    HALLES

                    Comment

                    • Zif

                      #11
                      Re: Question about createTextNode in xml files

                      DKM wrote:[color=blue]
                      >[/color]
                      [...][color=blue]
                      >
                      > Thank you to everyone. I have now figured a solution to the problem.
                      > Instead of inserting the string as a textNode, what I am doing now is
                      > parsing the string into a document and appending the documentElement of
                      > the document as follows:
                      >
                      > str = "<b>Hello World!!!</b>";
                      > doc = new DOMParser().par seFromString(st r, "text/xml");
                      > tmpEle = doc.DocumentEle ment;
                      > ele.appendChild (tmpEle);
                      >
                      > earlier I was doing the following:
                      >
                      > str = "<b>Hello World!!!</b>";
                      > tmpEle = document.create TextNode(str);
                      > ele.appendChild (tmpEle);
                      >
                      > The above works fine in FireFix 1.0.4 where I was unable to set the
                      > innerHTML. I suppose I will use the Microsoft's XML parser instead of
                      > the DOMParser to achive the same, but fortunately, ele.innerHTMl = str[/color]

                      ele.innerHTML
                      ------------^
                      [color=blue]
                      > does it for IE 6.0.
                      >[/color]

                      innerHTML works in Firefox (and most others) too - perhaps the typo was
                      an issue?


                      --
                      Zif

                      Comment

                      • DKM

                        #12
                        Re: Question about createTextNode in xml files



                        Zif wrote:[color=blue]
                        > DKM wrote:[color=green]
                        > >[/color]
                        > [...][color=green]
                        > >
                        > > Thank you to everyone. I have now figured a solution to the problem.
                        > > Instead of inserting the string as a textNode, what I am doing now is
                        > > parsing the string into a document and appending the documentElement of
                        > > the document as follows:
                        > >
                        > > str = "<b>Hello World!!!</b>";
                        > > doc = new DOMParser().par seFromString(st r, "text/xml");
                        > > tmpEle = doc.DocumentEle ment;
                        > > ele.appendChild (tmpEle);
                        > >
                        > > earlier I was doing the following:
                        > >
                        > > str = "<b>Hello World!!!</b>";
                        > > tmpEle = document.create TextNode(str);
                        > > ele.appendChild (tmpEle);
                        > >
                        > > The above works fine in FireFix 1.0.4 where I was unable to set the
                        > > innerHTML. I suppose I will use the Microsoft's XML parser instead of
                        > > the DOMParser to achive the same, but fortunately, ele.innerHTMl = str[/color]
                        >
                        > ele.innerHTML
                        > ------------^[/color]

                        That was a typo in my message. I was using innerHTML not innerHTMl.
                        [color=blue]
                        >[color=green]
                        > > does it for IE 6.0.
                        > >[/color]
                        >
                        > innerHTML works in Firefox (and most others) too - perhaps the typo was
                        > an issue?[/color]

                        I know innerHTML works in FireFox, but only if you are doing something
                        like ele.innerHTML = "Hello World!!!", butif you try ele.innerHTML =
                        "<b>Hello World!!!</b>", it will not show "Hello World!!" in bold
                        letters. It will display the string "<b>"Hello World!!!"</b>".

                        Anyway, as I had posted in one of the earlier message, I had found a
                        solution for this problem by using the DomParser object in FireFox.

                        Thanks.

                        D.K. Mishra

                        [color=blue]
                        >
                        >
                        > --
                        > Zif[/color]

                        Comment

                        Working...