7 Replies Latest reply on Nov 30, 2007 7:34 AM by peterent

    Working with mixed-content XML

    soup_pot
      Am new to Flex 2 and AS 3 but well versed in XML and related technologies. This is my first time posting to the forum.

      I have a simple Flex application that loads an external XML file via HTTPService and binds the XML to some MXML form controls. This works well as long as the XML element contains simple text, but breaks when the element contains a mixture of text and other elements (e.g. mixed-content).

      <something>some text</something> (Fine)
      <something>some <b>bold</b> text</something> (Breaks).

      Just wondering if anyone has could point me to an example of a Flex application interpreting mixed content XML. I've looked at various forums and in books and there isn't a lot on the subject from what I can see. Most of the XML examples I've found use flat, database/table-style XML which don't suit my purposes.

      Thanks

      Heather
        • 1. Re: Working with mixed-content XML
          peterent Level 2
          Are you saying that <mx:Text text="{xmlvar.something}" ... /> isn't working? If you could post some of the MXML it would be helpful. But what I think your XML should be is:

          <something><![CDATA[some <b>bold</b> text]]></something>
          • 2. Re: Working with mixed-content XML
            soup_pot Level 1
            Thanks for the reply. Changing the XML source isn't an option. To give some background, the content is a news feature article. I'm trying to load multiple XML paragraphs from the body of the article into a RichtextEditor control.

            MXML:
            <mx:RichTextEditor htmlText="{featureData.lastResult.content.body_text.p}" ... />

            XML:
            <body_text>
            <p>body text body text body text body text body text body text body text.</p>
            <p>here is some <b>bold</b> text. and some <i>italic</i> text. </p>
            </body_text>

            Flex result is:
            body text body text body text body text body text body text body text. [object Object]

            I wasn't surprised when it didn't work as I figured I'd probably have to do some fiddling, but I wasn't able to find any resources that might help me figure out what I needed to do... :-)
            • 3. Re: Working with mixed-content XML
              peterent Level 2
              When treating your information as XML (or perhaps you mean XHTML?), IE will parse it.

              So while it does parse as XML, it really isn't the way to send HTML content within XML - that's what CDATA is used for (and why we use it in MXML.

              However, if you want the RTE to use it, perhaps load your results using resultFormat="text" on the HTTPSerivce.
              • 4. Re: Working with mixed-content XML
                soup_pot Level 1
                Thanks for the replies. I don't think I fully understand the inner workings so I'll continue to play with this and try to incorporate some of your suggestions. Thanks again.

                Heather
                • 5. Re: Working with mixed-content XML
                  peterent Level 2
                  Let's not give up here. The problem is that you have HTML inside of an XML structure and the HTML, because its syntax is just like XML, cannot be distinguished by the XML parser. The proper way to get your HTML embedded in the XML is to use CDATA. So whoever created the XML didn't take that into account.

                  I gave this a bit more thought. This will work ONLY if the HTML inside of the XML is always complete. No <br> tags without a </br>; no <p> without </p> etc. as that won't be readable by the XML parser.

                  Once you've got your XML structure in Flex, you can get all of the <something> items like this:

                  var somethings:XMLList = xmlvar.something;

                  Now you have an XMLList - an Array of XML structures. This means somethings[0] is "some text" but somethings[1] is an XML node with a sub-structure which includes the <b> node. I hope you are withme so far.

                  Now try this: var sometext:String = XML( somethings[1] ).toString();

                  The toString() method should flatten the contents back into a string and you can assign that to the htmlText property of the control.

                  As I said, your XML has to be perfect for this to work. Or you have to convince the author(s) of the XML to use CDATA to enclose the HTML.
                  • 6. Re: Working with mixed-content XML
                    soup_pot Level 1
                    Ah. So how does it know that the tagging is HTML and not XML - by the htmlText attribute on the RTE? Or just by the presence of the HTML-y tags like <p> and <b>? I can confirm that the 'HTML' will always be complete so that shouldn't be a problem. The XMLList approach does make sense. I will give that a try and let you know if it works!

                    Thanks
                    • 7. Re: Working with mixed-content XML
                      peterent Level 2
                      Wherever you are getting your data from - the server on the other end of the HTTPService call - would have to assemble the XML correctly. If the server software were PHP, for example, and retrieving the information from a database, the PHP would form the XML and surround the query results with CDATA data tags as it assembled the XML response.