4 Replies Latest reply on Nov 5, 2007 9:10 AM by Haravikk the Horse

    Advanced XML connections

    Haravikk the Horse
      Hey there!

      I'm fairly new to Flex development so please excuse my ignorance if I've just managed to miss some feature or API completely!

      My situation is this:
      I wish to develop a program which will communicate with my server via a high-performance XML connection using a (currently proprietary, open-source later) binary XML format. Since I'm the one who has been writing the code for it in Java I have full access to the source to port to Flex/action-script. What I'm wondering is what (if any) is the standard API for XML parsers (readers/writers)? My format is a StAX ("pull") parser for XML streams, and I have classes that allow me to use XPaths efficiently over it.
      I would like very much to make it using a standard Flex API, extending where required to leverage additional features, as this would be more useful I think in future when I release it as an open-source project.

      Further to this, I will be hoping to use this with an SSL connection to keep data private, I am wondering if Flex already has SSL functionality, and how fast it is?

      Please don't dispute the use of binary XML here, since I'm communicating between two parties then readability isn't an issue (except debugging, in which case it's easy to output as text with a third party, or by dropping in a regular XML reader/writer). For my intended data-set I get around a 40-50% reduction in message size, haven't tested relative performance compared to text-XML parsers, but it should be faster since it doesn't need to do much work.

      So I'm looking for an idea of what I should implement or extend and what I will need to develop from scratch to make this work? My aim is to develop this as an AIR application, but may compile for browser as well since I have no major requirement for local storage.

      Cheers
        • 1. Re: Advanced XML connections
          ntsiii Level 3
          ActionScript XML is built around the "Ecmascript for XML" (e4x) standard, if that helps.

          Tracy
          • 2. Re: Advanced XML connections
            Haravikk the Horse Level 1
            Hmm, thanks for the response! While that looks like an interesting way of manipulating XML, it appears to be DOM-based? The method I'm hoping to implement is StAX or "pull" parsing. A simple example (in Java) using my parser could look like this. Note: I'm using hyphens in place of working tabs since there seems to be no equivalent of <pre> on HTML here that I see:

            Example XML:
            <items>
            - <category>Mixed</category>
            - <item>Bread</item>
            - <item>Monkey</item>
            </items>

            Java code (assuming an XMLStreamReader is positioned inside the <items> tag:
            int event;
            while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
            - switch (event) {
            - - case START_ELEMENT:
            - - String name = reader.getLocalName();
            - - if (name.equals("item")) System.out.println("\t"+reader.getElementText());
            - - else if (name.equals("category")) System.out.println(reader.getElementText());
            - - break;
            - }
            }

            The output of the above would appear (as the data arrives) like:
            Mixed
            - Bread
            - Monkey

            Basically, unlike DOM which reads in all the XML and provides you with a model you can then pull elements out from, a StAX parser lets you tell the parser when to parse, it tells you what it found and you can then decide what to do with that data. It's very good for using as an XPath stream parser, as you can look for an XPath match on an XML document hundreds of megabytes in size, without ever using more than a few kilobytes of memory. Disadvantage of course is that once you've passed an XML element, you can't go back unless you saved it somewhere.

            My XMLStreamReader is fairly easy to adapt into use as a DOM parser as well, and it's pretty good at that too, but I try to avoid DOM like the plague where possible.

            If Flex only has DOM available then I would like port my parser as a mirror image of the Java parser, and hope that any future Flex StAX parser standard is similar enough to easily mash my code into at that time.
            • 3. Re: Advanced XML connections
              jlmoller
              If you Base64 your binary data, you can pass it as XML without any issues at all. I move large datasets in this fashion. There are built in Base64 functions in Flex 2.
              • 4. Re: Advanced XML connections
                Haravikk the Horse Level 1
                I'm sorry, but Base64 is insufficient for my needs. My server-side program uses the afore-mentioned binary XML format to transmit its data. I therefore require a way to decode the same data, and was hoping I could this with Flex rather than having to try and get users to adopt a Java application for the front end (not to mention Java's Swing is disgusting and incredibly difficult to work with for no good reason).

                I can't go into the specifics of my implementation on the Java side, but the purpose of it is along the lines of:
                - <message> tag becomes byte value 64
                - I receive byte value 64, and thus decode it into a <message> tag for consumption

                Other benefits on the binary transmission format is that it can allow for rapid skipping of large sections of unwanted data, and hugely optimised XPath filter of results to retrieve matches as they arrive down the stream.