Loading ...
Sorry, an error occurred while loading the content.

Re: [ydn-javascript] Re: Dealing with XML DOM (Newbie)

Expand Messages
  • Ken Robinson
    ... No, the responseXML was not null, but it was not something that looked like what all the references I found led to believe it should look like. I finally
    Message 1 of 22 , Feb 28, 2006
    • 0 Attachment
      At 11:06 AM 2/28/2006, Thomas S. Sha wrote:
      > The Yahoo developers really need to document the
      > structures of these items, give us routines to
      > parse them, or return a regular XML object that
      > can be parsed with the "standard" methods.
      >
      > Ken
      >
      Actually, the connection manager does not transform or modify the
      responseXML values from the native object.

      If you find the responseXML property is null, you are expecting XML,
      did your resource reply with a MIME header of "text/xml"?

      No, the responseXML was not null, but it was not something that looked like what all the references I found led to believe it should look like.

      I finally used a function to dump the contents of the object:

      function dump_props(obj, obj_name) {
               var dbg = document.getElementById('debug');
               cur = dbg.innerHTML;
         var result = "[ "
         for (var i in obj) {
            result += obj_name + "." + i + " = " + obj[i] + "//"
         }
         result += " ]"
         cur += result + '<hr />';
               dbg.innerHTML = cur;
      }

      And discovered that to get the information out of the XML file I created via PHP:


      <?xml version="1.0" standalone="yes"
      ?>
      <events>
         <event>
           <date>01/06/2006</date>
           <org>Ironia Reelers</org>
         </event>
         <event>
           <date>01/20/2006</date>
           <org>Ironia Reelers</org>
         </event>
         <event>
           <date>01/22/2006</date>
           <org>NNJSDA</org>
         </event>
         <event>
           <date>02/03/2006</date>
           <org>Ironia Reelers</org>
         </event>
      </events>
      
      I needed to do this:
                var xml =
      xmlHttp.responseXML;
                var dates
      = xml.documentElement.getElementsByTagName("date");
                var org =
      xml.documentElement.getElementsByTagName("org");
      
      Instead of this:
                var xml =
      xmlHttp.responseXML;
                var dates
      = xml.getElementsByTagName("date");
                var org =
      xml.getElementsByTagName("org");
      
      On February 22, Steven Peterson from Yahoo wrote:
      
      
      responseXML in Connection is returned as an already-parsed tree, so you can use all the usual DOM methods and fields like childNodes, documentElement, etc.

      So you can see it is has been modified.
      
      Ken
      
      
      
       
      
    • Thomas S. Sha
      ... Try replacing your *else if* with this: else if(children[i].nodeName == titel ){ titelNode = children[i].firstChild; // this should return Tanztherapie
      Message 2 of 22 , Feb 28, 2006
      • 0 Attachment
        --- In ydn-javascript@yahoogroups.com, "piaffra_woods"
        <piaffra_woods@...> wrote:

        > <script type="text/javascript"><!--
        > var responseSuccess = function(o){
        > var scheissdreck = o.responseXML;
        > var children = scheissdreck.documentElement.childNodes;
        > var autorNode = false;
        > var titelNode = false;
        > for (var i=0; i<children.length; i++) {
        > // if this node is an element node
        > if (children[i].nodeType == 1) {
        > if (children[i].nodeName == 'autor') {
        > autorNode = children[i].firstChild;
        > }
        > else if (children[i].nodeName == 'titel') {
        > titelNode = children[i];
        > }
        > } // end if element
        > } // end for
        > alert('Titel: ' + titelNode.data); // + ' Autor'.autorNode.nodeValue);
        > }

        Try replacing your *else if* with this:

        else if(children[i].nodeName == 'titel'){
        titelNode = children[i].firstChild;
        // this should return Tanztherapie
        alert(titelNode.nodeValue);
        }

        Regards,
        Thomas

        > --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@> wrote:
        > >
        > > --- In ydn-javascript@yahoogroups.com, Ken Robinson <kenrbnsn@> wrote:
        > > >
        > > > At 09:50 AM 2/28/2006, piaffra_woods wrote:
        > > > >See, this is the problem. Enough stuff out there regarding Dom
        - HTML
        > > > >but i can´t find a neat howto about "standard" dom -methods and
        > > > >(non-html) xml. It is quite absurd (or its my lack of
        > > > >javascript-knowledge) i try since tree days to get the value from a
        > > > >dom-element-node, where i can get the nodeName without any trouble.
        > > > >Maybe someone has an example with yahoos transport library,
        > > > >xml-response and parsing trough this response.
        > > >
        > > > Unfortunately, as I discovered, the xml-response
        > > > from the Yahoo libraries has already been
        > > > transformed to something else and none of the "standard" methods
        work.
        > > >
        > > > The Yahoo developers really need to document the
        > > > structures of these items, give us routines to
        > > > parse them, or return a regular XML object that
        > > > can be parsed with the "standard" methods.
        > > >
        > > > Ken
        > > >
        > > Actually, the connection manager does not transform or modify the
        > > responseXML values from the native object.
        > >
        > > If you find the responseXML property is null, you are expecting XML,
        > > did your resource reply with a MIME header of "text/xml"?
        > >
        > > Regards,
        > > Thomas
        > >
        >
      • Matt Warden
        ... Hash: SHA1 ... Not sure, but might have something to do with your XML being invalid. If I am having problems like this, the first thing I do is make sure
        Message 3 of 22 , Feb 28, 2006
        • 0 Attachment
          -----BEGIN PGP SIGNED MESSAGE-----
          Hash: SHA1

          piaffra_woods wrote:
          > first of all, thank you for your advice. but let´s go into detail. i
          > wrote some php, which put out (yes with a xml-http-header)something
          > like that:
          >
          > <objekt>
          > <autor>
          > <a target="show_autor?id=112">Miriam Daill</a>
          > </autor>
          > <titel>Tanztherapie </titel>
          > <link>"show_article.php?id=22101"</link>
          > </objekt>
          ...
          > any ideas why i get: Titel: undefined? (i tried, .value, .nodeValue,
          > .text usw.)

          Not sure, but might have something to do with your XML being invalid. If
          I am having problems like this, the first thing I do is make sure I'm
          sending a text/xml content-type header, and the second thing I do is
          validate the XML that is being output with an XML validator.

          hth,

          - --
          Matt Warden
          Miami University
          Oxford, OH, USA
          http://mattwarden.com


          This email proudly and graciously contributes to entropy.
          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.1 (GNU/Linux)
          Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

          iD8DBQFEBK20rI3LObhzHRMRAmkNAJ9yDgP3Hgb+nkF+4NLozhNLNMrOdgCfWGaf
          VPYYtzsoOXw6PuA+lBEtmC0=
          =TsL5
          -----END PGP SIGNATURE-----
        • Cecil Vermule
          If you really are getting the message Titel: undefined be sure that you are not referencing it as Titel and that you reference it as titel Look at the
          Message 4 of 22 , Feb 28, 2006
          • 0 Attachment
            If you really are getting the message "Titel: undefined" be sure that you are not referencing it as "Titel" and that you reference it as "titel"
             
            Look at the case sensitivity.

             
            On 2/28/06, Matt Warden <mwarden@...> wrote:
            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            piaffra_woods wrote:
            > first of all, thank you for your advice. but let´s  go into detail. i
            > wrote some php, which put out (yes with a xml-http-header)something
            > like that:
            >
            > <objekt>
            > <autor>
            > <a target="show_autor?id=112">Miriam Daill</a>
            > </autor>
            > <titel>Tanztherapie </titel>
            > <link>"show_article.php?id=22101"</link>
            > </objekt>
            ...
            > any ideas why i get: Titel: undefined?  (i tried, .value, .nodeValue,
            > .text usw.)

            Not sure, but might have something to do with your XML being invalid. If
            I am having problems like this, the first thing I do is make sure I'm
            sending a text/xml content-type header, and the second thing I do is
            validate the XML that is being output with an XML validator.

            hth,

            - --
            Matt Warden
            Miami University
            Oxford, OH, USA
            http://mattwarden.com


            This email proudly and graciously contributes to entropy.
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.1 (GNU/Linux)
            Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

            iD8DBQFEBK20rI3LObhzHRMRAmkNAJ9yDgP3Hgb+nkF+4NLozhNLNMrOdgCfWGaf
            VPYYtzsoOXw6PuA+lBEtmC0=
            =TsL5
            -----END PGP SIGNATURE-----



            Yahoo! Groups Links

            <*> To visit your group on the web, go to:
               http://groups.yahoo.com/group/ydn-javascript/

            <*> To unsubscribe from this group, send an email to:
               ydn-javascript-unsubscribe@yahoogroups.com

            <*> Your use of Yahoo! Groups is subject to:
               http://docs.yahoo.com/info/terms/




          • Thomas S. Sha
            ... In the event I missed something, I tested your posted XMLdocument and your accessor code -- both using the connection manager and a native XHR object(as
            Message 5 of 22 , Feb 28, 2006
            • 0 Attachment
              --- In ydn-javascript@yahoogroups.com, Ken Robinson <kenrbnsn@...>

              > And discovered that to get the information out of the XML file I
              > created via PHP:
              >
              >
              > <?xml version="1.0" standalone="yes" ?>
              > <events>
              > <event>
              > <date>01/06/2006</date>
              > <org>Ironia Reelers</org>
              > </event>
              > <event>
              > <date>01/20/2006</date>
              > <org>Ironia Reelers</org>
              > </event>
              > <event>
              > <date>01/22/2006</date>
              > <org>NNJSDA</org>
              > </event>
              > <event>
              > <date>02/03/2006</date>
              > <org>Ironia Reelers</org>
              > </event>
              > </events>
              >
              > I needed to do this:
              > var xml = xmlHttp.responseXML;
              > var dates = xml.documentElement.getElementsByTagName("date");
              > var org = xml.documentElement.getElementsByTagName("org");
              >
              > Instead of this:
              > var xml = xmlHttp.responseXML;
              > var dates = xml.getElementsByTagName("date");
              > var org = xml.getElementsByTagName("org");

              In the event I missed something, I tested your posted XMLdocument and
              your accessor code -- both using the connection manager and a native
              XHR object(as the control) to produce the following outcome:

              *accessor code*
              var root = o.responseXML;
              var dateNode = root.getElementsByTagName('date');
              var orgNode = root.getElementsByTagName('org');

              var xml is a node type 9 and nodeName of #document
              var dates is a collection of 4 elements
              var org is a collection of 4 elements

              var dates[0 - 3] reference the elements in the collection and I can
              access the following:

              dates[0].nodeName gives me 'date'
              dates[0].firstChild.nodeValue gives me '01/06/2006'

              These are the results you were expecting, correct?

              > On February 22, Steven Peterson from Yahoo wrote:
              >
              > responseXML in Connection is returned as an already-parsed tree, so
              > you can use all the usual DOM methods and fields like childNodes,
              > documentElement, etc.
              >
              > So you can see it is has been modified.

              This is what the native XHR object provides in responseXML, a
              XMLDocument of nodeType = 9.

              If I missed something, please feel free to post additional code and
              I'll be happy to help dig in further to see what's happening.

              Regards,
              Thomas
            • Ken Robinson
              ... When it is shown like that, it s easy, but the problem here is that not all of us are expert Javascript coders. I, for one, am very good at PHP4 and very
              Message 6 of 22 , Feb 28, 2006
              • 0 Attachment
                At 03:27 PM 2/28/2006, Thomas S. Sha wrote:
                In the event I missed something, I tested your posted XMLdocument and
                your accessor code -- both using the connection manager and a native
                XHR object(as the control) to produce the following outcome:

                *accessor code*
                var root = o.responseXML;
                var dateNode = root.getElementsByTagName('date');
                var orgNode = root.getElementsByTagName('org');

                var xml is a node type 9 and nodeName of #document
                var dates is a collection of 4 elements
                var org is a collection of 4 elements

                var dates[0 - 3] reference the elements in the collection and I can
                access the following:

                dates[0].nodeName gives me 'date'
                dates[0].firstChild.nodeValue gives me '01/06/2006'

                These are the results you were expecting, correct?

                When it is shown like that, it's easy, but the problem here is that not all of us are expert Javascript coders. I, for one, am very good at PHP4 and very weak at Javascript. When I write a script I "think in PHP" and the try to translate that to Javascrpt. A lot of the concepts just don't translate well.

                If you could point to a document or web site where the structures are defined it would be helpful.

                BTW, I gave up on the XML for a while and went to generating everything I needed in PHP and using the PHP to write the Javascript.
                < http://www.nnjsda.org/calendar_test/index3db.php>

                Thanks for your time and effort.

                Ken
              • Matt Warden
                ... Hash: SHA1 ... It s standard DOM, which means if you really want to, you *could* think in PHP : http://php.net/domxml e.g.:
                Message 7 of 22 , Feb 28, 2006
                • 0 Attachment
                  -----BEGIN PGP SIGNED MESSAGE-----
                  Hash: SHA1

                  Ken Robinson wrote:
                  > When it is shown like that, it's easy, but the problem here is that not
                  > all of us are expert Javascript coders. I, for one, am very good at PHP4
                  > and very weak at Javascript. When I write a script I "think in PHP" and
                  > the try to translate that to Javascrpt. A lot of the concepts just don't
                  > translate well.
                  >
                  > If you could point to a document or web site where the structures are
                  > defined it would be helpful.

                  It's standard DOM, which means if you really want to, you *could* "think
                  in PHP":

                  http://php.net/domxml

                  e.g.: http://php.net/domdocument-get-elements-by-tagname

                  - --
                  Matt Warden
                  Miami University
                  Oxford, OH, USA
                  http://mattwarden.com


                  This email proudly and graciously contributes to entropy.
                  -----BEGIN PGP SIGNATURE-----
                  Version: GnuPG v1.4.1 (GNU/Linux)
                  Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

                  iD8DBQFEBMEarI3LObhzHRMRAl2eAJ45ktRrg7epdG64Y8O5nFSvc5n/tgCfaDMH
                  EI2/nkfQ62XT+brz2hUlEDE=
                  =3ek7
                  -----END PGP SIGNATURE-----
                • piaffra_woods
                  Thank you, it works that way. Nevertheless i ask myself why, because, i thought, i am already in the node , so why firstChild. But that´s certainly my lack
                  Message 8 of 22 , Mar 1, 2006
                  • 0 Attachment
                    Thank you, it works that way. Nevertheless i ask myself why, because,
                    i thought, i am already in "the node", so why firstChild. But that´s
                    certainly my lack of dom-knowledge.

                    --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@...> wrote:
                    >
                    > --- In ydn-javascript@yahoogroups.com, "piaffra_woods"
                    > <piaffra_woods@> wrote:
                    >
                    > > <script type="text/javascript"><!--
                    > > var responseSuccess = function(o){
                    > > var scheissdreck = o.responseXML;
                    > > var children = scheissdreck.documentElement.childNodes;
                    > > var autorNode = false;
                    > > var titelNode = false;
                    > > for (var i=0; i<children.length; i++) {
                    > > // if this node is an element node
                    > > if (children[i].nodeType == 1) {
                    > > if (children[i].nodeName == 'autor') {
                    > > autorNode = children[i].firstChild;
                    > > }
                    > > else if (children[i].nodeName == 'titel') {
                    > > titelNode = children[i];
                    > > }
                    > > } // end if element
                    > > } // end for
                    > > alert('Titel: ' + titelNode.data); // + '
                    Autor'.autorNode.nodeValue);
                    > > }
                    >
                    > Try replacing your *else if* with this:
                    >
                    > else if(children[i].nodeName == 'titel'){
                    > titelNode = children[i].firstChild;
                    > // this should return Tanztherapie
                    > alert(titelNode.nodeValue);
                    > }
                    >
                    > Regards,
                    > Thomas
                    >
                    > > --- In ydn-javascript@yahoogroups.com, "Thomas S. Sha" <tsha@> wrote:
                    > > >
                    > > > --- In ydn-javascript@yahoogroups.com, Ken Robinson <kenrbnsn@>
                    wrote:
                    > > > >
                    > > > > At 09:50 AM 2/28/2006, piaffra_woods wrote:
                    > > > > >See, this is the problem. Enough stuff out there regarding Dom
                    > - HTML
                    > > > > >but i can´t find a neat howto about "standard" dom -methods and
                    > > > > >(non-html) xml. It is quite absurd (or its my lack of
                    > > > > >javascript-knowledge) i try since tree days to get the value
                    from a
                    > > > > >dom-element-node, where i can get the nodeName without any
                    trouble.
                    > > > > >Maybe someone has an example with yahoos transport library,
                    > > > > >xml-response and parsing trough this response.
                    > > > >
                    > > > > Unfortunately, as I discovered, the xml-response
                    > > > > from the Yahoo libraries has already been
                    > > > > transformed to something else and none of the "standard" methods
                    > work.
                    > > > >
                    > > > > The Yahoo developers really need to document the
                    > > > > structures of these items, give us routines to
                    > > > > parse them, or return a regular XML object that
                    > > > > can be parsed with the "standard" methods.
                    > > > >
                    > > > > Ken
                    > > > >
                    > > > Actually, the connection manager does not transform or modify the
                    > > > responseXML values from the native object.
                    > > >
                    > > > If you find the responseXML property is null, you are expecting XML,
                    > > > did your resource reply with a MIME header of "text/xml"?
                    > > >
                    > > > Regards,
                    > > > Thomas
                    > > >
                    > >
                    >
                  • Nige White
                    ... The titel node is an Element, *not* a text node. It represents the tag. Anything between that tag and its matching end tag *including text* is a
                    Message 9 of 22 , Mar 1, 2006
                    • 0 Attachment
                      piaffra_woods wrote:

                      > Thank you, it works that way. Nevertheless i ask myself why, because,
                      > i thought, i am already in "the node", so why firstChild. But that´s
                      > certainly my lack of dom-knowledge.


                      The titel node is an Element, *not* a text node.

                      It represents the <titel> tag. Anything between that tag and its
                      matching end tag *including text* is a child node of that element.


                      _____________________________________________________________________
                      This message has been checked for all known viruses. Virus scanning
                      powered by Messagelabs http://www.messagelabs.com For more information
                      e-mail : hostmaster@...
                    Your message has been successfully submitted and would be delivered to recipients shortly.