7 Replies Latest reply on Apr 20, 2007 1:31 PM by MadamZuZu

    XML In Flex

    Kai001 Level 1
      Hey everyone,

      I want to try to make a simply page in Flex that displays the latest news about my website in 3 label field from my XML file. The first label is for the title, the second for the date and the third for the story. I was curious as to how to import an XML file into flex and display the fields in these labels.

      Here is my XML:
      <?xml version="1.0" encoding="utf-8"?>
      <NewsStorys>
      <NewsStory>
      <Title>First Story!</Title>
      <Date>2007-04-19</Date>
      <Story>Heres the story!</Story>
      </NewsStory>
      </NewsStorys>

      I came here to find out because I tend to have some trouble understanding the flex help file...

      Any help is greatly appreciated!
        • 1. Re: XML In Flex
          Kai001 Level 1
          Sorry for double posting, but I know that alot of people know how to do this. How come I can't find it out? XD
          • 2. Re: XML In Flex
            MadamZuZu
            displaying in a LIST

            <mx:Model id="MyXML" source="YourFile.xml" />

            <mx:List id="QAList" width="100%" color="#004080"
            dataProvider="{MyXML.NewsStory}" />

            hope this helps :)

            • 3. XML In Flex
              Kai001 Level 1
              Thanks! :D This helped me immensely.

              I never knew about the <mx:model> tag and how simple it is to utilize it. Thanks again! :D

              EDIT: Actually I have one more side question. I can put the titles of the storys in the listbox by using MyXML.NewsStory.Title, but if I put more than one record in the XML file it doesn't display anything. Is it because I don't confirm an index? Such as NewsStory.Title[7]?
              • 4. Re: XML In Flex
                MadamZuZu Level 1
                i have no idea :) i'v just learned how to use XML yesterday myself :)
                • 5. Re: XML In Flex
                  MadamZuZu Level 1
                  hm... but mine works, and i have more than 1... here is my source.... maybe it'll help :)

                  <mx:Script>
                  <![CDATA[
                  [Bindable]
                  public var selectedItem:Object;

                  ]]>
                  </mx:Script>

                  <mx:Model id="MyQuestions" source="FAQ.xml" />
                  <mx:Label text="Desktop Support Billing System - FAQ" id="lblTitle" fontFamily="Arial" fontSize="23" fontWeight="bold"/>



                  <mx:Panel height="250" width="600"
                  paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10" id="panel1" cornerRadius="5" fontSize="10" horizontalAlign="center">

                  <mx:List id="QAList" width="100%" color="#004080"
                  dataProvider="{MyQuestions.state}"
                  change="this.selectedItem=List(event.target).selectedItem" click="currentState='answer'"/>

                  </mx:Panel>
                  • 6. XML In Flex
                    Kai001 Level 1
                    Well I can't seem to get it working with more than one record still. I think maybe I've formatted my XML improperly. Could you show me what your XML looks like or?

                    Also, I'm not sure on the details but I switched to an HTTP service, apparently then the XML can be reloaded at runtime if needed.

                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="srv.send()">
                    <mx:HTTPService id="srv" url="MyFlexXMLFile.xml"/>
                    <mx:List id="QAList" width="100%" color="#004080" dataProvider="{srv.lastResult.NewsStorys.NewsStory.Title}" />
                    </mx:Application>

                    Its fine with 1 record but as soon as I add another it breaks..

                    Example works:
                    <?xml version="1.0" encoding="ISO-8859-1"?>
                    <NewsStorys>
                    <NewsStory>
                    <Title>My First News Story!</Title>
                    <Date>2007-04-20</Date>
                    <Story>The story!</Story>
                    </NewsStory>
                    </NewsStorys>

                    Doesn't work:
                    <?xml version="1.0" encoding="ISO-8859-1"?>
                    <NewsStorys>
                    <NewsStory>
                    <Title>My First News Story!</Title>
                    <Date>2007-04-20</Date>
                    <Story>The story!</Story>
                    </NewsStory>
                    <NewsStory>
                    <Title>My Second News Story!</Title>
                    <Date>2007-04-20</Date>
                    <Story>The second story!</Story>
                    </NewsStory>
                    </NewsStorys>

                    EDIT: If I put multiple title tags under the same NewsStory tag it works, for example:
                    <?xml version="1.0" encoding="ISO-8859-1"?>
                    <NewsStorys>
                    <NewsStory>
                    <Title>My First News Story!</Title>
                    <Title>My Second News Story!</Title>
                    <Date>2007-04-20</Date>
                    <Story>The story!</Story>
                    </NewsStory>
                    </NewsStorys>

                    But this isn't proper formatting as you can see. :P

                    Also I removed your first post as the answer because the question has changed. :D
                    • 7. Re: XML In Flex
                      MadamZuZu Level 1
                      ooo, yea MY xml is different. here is what it looks like:

                      <states>
                      <state label="How do i reset my password?" data="call iTAC" data2=""/>
                      <state label="When does my password expire?" data="never" data2="this is a second paragraph"/>
                      <state label="How do i search for a ticket?" data="do this and that" data2=""/>
                      <state label="When does my password expire?" data="never" data2="this is a second paragraph"/>
                      <state label="How do i search for a ticket?" data="do this and that" data2=""/>
                      <state label="When does my password expire?" data="never" data2="this is a second paragraph"/>
                      <state label="How do i search for a ticket?" data="do this and that" data2=""/>
                      <state label="When does my password expire?" data="never" data2="this is a second paragraph"/>
                      <state label="How do i search for a ticket?" data="do this and that" data2=""/>
                      <state label="How do i search for a ticket?" data="do this suuuuuuuupppppppppeeeeeeeeeeeerrrrrrr \n\r
                      llllloooooooooooonnnnnnnnnggggggg aaaaaaaaaaaaaaannnnnnnnnnnnnssssssssswwwwwwwwwww \n\r
                      wwwweeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrr and that" data2=""/>
                      </states>


                      so, basically, i hae questions aka label dispayed in the list box.
                      when you click on it, i display "data" in the label below like so:

                      <mx:VBox width="100%">
                      <mx:Text text="Q: {QAList.selectedItem.label}" width="350" fontWeight="bold"/>
                      <mx:Text text="A: {QAList.selectedItem.data}" width="350" />
                      </mx:VBox>


                      let me know if your still having problems, ill try to help :)