6 Replies Latest reply on Oct 11, 2007 7:32 AM by Peter deHaan

    Trouble with external XML in Flex 3 Beta 2

    Eratosthenes_Junior
      Here is a small MXML application test:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">
      <mx:XML id="lfx" format="e4x" source="bug1.xml" />
      </mx:Application>

      The associated bug1.xml file looks like this:

      <?xml version='1.0' ?>
      <a>Outer text<b>Inner text</b></a>

      When I compile this with mxmlc (command-line compiler), I get the
      following:

      bug1.mxml(3): Error: Problem parsing external XML: bug1.xml
      <mx:XML id="lfx" format="e4x" source="bug1.xml" />
      bug1.mxml(2): Error: Encountered "<b>" at line 2, column 14.
      Was expecting:
      Non-MXML language element ...

      If I remove "Outer text" from the xml file, the compilation works fine.
      It also works if I restructure the external file as ActionScript (using
      e4x to specify the same XML).

      I have Flex Builder 3 Beta 2 (but I'm using the command-line tools
      from the included SDK). The same error showed up in the Beta 1
      version.

      This looks like a bug to me. Am I doing something wrong
      here? (This is a boiled-down example from a larger project.)

      Regards,

      Jeff Scofield
      a.k.a. Eratosthenes Junior

        • 1. Trouble with external XML in Flex 3 Beta 2
          ebelair
          This is not bug. Your XML format is not correct.

          Are the "<b>" and "</b>" tags representing HTML Bold tags? If so, you will need to escape the less than ("<") and greater than (">") characters, since these are seen as XML tags when the XML is parsed. Replace these characters as follows:

          < = &lt;
          > = &gt;

          If you are using ColdFusion to generate your XML file, you should always use the <CFXML> tag to build the XML. This will verify that the format of your XML is correct - an CF exception will be thrown if the format is invalid. Most strings won't be affected by the XMLFormat() function, but it's better to have it in there, than not. Also, you should wrap every XML Value in an XMLFormat() function, which escapes all characters that XML cannot parse. Here's what your code might look like:

          <cfxml variable="myXML">
          <a><cfoutput>#XMLFormat("Outer text<b>Inner text</b>")#</cfoutput></a>
          </cfxml>

          Your XML document would then look like this:

          <?xml version='1.0' ?>
          <a>Outer text&lt;b&gt;Inner text&lt;/b&gt;</a>

          It should be noted, however, that escaping characters in XML is not always recommended ( http://www.xml.com/pub/a/2003/08/20/embedded.html ).

          Otherwise, if "<b>" is actually an XML tag, "Outer text" should instead either be an attribute of <a>, or a child tag of <a>:

          <?xml version='1.0' ?>
          <a x="Outer text"><b>Inner text</b></a>

          OR

          <?xml version='1.0' ?>
          <a><x>Outer text</x><b>Inner text</b></a>
          • 2. Re: Trouble with external XML in Flex 3 Beta 2
            MorphicBen
            Eratosthenes,
            You may want to change your result format to "xml" instead of "e4x". Whenever you set your result format to e4x Flash/Flex wants to serialize that xml into an object. Having a potential "object" inside of a string causes parsing errors.

            Let me know if this works....
            • 3. Re: Trouble with external XML in Flex 3 Beta 2
              Eratosthenes_Junior Level 1
              quote:

              Originally posted by: ebelair
              This is not bug. Your XML format is not correct.

              Otherwise, if "<b>" is actually an XML tag, "Outer text" should instead either be an attribute of <a>, or a child tag of <a>:


              Yes, <b> is actually an XML tag. (This is a boiled-down example with completely
              fake tags, just for discussion.)

              I don't see why you say that "Outer text" must be an attribute of a or a child tag of a. I see
              many examples of XML where this is not the case. In essence, the children of a node
              can be a mixture of text nodes and elements. Furthermore, this structure is accepted when
              expressed as e4x. Do you have a link that explains this limitation?

              Thanks very much for your help.

              Jeff Scofield
              a.k.a. Eratosthenes Junior

              • 4. Re: Trouble with external XML in Flex 3 Beta 2
                Eratosthenes_Junior Level 1
                quote:

                Originally posted by: Ben8236
                You may want to change your result format to "xml" instead of "e4x". Whenever you set your result format to e4x Flash/Flex wants to serialize that xml into an object. Having a potential "object" inside of a string causes parsing errors.



                Unfortunately, the behavior is the same with format="xml" as it is with
                format="e4x". As I understand it, both of these create objects. One creates the backward-
                compatible kind of object named XMLNode. The other creates the newer (e4x compatible)
                kind of object now named XML.

                Thanks for the suggestion; I appreciate it.

                Jeff Scofield
                a.k.a. Eratosthenes Junior

                • 5. Re: Trouble with external XML in Flex 3 Beta 2
                  Eratosthenes_Junior Level 1
                  quote:

                  Originally posted by: ebelair
                  Otherwise, if "<b>" is actually an XML tag, "Outer text" should instead either be an attribute of <a>, or a child tag of <a>:



                  This isn't the way I thought that XML works, and so I was motivated to look at the definition of XML 1.0. If
                  you look at Section 3.1 of http://www.w3.org/TR/REC-xml/ you'll see that the content of a tag can consist
                  of any mixture of text (known as "character data") and nested tags. Look for the definition of "content". So,
                  I claim that my XML is well formed. Here it is again for reference:

                  <a>Outer text<b>Inner text</b></a>

                  Any further insight is very welcome.

                  Regards,

                  Jeff Scofield
                  a.k.a. Eratosthenes Junior

                  • 6. Re: Trouble with external XML in Flex 3 Beta 2
                    Peter deHaan Level 4
                    Eratosthenes Junior/Jeff,

                    I saved your XML file to my desktop and IE seems to say its valid (as do many online validators). I also see the same error as you when using this snippet:
                    <mx:XML id="lfx" format="e4x" source="bug1.xml" />

                    But it seems that you can easily work around the issue by loading the XML file using the HTTPService tag to load the XML (code below for those who care).

                    Would you mind filing a bug in the public bugbase and we can investigate it internally. http://bugs.adobe.com/flex/

                    Thanks,
                    Peter

                    [code]
                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="serv.send()">

                    <mx:XML id="myXML" />
                    <mx:HTTPService id="serv" url="bug1.xml" resultFormat="e4x" result="myXML = XML(serv.lastResult);" />

                    <mx:Form>
                    <mx:FormItem label="outer:">
                    <mx:Label text="{myXML.text()}" />
                    </mx:FormItem>
                    <mx:FormItem label="inner:">
                    <mx:Label text="{myXML.b.text()}" />
                    </mx:FormItem>
                    <mx:FormItem label="XML:">
                    <mx:Text text="{myXML.toXMLString()}" />
                    </mx:FormItem>
                    </mx:Form>

                    </mx:Application>
                    [/code]