7 Replies Latest reply on Oct 18, 2007 8:19 AM by djbagley

    Flex and creating XML

    djbagley
      Hi

      I'm having problems finding some good examples on creating XML documents in FLEX.

      What I need to do is I have piece of XML like this.

      var xml:XML =
      <RegistryInterface >
      <Header>
      <ID>JD014</ID>
      <Test>false</Test>
      <Prepared>2006-08-11T09:30:47-05:00</Prepared>
      <Sender id="WB">
      </Sender>
      </Header>
      <QueryStructureRequest resolveReferences="false">
      <DataflowRef>
      <registry:URN></registry:URN>
      </DataflowRef>
      </QueryStructureRequest>
      </RegistryInterface>;

      what I need to do is set the text in the <registry:URN tag>.

      I'm having problem walking the tree to set the text in the tag.

      If anyone’s got some information on how I would do this I would really appreciate it.

      Thanks in advance

      Dan

        • 1. Re: Flex and creating XML
          ntsiii Level 3
          If you want to set it in-line:
          var sURN:String = "whatever";
          var xml:XML = ...
          <registry:URN>{sURN}</registry:URN>
          ..

          To find and set it later (assuming the "xml" variable is in scope)
          xml.QueryStructureRequest .registry::URN = "whatever"

          I think your problem is the namespace on the URN xml tag. Do you require that?

          Tracy
          • 2. Re: Flex and creating XML
            ChristopherCesarz
            The cleanest way is to keep it in a separate file.

            <mx:Script>
            <![CDATA[
            private var _url:String;
            private var urlRequest:URLRequest;
            private var urlLoader:URLLoader;
            private var xmlData:XML;

            private function init():void //Call the initializer from the application tag

            {
            _url = new String("Filelocation.xml");
            urlRequest = new URLRequest(_url);
            urlLoader = new URLLoader(urlRequest);

            urlLoader.addEventListener(Event.COMPLETE, loadXML);
            }
            private function loadXML():void
            {
            xmlData = XML(urlLoader.data);

            //To get an item from it.
            var a:String = xmlData.child(0).Author; //Extract element 0 "Author" from xmlData
            var t:String = xmlData.child(0).Title; //Extract element 0 "Title" from xmlData

            Alert.show("The author of element 0 is: " + a); //Show "Author" string
            Alert.show("The title of element 0 is: " + t); //Show "Title" string

            /* To show all of the elements, do this
            var allItems:String = new String("");
            for(var c:uint = 0; c < xmlData.children().length(); c++)
            {
            allItems = allItems + xmlData.child(c).Author + " " + xmlData.child(c).Title + " ";
            }
            }

            ]]>
            </mx:Script>









            XML File...

            <?xml version="1.0" encoding="utf-8"?>

            <Elements> (You must have a parent container tag)


            <Element> (You must have child containers.)
            <Title>A Brief History of Time</Title>
            <Author>Stephen Hawking</Author>
            </Element>

            <Element>
            <Title>Hyperspace</Title>
            <Author>Michio Kaku</Author>
            </Element>


            </Elements>

            Note: If you receive some sort of 2148 or 2048 error, do the following below.
            -Right-click on the project
            -Click on properties
            -Click on Flex Compiler
            -In the additional compiler arguments box, type -use-network=false. Put a space between the first argument and the typed one or else the new argument will not be accepted.



            Good luck!

            --Christopher--
            • 3. Re: Flex and creating XML
              ChristopherCesarz Level 1
              Oops. It should be "private function loadXML(e:Event):void instead of "private function loadXML():void"
              • 4. Re: Flex and creating XML
                ntsiii Level 3
                Christopher's advice is good if your xml is static.

                My personal preference is for HTTPService over URLLoader. It is essentially the same, in fact it uses URLLoader internally, but is a bit easier to code, especially if you are more comfortable in mxml.

                Tracy
                • 5. Re: Flex and creating XML
                  djbagley Level 1
                  THANKS.

                  Well that sides all working, but unfortunately I do need the namespaces and they are making things more complicated that it needs to be, but thanks all works.
                  I was trying similar code and although I love Flex and think the results are great I am a bit disappointed by the lack of error reporting when trying to navigate complex xml. For someone new to pick up it’s a bit confusing when everything runs and it just does nothing when there's an error in xml path.

                  Also interesting to see that you can embed var’s in strings that should be very useful in the future.

                  Thanks for your responses.

                  D
                  • 6. Re: Flex and creating XML
                    ntsiii Level 3
                    "...embed var’s in strings ..." That only works in assigning "literal" xml to a variable.

                    When working with XML, if the path is not extremely simple, I usually build the path a step at a time, checking each step using trace(toXMLString())
                    trace(myXML.toXMLString())
                    var xmlTemp:XML = myXML.FirstPathStep;
                    trace(xmlTemp.toXMLString())
                    xmlTemp = xmlTemp.NextPathStep;
                    trace....

                    Tracy
                    • 7. Re: Flex and creating XML
                      djbagley Level 1
                      Good advice I think I'll to do that.

                      Many thanks

                      D