7 Replies Latest reply on Sep 9, 2007 5:40 AM by rainwebs

    XML and CDATA

    Jinglesthula
      I am trying to create an XML object that includes cdata. My code looks like this:

      var myXMLObj:XML;
      myXMLObj.myNewNode = "<![CDATA[ Here is some HTML <br/> And a variable: " + myVariable + " ]]>";

      The result of this is that all the < and > characters get HTML encoded as they are added to myXMLObj. I need the XML to look like this:

      <myNewNode><![CDATA[ Here is some HTML <br/> And a variable: value ]]></myNewNode>

      but it ends up looking like this:

      <myNewNode>&lt;![CDATA[ Here is some HTML &lt;br/&gt; And a variable: value ]]&gt;</myNewNode>

      I tried the following (none of which worked):

      myXMLObj.myNewNode = "&lt;![CDATA[ Here is some HTML &lt;br/&gt; And a variable: " + myVariable + " ]]&gt;";
      myXMLObj.myNewNode = XML("<![CDATA[ Here is some HTML <br/> And a variable: " + myVariable + " ]]>");
      myXMLObj.myNewNode = "\<![CDATA[ Here is some HTML \<br/\> And a variable: " + myVariable + " ]]\>";
      myXMLObj.myNewNode = unescape("<![CDATA[ Here is some HTML <br/> And a variable: " + myVariable + " ]]>");

      Anybody know how to include characters like <, >, and & in a String object that's going into an XML object?
        • 1. Re: XML and CDATA
          dsvsadvas
          Anybody knows the answer to this issue?

          "Anybody know how to include characters like <, >, and & in a String object that's going into an XML object?"

          I"m trying to add HTML text to an XML object and I"m having the same issue.
          • 2. Re: XML and CDATA
            Rahul Mainkar
            Hi,
            I had a similar requirement , but the only difference was that I converted string objects into XML.
            Use XML() function to convert your string into xml.
            Ex:
            var itemStr:String=<br/>
            var myXMLObj:XML;
            myXMLObj.myNewNode =XML(itemStr)

            Hope this works.
            I havent tested this as my flex builder 2 is uninstalled.
            Good Luck.
            • 3. Re: XML and CDATA
              dsvsadvas Level 1
              Thanks for the input. I tried what you suggested but it didn't work. There must be a way to add HTML tags to an XML object in Flex. Any Flex guru out there would like to help? I've been struggling with this problem for awhile.

              This is what my code looks like in an AS class:

              var mainXML:XML = <entries> </entries>;
              var itemXML:XML =
              <entry>
              <date></date>
              <headline></headline>
              <detail></detail>
              </entry>

              itemXML.date = "05/10/2007";
              itemXML.headline ="<![CDATA[ <b>My Headline</b> ]]>";
              itemXML.detail ="<![CDATA[ <b> Body Text </b> ]]>";

              mainXML = mainXML.appendChild(itemXML);
              //
              trace(mainXML.toString())


              Trace results:
              Notice the "<" and ">" got encoded.


              <entries>
              <entry>
              <date>05/10/2007</date>
              <headline>&lt;![CDATA[ &lt;b&gt;My Headline&lt;/b&gt; ]]&gt;</headline>
              <detail>&lt;![CDATA[ &lt;b&gt; Body Text &lt;/b&gt; ]]&gt;</detail>
              </entry>
              </entries>
              • 4. Re: XML and CDATA
                Level 7
                Thanks for the input. I tried what you suggested but it didn't work. There
                must be a way to add HTML tags to an XML object in Flex. Any Flex guru out
                there would like to help? I've been struggling with this problem for awhile.

                This is what my code looks like in an AS class:

                var mainXML:XML = <entries> </entries>;
                var itemXML:XML =
                <entry>
                <date></date>
                <headline></headline>
                <detail></detail>
                </entry>

                itemXML.date = "05/10/2007";
                itemXML.headline ="<![CDATA[ My Headline ]]>";
                itemXML.detail ="<![CDATA[ Body Text ]]>";

                mainXML = mainXML.appendChild(itemXML);
                //
                trace(mainXML.toString())


                Trace results:
                Notice the "<" and ">" got encoded.


                <entries>
                <entry>
                <date>05/10/2007</date>
                <headline><![CDATA[ <b>My Headline</b> ]]></headline>
                <detail><![CDATA[ <b> Body Text </b> ]]></detail>
                </entry>
                </entries>

                • 5. Re: XML and CDATA
                  Ashish Mishra Level 1

                  Even I m facing the Same problem, No body is there who knows about this. How can we include HTML data in xml. Since some tags are not in xml format.

                  And what about, display of HTML data, Flex don't have a component to display HTML data Also.
                  • 7. Re: XML and CDATA
                    rainwebs
                    Normally, it should be possible to replace a '<' with a '&lt;' and '>' with '&gt;', so that you can use the rest as a normal text string without using CDATA.

                    Example:

                    <myXMLTag>&lt;myHTMLTag&gt;What a nice HTML tag here&lt;/myHTMLTag&gt;</myXMLTag>

                    Although, I don't have tested this in a Flex environment.