11 Replies Latest reply on Jan 21, 2008 12:30 PM by vaudor

    html tags in XML won't load into text field

    maguskrool Level 1
      (Hi and thanks for trying to help me. Please read my next post, I've made some progress and I think things are more clearly explained there)

      Hi.

      I've got a mydata.xml file with the following code:

      <?xml version="1.0" encoding="UTF-8"?>
      <mytext>This text should be<b>bold</b> or <i>italic</i>, please.<mytext>

      In my flash file I have a dynamic text field named txt01 with embedded characters (I'm mentioning this because I believe I've read embedding caused some problems with xml and html tags, somewhere). I load the xml into the txt01, but I get:

      This text should be

      The text gets truncated at the beginning of the first html tag, probably considers it to be a new xml tag and so it doesn't display anything else. Attached is the code I'm using.

      Can anyone give me hand with this? Thanks. Text
        • 1. Re: html tags in XML won't load into text field
          Level 7
          Hi maguskrool,

          I think the problem is the slash in your closing tags. Try enclosing
          the whole string in a CDATA section like this:

          <?xml version="1.0" encoding="UTF-8"?>
          <mytext><![CDATA[This text should be bold or italic,
          please.]]><mytext>

          Patrick

          maguskrool wrote:
          > Hi.
          >
          > I've got a mydata.xml file with the following code:
          >
          > <?xml version="1.0" encoding="UTF-8"?>
          > <mytext>This text should be bold or italic, please.<mytext>
          >
          > In my flash file I have a dynamic text field named txt01 with embedded
          > characters (I'm mentioning this because I believe I've read embedding caused
          > some problems with xml and html tags, somewhere). I load the xml into the
          > txt01, but I get:
          >
          > This text should be
          >
          > The text gets truncated at the beginning of the first html tag, probably
          > considers it to be a new xml tag and so it doesn't display anything else.
          > Attached is the code I'm using.
          >
          > Can anyone give me hand with this? Thanks.
          >
          > //1ST FRAME
          > stop();
          > var raiz:MovieClip = this;
          > //
          > System.useCodepage = true;
          > var xmlTxt:XML = new XML();
          > xmlTxt.ignoreWhite = true;
          > xmlTxt.onLoad = XMLcorrect;
          > xmlTxt.load("xml/teste_css_01.xml");
          > //
          > //# # # # # FUNCTIONS # # # # #
          > function XMLcorrecto (success:Boolean, bFicheiro:Boolean):Void {
          > if (this.loaded && this.status == 0) {
          > raiz.gotoAndStop(2);
          > } else {
          > trace("Error: "+this.status);
          > }
          > }
          > //
          > //2ND FRAME
          > var xnTxt:XMLNode = xmlTxt.firstChild;
          > this.txt01.text = xnTxt.firstChild;
          >

          --
          http://www.baynewmedia.com
          Faster, easier, better...ActionScript development taken to new heights.
          Download the BNMAPI today. You'll wonder how you ever did without it!
          Available for ActionScript 2.0/3.0.
          • 2. Re: html tags in XML won't load into text field
            maguskrool Level 1
            Hi Patrick and thank you.

            The thing is, I want the html formatting, as well as the displayed text. Using <![CDATA[ displays the tags but doesn't apply the html formatting.
            • 3. Re: html tags in XML won't load into text field
              Marghoob Suleman Level 1
              Please your share your code and xml.
              • 4. html tags in XML won't load into text field
                maguskrool Level 1
                Ok, I've made some progress, but it's still not quite there. Recap: I have a dynamic text field on the Stage, named txt01. I'm trying to load text into it, from a .xml file (text_01.xml) and have it formatted through an external .css file (css_01.css) that I'll load into Flash as well. I've tried checking and unchecking the "render text as html" option but the results were the same.

                When I test the movie, I see the bold and italic text correctly, but none of the other formatting described in the .css. Using getStyleNames displays the correct styles defined in css_01.css, so I know they are loaded correctly.

                I've seen the example described here:
                http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Liv eDocs_Parts&file=00000910.html

                and it works mostly like mine, except it loads the text from a .txt and not a .xml file, and there the css works perfectly. I do need to use .xml however, so trying a .txt is not an option. Any clues on what's wrong and how I might get it to work?

                Thank you for your time.

                • 5. Re: html tags in XML won't load into text field
                  Level 7
                  Hi,

                  Well, in your first example you used square brackets and ,.
                  Here you're using tags which will really mess up your XML unless you
                  enxlose it as CDATA. Also, when you define ".biggy", doesn't this define
                  a style class:

                  Like:
                  <p class="biggy">Big text</p>

                  I'm a bit confused as to what exactly you're doing at this point but
                  maybe this will help.

                  Patrick

                  maguskrool wrote:
                  > Ok, I've made some progress, but it's still not quite there. Recap: I have a
                  > dynamic text field on the Stage, named txt01. I'm trying to load text into it,
                  > from a .xml file (text_01.xml) and have it formatted through an external .css
                  > file (css_01.css) that I'll load into Flash as well.
                  >
                  > When I test the movie, I see the bold and italic text correctly, but none of
                  > the other formatting described in the .css. Using getStyleNames displays the
                  > correct styles defined in css_01.css, so I know they are loaded correctly.
                  >
                  > I've seen the example described here:
                  >
                  > http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?c
                  > ontext=LiveDocs_Parts&file=00000910.html
                  >
                  > and it works mostly like mine, except it loads the text from a .txt and not a
                  > .xml file, and there the css works perfectly. I do need to use .xml however, so
                  > trying a .txt is not an option. Any clues on what's wrong and how I might get
                  > it to work?
                  >
                  > Thank you for your time.
                  >
                  >
                  >
                  > //FLASH, AS2
                  > //1ST FRAME
                  > var style_sheet:TextField.StyleSheet = new TextField.StyleSheet ();
                  > var my_xml:XML = new XML ();
                  > my_xml.ignoreWhite = true;
                  > //
                  > style_sheet.onLoad = function (success:Boolean):Void {
                  > if (success) {
                  > txt01.styleSheet = style_sheet;
                  > my_xml.load ("text_01.xml");
                  > } else {
                  > trace ("Error loading CSS.");
                  > }
                  > };
                  >
                  > //
                  > my_xml.onLoad = function (success:Boolean):Void {
                  > if (success) {
                  > txt01.text = my_xml;
                  > } else {
                  > trace ("Error loading XML.");
                  > }
                  > };
                  > //
                  > style_sheet.load ("css_01.css");
                  >
                  > //XML (text_01.xml)
                  > <?xml version="1.0" encoding="UTF-8"?>
                  > <mytext>This <biggy>text</biggy> should be<b>bold</b> or <i>italic</i>,
                  > please.</mytext>
                  >
                  > //CSS (css_01.css)
                  > .biggy{
                  > font-size: 36px;
                  > color:#006600;
                  > }
                  > b {
                  > font-size: 16px;
                  > color:#336633;
                  > }
                  >

                  • 6. Re: html tags in XML won't load into text field
                    maguskrool Level 1
                    What I need is for the tags in the .xml to be used to format the text they enclose, when that text is loaded into the text field in Flash. To that end, I defined styles in a .css file that Flash also loads.

                    Correct me if I'm wrong, but CDATA would cause those tags to be ignored and read as regular text, right? That's not what I need, I need the tags in the xml to be recognised and treated as css style "enablers", so the text they enclose gets the appropriate format.

                    Also, I didn't understand when you said:
                    "Well, in your first example you used square brackets and ,. "

                    I'm guessing you meant < b >, but I don't think I used square brackets anywhere.

                    On the brightside, after some more research, I've got this thing working like I needed and the solution could not be simples - a bit of nonsense, in my opinion.

                    I in the .css file, just remove the . before the classes' names et voilá, the css styles are applied to the text.

                    Thanks for all the help, guys.
                    • 7. Re: html tags in XML won't load into text field
                      Level 7
                      CDATA means your text won't be translated. If you leave it as shown, the
                      information will be parsed as XML nodes. In other words:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <mytext>This <biggy>text</biggy> should be<b>bold</b> or
                      <i>italic</i>, please.</mytext>

                      mytext is a node
                      biggy is a node
                      b is a node
                      i is a node.

                      --not HTML nodes...XML nodes.

                      If you do something like: trace (myXML.firstChild)...you'll see
                      something like:

                      "This should be or, please."

                      You may get the whole structure but that's just pure luck. Future
                      changes can really muddy the waters.

                      What you're doing is translating this HTML text to XML and having the
                      XML object mangle it. That's why the example shown on Adobe's site is a
                      text file...if you load it as XML and have XML parse it, you are
                      translating it. What this creates is:

                      Object=XML
                      Object.firstChild=mytext
                      Object.firstChild.childNodes[0]=biggy
                      Object.firstChild.childNodes[1]=b
                      Object.firstChild.childNodes[2]=i

                      It's being parsed. It's not HTML, it's XML and it's being treated that
                      way. If you want it treated as HTML, you must specify that the data in
                      the "mytext" node is not to be translated, parsed, or otherwise
                      modified. It must stay as HTML (or whatever you put in there). And to do
                      that, it must be CDATA. If you add more nodes to this structure, there
                      will come a point at which you will get things "breaking" and you won't
                      be able to fix them. They won't actually be breaking...the XML object is
                      translating them exactly it should...as XML. HTML is not XML. XML cannot
                      arbitrarily contain HTML. These two are not directly compatible and will
                      continue to cause problems if you add tags in directly. Sorry...I know
                      I'm rambling...I just need to make sure I get this point across because
                      it is incredibly important.

                      About the square brackets, that came directly from your first post:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <mytext>This text should be bold or italic, please.<mytext>

                      I assumed you were translating the square brackets in your script, but
                      in a subsequent post (and without further explanation), these became tag
                      markers:

                      <?xml version="1.0" encoding="UTF-8"?>
                      <mytext>This <biggy>text</biggy> should be<b>bold</b> or <i>italic</i>,
                      please.</mytext>

                      This is where my confusion came from. There wasn't really any
                      explanation as to what happened in between. Had you created a function
                      to translate the square brackets? Was that just a notation you were using?

                      But that's not really important. I really highly recommend that you make
                      your HTML data CDATA otherwise you're setting yourself up for problems.
                      At some point, the XML parser will remove some vital piece of HTML
                      formatting, or you will run into incompatible tag notation. Either way,
                      the simple solution is CDATA. Or use it as a text file :)

                      Patrick

                      maguskrool wrote:
                      > What I need is for the tags in the .xml to be used to format the text they
                      > enclose, when that text is loaded into the text field in Flash. To that end, I
                      > defined styles in a .css file that Flash also loads.
                      >
                      > Correct me if I'm wrong, but CDATA would cause those tags to be ignored and
                      > read as regular text, right? That's not what I need, I need the tags in the xml
                      > to be recognised and treated as css style "enablers", so the text they enclose
                      > gets the appropriate format.
                      >
                      > Also, I didn't understand when you said:
                      > "Well, in your first example you used square brackets and ,. "
                      >
                      > I'm guessing you meant < b >, but I don't think I used square brackets
                      > anywhere.
                      >
                      > On the brightside, after some more research, I've got this thing working like
                      > I needed and the solution could not be simples - a bit of nonsense, in my
                      > opinion.
                      >
                      > I in the .css file, just remove the . before the classes' names et voil?, the
                      > css styles are applied to the text.
                      >
                      > Thanks for all the help, guys.
                      >

                      --
                      http://www.baynewmedia.com
                      Faster, easier, better...ActionScript development taken to new heights.
                      Download the BNMAPI today. You'll wonder how you ever did without it!
                      Available for ActionScript 2.0/3.0.
                      • 8. html tags in XML won't load into text field
                        Greg Dove Level 4
                        Patrick is right CDATA is best - and it means that you don't get the html parsed as xml when you don't need it to be.

                        But it is also possible to do it with xml so long as all the tags are closed.

                        I think this will help illustrate the current problem and the xml alternative.

                        • 9. html tags in XML won't load into text field
                          maguskrool Level 1
                          Thank you very much for your help!

                          I was having some trouble understanding why things inside CDATA would be then correctly read as .html, but I guess they are. I agree with you, it is best to keep xml and html distinct and avoid future problems. In this particular case I had no xml tags inside any html tags, all were well closed and the access to the xml data was tailored to each particular case. Still, I strive for more elegant and correct code so I'll use your approach.

                          GWD:
                          The one thing that eluded me was why you used the .join("")
                          • 10. Re: html tags in XML won't load into text field
                            Greg Dove Level 4
                            I used the join method for the xml representation of the html because childNodes is an array of xmlNodes - some of which could be textnodes, some could be <b> or <i> or whatever, with their own childnodes. Using join forces the .toString method on each node (which is handy here), and joins them at the array level with an empty string.

                            Again... although this is possible its better to use CDATA (IMO).

                            Another 3rd way I have used is to put the html inside an attribute's value.







                            • 11. Re: html tags in XML won't load into text field
                              vaudor Level 1
                              Hi all

                              Confronted to the problem and wanting to pass a font size tag (plus my client has to modify the xml himself so it needs to be simple) here's what I did : I use my "own" tagging in the xml (in my case, [some text] )

                              in the xml :

                              <item name="[some] text">


                              in flash my textField has a 20 size

                              the code :

                              var small = {fontSize:15};
                              _global.smallText = new TextField.StyleSheet();
                              smallText.setStyle("html", small);

                              after loading your text from xml into my_string :


                              var small = my_string.substring(my_string.lastIndexOf("[")+1, my_string.lastIndexOf("]"));
                              if (small) {
                              textfield.styleSheet = smallText;
                              textfield.html = true;
                              textfield.htmlText = "<html>"+small+"</html>";
                              textfield.autoSize = true;
                              textfield.styleSheet = null;
                              textfield.htmlText += my_string.substring(my_string.lastIndexOf("]")+1);
                              }

                              It works just fine
                              Cheers!