16 Replies Latest reply on Mar 7, 2012 5:58 PM by afukada

    xml parsing issue

    afukada

      Given this xml,

       

      <ch>

         <ex>

            <extitle>Sound check</extitle>

            <app>student</app>

        </ex>

      </ch>

      <ch>

      .

      .

      .

      </ch>

       

      my understanding is that in order to fetch "Sound check", either xmldata.ch[0].ex.extitle or xmldata.ch[0].ex[0].extitle will work. However, the second option gives me a run-time error (presumably because I only have one set of <ex>...</ex>). This is inconvenient because sometimes I have more than one set of <ex>...</ex> within a <ch>...</ch>. That means that I have to check how many sets of <ex>...</ex> I have before I can decide how to access them. Can someone suggest a good approach to this?

        • 1. Re: xml parsing issue
          Flex harUI Adobe Employee

          Try xmldata..extitle.toString()

           

          Yes, there are two dots in there.

          • 2. Re: xml parsing issue
            afukada Level 1

            Thanks for the useful trick. I didn't know that one.

            I'm sorry that I didn't make my intention clear.

            What I need is a general parsing routine that works for both of these cases:

             

            1. when there is only one set of <ex>...</ex>

            2. when there are more than one

             

            Do I have to count before tackling <ex>...</ex>? Surely, there's a better way. Maybe not...

            • 3. Re: xml parsing issue
              Flex harUI Adobe Employee

              Can you show me the xml that doesn’t work when there are two “ex” tags?

              • 4. Re: xml parsing issue
                Omererer

                afukada wrote:

                 

                xmldata.ch[0].ex.extitle or xmldata.ch[0].ex[0].extitle will work. However, the second option gives me a run-time error (presumably because I only have one set of <ex>...</ex>).

                There's nothing wrong with the second expression. The run-time error you're getting must be due to something else. If the run-time error is a range error, it could be because your XML doesn't have any ex elements inside the first ch element, or there may not be any ch elements. If you can post the error message, that will help troubleshoot this.

                • 5. Re: xml parsing issue
                  afukada Level 1

                  Here's the error message: TypeError: Error #1010: A term is undefined and has no properties.

                   

                  This XML comes from a server PHP script:

                   

                  <ch>

                    <chtitle>Lecture One</chtitle>

                    <ex>

                      <extitle>Sound check 1</extitle>

                      <items>

                        <item>1</item>

                      </items>

                      <app>student</app>

                    </ex>

                  </ch>

                  <ch>

                    <chtitle>Lecture Two</chtitle>

                    <ex>

                      <extitle>Video check 1</extitle>

                      <items>

                        <item>1</item>

                      </items>

                      <app>student</app>

                    </ex>

                    <ex>

                      <extitle>Video check 2</extitle>

                      <items>

                        <item>1</item>

                      </items>

                      <app>student</app>

                    </ex>

                  </ch>

                   

                   

                  resultObj.ch[0].ex[0].extitle -- error

                  resultObj.ch[0].ex.extitle -- works

                   

                  resultObj.ch[1].ex[0].extitle -- works

                  resultObj.ch[1].ex[1].extitle -- works

                  • 6. Re: xml parsing issue
                    Flex harUI Adobe Employee

                    I don’t have time to try it, but it looks like it should work.  If you don’t use PHP and hardcode this XML into a test app does it still fail?

                    • 7. Re: xml parsing issue
                      afukada Level 1

                      Flex harUI wrote:

                       

                      I don’t have time to try it, but it looks like it should work.  If you don’t use PHP and hardcode this XML into a test app does it still fail?

                       

                      Yes, it does.

                      • 8. Re: xml parsing issue
                        flexercised Level 2

                        Does it work if the xml is wrapped in a single root element i.e.

                         

                        <data>

                          <ch>

                            ...

                          </ch>

                          <ch>

                            ...

                          </ch>

                        </data>

                        • 9. Re: xml parsing issue
                          afukada Level 1

                          flexercised wrote:

                           

                          Does it work if the xml is wrapped in a single root element i.e.

                           

                          <data>

                            <ch>

                              ...

                            </ch>

                            <ch>

                              ...

                            </ch>

                          </data>

                           

                          I just tried. It made no difference.

                          • 10. Re: xml parsing issue
                            Flex harUI Adobe Employee

                            The following test worked for me:

                             

                            
                            package
                            {
                            import flash.display.Sprite;
                            import flash.display.Loader;
                            import flash.events.Event;
                            import flash.events.MouseEvent;
                            import flash.net.URLRequest;
                            import flash.system.System;
                            
                            public class XMLTest extends Sprite
                            {
                            
                                public function XMLTest()
                                {
                                    loaderInfo.addEventListener(Event.INIT, initHandler);
                                }
                            
                                private var child:Sprite;
                                private var child1:Sprite;
                                private var child2:Sprite;
                            
                                private function initHandler(event:Event):void
                               {
                                    child = new Sprite();
                                    addChild(child);
                                    child.graphics.beginFill(0x800000);
                                    child.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
                                    child.graphics.endFill();
                            
                                    var resultObj:XML = <xml>
                            <ch>
                              <chtitle>Lecture One</chtitle>
                              <ex>
                                <extitle>Sound check 1</extitle>
                                <items>
                                  <item>1</item>
                                </items>
                                <app>student</app>
                              </ex>
                            </ch>
                            <ch>
                              <chtitle>Lecture Two</chtitle>
                              <ex>
                                <extitle>Video check 1</extitle>
                                <items>
                                  <item>1</item>
                                </items>
                                <app>student</app>
                              </ex>
                              <ex>
                                <extitle>Video check 2</extitle>
                                <items>
                                  <item>1</item>
                                </items>
                                <app>student</app>
                              </ex>
                            </ch>
                            </xml>
                            
                                    trace(resultObj.ch[0].ex[0].extitle);
                                    trace(resultObj.ch[0].ex.extitle);
                            
                                    trace(resultObj.ch[1].ex[0].extitle);
                                    trace(resultObj.ch[1].ex[1].extitle);
                            
                                }
                            
                            }
                            
                            }
                            
                            

                             

                            • 11. Re: xml parsing issue
                              archemedia Level 4

                              Could it be that the first is converted to an XML object and the other ones to XMLList objects? The difference is that XMLList has more than one root node.

                              Don't have time to dig in though...

                               

                              -Dany

                              • 12. Re: xml parsing issue
                                afukada Level 1

                                Hm... I have a feeling that the problem lies in the way my code receives the xml data. I'm using an HTTPService to fetch data from a PHP server script this way:

                                 

                                <mx:HTTPService

                                   id="homepage"

                                   result="homePageInfo(event)"

                                   ...

                                 

                                Then, I access the xml data this way.

                                 

                                private function homePageInfo(evt:ResultEvent):void

                                {

                                   var resultObj:Object = evt.result;

                                   Alert.show(resultObj.data.ch[0].ex[0].extitle); //ERROR!

                                }

                                 

                                How do I convert an object into an xml data type?

                                • 13. Re: xml parsing issue
                                  Omererer Level 1

                                  You can use the global function XML().

                                   

                                  var xml:XML = XML(event.result);

                                  1 person found this helpful
                                  • 14. Re: xml parsing issue
                                    Sathyamoorthi Level 2

                                    this will surely work,if you returned xml in good structure from PHP.

                                     

                                    var xmlResult:XML = new XML(event.message.body.toString());
                                    1 person found this helpful
                                    • 15. Re: xml parsing issue
                                      Flex harUI Adobe Employee

                                      Are you getting an object? Or String? Or XML?  You can set resultFormat on the HTTPService.

                                      1 person found this helpful
                                      • 16. Re: xml parsing issue
                                        afukada Level 1

                                        resultFormat="e4x" seems the most simple and elegant, and it works wonderfully.

                                         

                                        Thankyou very very much everyone for helping me! You've been wonderful.