14 Replies Latest reply on May 13, 2009 1:35 PM by jlopes151

    Trouble accessing a child of an XMLList

    jlopes151 Level 1

      I load an XML object with formed xml. I can use the object to trace the xml I can even display the attributes of the parent node. I can get a count of the children using the length().

       

      What I can do is access the data of a child node. What I get if I try xmlobj.child("somechild").toString() is a blank. No error occur.

       

      Thanks for any help/

        • 1. Re: Trouble accessing a child of an XMLList
          Gregory Lafrance Level 6

          Use e4x syntax:

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_03.html

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="dataRequest.send();">
            <mx:Script>
              <![CDATA[
                import mx.rpc.events.ResultEvent;
                import mx.collections.XMLListCollection;
                [Bindable] private var xlc:XMLListCollection;
               
                private function processResult(evt:ResultEvent):void{
                  var xmllist:XMLList = evt.result..item;
                  xlc = new XMLListCollection(xmllist);
                }
              ]]>
            </mx:Script>

            <mx:HTTPService id="dataRequest" url="data.xml" resultFormat="e4x"
              result="processResult(event)"/> 
            <mx:ComboBox dataProvider="{xlc}" labelField="firstData"/>
          </mx:Application>

          • 2. Re: Trouble accessing a child of an XMLList
            jlopes151 Level 1

            I should have explained my problem too frustrated I guess. My project is built around AS. I read an XML file into a string, then new an XML object using the string.

             

             

             

             

            theOPFContent = inStream.readUTFBytes(inStream.bytesAvailable).toString();

            trace(theOPFContent);

            theOPFXML =

            new XML(theOPFContent);

            trace(theOPFXML.children().toXMLString());

            trace

             

             

            (theOPFXML.attributes().toXMLString());

             

            the above trace calls display the xml structure

             

            but if I

            trace(theOPFXML.attributes().child("child").toXMLString());

            the only thing I see in the console is a cairrage return.

            • 3. Re: Trouble accessing a child of an XMLList
              Flex harUI Adobe Employee

              Child() calls the child() method on each XML object in the XMLList.

               

              Attributes() returns essentially an Array of values.  Normally you loop through the results.

               

              To find the value of a particular attribute you call attribute("attrName") or simply use xml.@attrName

               

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Trouble accessing a child of an XMLList
                jlopes151 Level 1

                So how does your reply help? I saying nothing is displayed in the trace when I call the child() method with the name of the child.

                • 5. Re: Trouble accessing a child of an XMLList
                  BennyBeta Level 4

                  Do you mind posting the following:

                   

                  • The XML (or a snippet)
                  • The code to you are using
                  • The output you expect

                   

                  Thanks.

                  • 6. Re: Trouble accessing a child of an XMLList
                    Flex harUI Adobe Employee

                    I'm saying I think you're making a mistake calling child() on attributes().  Please provide example XML and output you expect

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui

                    • 7. Re: Trouble accessing a child of an XMLList
                      jlopes151 Level 1

                      My code I found that the child("node name") doesn't work using the ordinal does. I'm reading the xml file contents.opf

                      What I want to see is the text data of the metadata child.

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                       

                      private function readOPFFile(fileName:String):

                      void

                      {

                       

                       

                      var inStream:FileStream = new FileStream();

                       

                       

                      var inFile:File = targetDir;

                      inFile = inFile.resolvePath(fileName);

                       

                      inStream.open(inFile, FileMode.READ);

                       

                       

                      var metadata:String = "metadata"

                      ;

                       

                       

                       

                      try

                      {

                      theOPFContent = inStream.readUTFBytes(inStream.bytesAvailable).toString();

                      // trace(theOPFContent);

                      theOPFXML =

                       

                      new

                      XML(theOPFContent);

                      // trace(theOPFXML.attributes().toXMLString());

                       

                       

                       

                      trace("The Namespace::Name "

                      + theOPFXML.child(0).child(0).child(0).name()); // I see the child name with namespace

                       

                       

                      trace("The Namespace::Name "

                      + theOPFXML.child(0).child(0).child(1).name());

                       

                       

                      trace

                      (theOPFXML.child(0).child(0).child(0).toString());

                       

                       

                      trace

                      (theOPFXML.children()[0].children()[0].children()[0].toString());

                       

                       

                      trace

                      (theOPFXML.children()[0].children()[0].children()[1].toString());

                      // trace(theOPFXML.children()[1].toXMLString());

                       

                       

                      trace

                      (theOPFXML.children()[1].children()[0].attributes()[1]);

                       

                       

                      trace

                      (theOPFXML.children()[2].children()[0].attributes()[0]);

                       

                       

                      trace

                      (theOPFXML);

                      }

                       

                      catch

                      (e:EOFError){

                       

                       

                      trace

                      (e);

                      • 8. Re: Trouble accessing a child of an XMLList
                        jlopes151 Level 1

                        There was a problem loading the xml file here it is again

                         

                        <?xml version="1.0"?>
                        <!DOCTYPE package
                          PUBLIC "+//ISBN 0-9673008-1-9//DTD OEB 1.2 Package//EN"
                          "http://openebook.org/dtds/oeb-1.2/oebpkg12.dtd">
                        <package xmlns="http://www.idpf.org/2007/opf" unique-identifier="bookid" version="2.0">
                        <metadata>
                          <dc-metadata xmlns:dc="http://purl.org/dc/elements/1.1/"
                        xmlns:dcterms="http://purl.org/dc/terms/"
                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
                           <dc:title>The Golden Age</dc:title>
                           <dc:creator>Kenneth Grahame</dc:creator>
                           <dc:language xsi:type="dcterms:RFC3066">en-GB</dc:language>
                           <dc:rights>Public Domain</dc:rights>
                           <dc:publisher>Gutenberg Foundation</dc:publisher>
                           <dc:identifier id="bookid">urn:uuid:jedisaber.com030720081610</dc:identifier>
                          </dc-metadata>
                        </metadata>
                        <manifest>
                          <item id="ncx" href="toc.ncx" media-type="text/xml"/>
                          <item id="style" href="stylesheet.css" media-type="text/css"/>
                          <item id="pagetemplate" href="page-template.xpgt" media-type="application/vnd.adobe-page-template+xml"/>
                          <item id="titlepage" href="title_page.html" media-type="text/html"/>
                          <item id="copyright" href="copyright.html" media-type="text/html"/>
                          <item id="tableofc" href="toc.html" media-type="text/html"/>
                          <item id="prologue" href="prologue.html" media-type="text/html"/>
                          <item id="chapter01" href="chap01.html" media-type="text/html"/>
                          <item id="chapter02" href="chap02.html" media-type="text/html"/>
                          <item id="chapter03" href="chap03.html" media-type="text/html"/>
                          <item id="chapter04" href="chap04.html" media-type="text/html"/>
                          <item id="chapter05" href="chap05.html" media-type="text/html"/>
                          <item id="chapter06" href="chap06.html" media-type="text/html"/>
                          <item id="chapter07" href="chap07.html" media-type="text/html"/>
                          <item id="chapter08" href="chap08.html" media-type="text/html"/>
                          <item id="chapter09" href="chap09.html" media-type="text/html"/>
                          <item id="chapter10" href="chap10.html" media-type="text/html"/>
                          <item id="chapter11" href="chap11.html" media-type="text/html"/>
                          <item id="chapter12" href="chap12.html" media-type="text/html"/>
                          <item id="chapter13" href="chap13.html" media-type="text/html"/>
                          <item id="chapter14" href="chap14.html" media-type="text/html"/>
                          <item id="chapter15" href="chap15.html" media-type="text/html"/>
                          <item id="chapter16" href="chap16.html" media-type="text/html"/>
                          <item id="chapter17" href="chap17.html" media-type="text/html"/>
                          <item id="legal" href="legal.html" media-type="text/html"/>
                        </manifest>
                        <spine toc="ncx">
                          <itemref idref="titlepage"/>
                          <itemref idref="copyright"/>
                          <itemref idref="tableofc"/>
                          <itemref idref="prologue"/>
                          <itemref idref="chapter01"/>
                          <itemref idref="chapter02"/>
                          <itemref idref="chapter03"/>
                          <itemref idref="chapter04"/>
                          <itemref idref="chapter05"/>
                          <itemref idref="chapter06"/>
                          <itemref idref="chapter07"/>
                          <itemref idref="chapter08"/>
                          <itemref idref="chapter09"/>
                          <itemref idref="chapter10"/>
                          <itemref idref="chapter11"/>
                          <itemref idref="chapter12"/>
                          <itemref idref="chapter13"/>
                          <itemref idref="chapter14"/>
                          <itemref idref="chapter15"/>
                          <itemref idref="chapter16"/>
                          <itemref idref="chapter17"/>
                          <itemref idref="legal"/>
                        </spine>
                        </package>

                        • 9. Re: Trouble accessing a child of an XMLList
                          Flex harUI Adobe Employee

                          The safest way to get the metadata child is via:

                           

                               var metadataTag:XML = theOPFXML.metadata[0]

                           

                          or

                               var metadataTag:XML = theOPFXML.child("metadata")[0];

                           

                          since you can't guarantee the child index of the metadata tag

                           

                          The metadata tag has only children and no attributes so you shouldn't need an attributes() call.  The children of the metadata tag are:

                           

                               var metadataChildren:XMLList = metadataTag.children();

                           

                          All of the text in those children are available as:

                           

                               var metadataChildren:XMLList = metadataTag.text();

                           

                          Alex Harui

                          Flex SDK Developer

                          Adobe Systems Inc.

                          Blog: http://blogs.adobe.com/aharui

                          • 10. Re: Trouble accessing a child of an XMLList
                            jlopes151 Level 1

                            Your suggestion doesn't work. I believe there is a bug in the "name" access to children.

                             

                                 var metadataTag:XML = theOPFXML.metadata[0]

                             

                            or

                                 var metadataTag:XML = theOPFXML.child("metadata")[0];

                             

                            My debuger says theOPFXML.metadata[0] is null.

                             

                            I can access the children with

                             

                             

                             

                             

                            var metadataTag = theOPFXML.child(0);

                             

                             

                            trace("The metadataTag " + metadataTag.children());

                             

                            Using the ordinal seems to be the only way.

                             

                            Thanks for the help seems like Flex QA needs to test this.

                            • 11. Re: Trouble accessing a child of an XMLList
                              jlopes151 Level 1

                              Request confirmation:

                               

                              Flex is Open Source?

                              • 12. Re: Trouble accessing a child of an XMLList
                                jlopes151 Level 1

                                I didn't set the type on the var metadataTag = but it doesn't seem to matter I was able to trace the children.

                                • 13. Re: Trouble accessing a child of an XMLList
                                  Flex harUI Adobe Employee

                                  I didn't notice last night that there is a default xml namespace in play.  I think you'll need to factor that in.  The true name of the metadata tag is QName("http://www.idpf.org/2007/opf", "metadata") or something like that.  If you haven't figured it out and I get more time I'll try to post the exact code.

                                   

                                  This stuff is pretty well debugged, but namespaces make it difficult to get it right.

                                   

                                  Flex is opensource

                                   

                                  Alex Harui

                                  Flex SDK Developer

                                  Adobe Systems Inc.

                                  Blog: http://blogs.adobe.com/aharui

                                  • 14. Re: Trouble accessing a child of an XMLList
                                    jlopes151 Level 1

                                    trace("The Namespace::Name " + theOPFXML.child("http://www.idpf.org/2007/opf::metadata").children());

                                     

                                    I got nothing with this.