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

Dealing with XML DOM (Newbie)

Expand Messages
  • Jim
    I am pretty much a newbie to XML, atleast with dealing with the XML DOM. I am having troubling getting the xml tag names and values. Given the simple xml:
    Message 1 of 22 , Feb 17, 2006
      I am pretty much a newbie to XML, atleast with dealing with the XML DOM.  I am having troubling getting the xml tag names and values.  Given the simple xml:

      <apples>
          <apple>
              <color>Red</color>
              <size>Big</size>
          </apple>
      </apples>

      I get the xml response using YAHOO.util.Connect.asyncRequest
      xmlDoc = o.responseXML;

      The following will give me "apples"
      xmlDoc.documentElement.nodeName;

      But the following gives me "#text", how would I get "apple" and eventually "color" => "Red"
      xmlDoc.documentElement.firstChild.nodeName;
      xmlDoc.documentElement.childNodes[0].nodeName;

    • Matt Warden
      ... Hash: SHA1 ... I m not sure why you are getting what you re getting, but essentially there are more nodes than you think there are. Node != Element. An
      Message 2 of 22 , Feb 17, 2006
        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Jim wrote:
        > I am pretty much a newbie to XML, atleast with dealing with the XML DOM.
        > I am having troubling getting the xml tag names and values. Given the
        > simple xml:
        >
        > <apples>
        > <apple>
        > <color>Red</color>
        > <size>Big</size>
        > </apple>
        > </apples>
        >
        > I get the xml response using YAHOO.util.Connect.asyncRequest
        > xmlDoc = o.responseXML;
        >
        > The following will give me "apples"
        > xmlDoc.documentElement.nodeName;
        >
        > But the following gives me "#text", how would I get "apple" and
        > eventually "color" => "Red"
        > xmlDoc.documentElement.firstChild.nodeName;
        > xmlDoc.documentElement.childNodes[0].nodeName;

        I'm not sure why you are getting what you're getting, but essentially
        there are more nodes than you think there are. Node != Element. An
        element is a type of node.

        My guess is that somehow you are accessing a text node (TEXT_NODE=3).
        What you are looking for are elements (ELEMENT_NODE=1).

        something like (just demonstration, as it only handles the case where
        there is 1 apple and 1 color node -- and really it won't even get the
        'color' node, I don't think, because childNodes is direct children only
        - -- nevertheless):

        var children = xmlDoc.documentElement.childNodes;
        var appleNode = false;
        var colorNode = 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 == 'apple') {
        appleNode = children[i];
        }
        else if (children[i].nodeName == 'color') {
        colorNode = children[i];
        }
        } // end if element
        } // end for


        Another option, depending on what you're doing exactly, is to do this:

        var apples = xmlDoc.getElementsByTagName('apple');
        var colors = xmlDoc.getElementsByTagName('color');

        Or, if you want to keep the association between apples and colors:

        var apples = xmlDoc.getElementsByTagName('apple');
        for (var i=0; i<apples.length; i++) {
        // this returns only colors which are descendents of this apple
        var colors = apples[i].getElementsByTagName('colors');
        // .. do something with this color/apple combo ...
        }

        - --
        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

        iD8DBQFD9gLarI3LObhzHRMRAhNdAJwKiCqtWey/D5HgJKpFbtW+AKCmaACgi6/v
        IdxCW1O/yIdV5GJkOWUHnFQ=
        =YxU5
        -----END PGP SIGNATURE-----
      • Eric Herrera
        ... Use nodeValue In your example above: xmlDoc.documentElement.firstChild.firstChild.firstChild.nodeValue
        Message 3 of 22 , Feb 17, 2006
          > But the following gives me "#text", how would I get "apple" and
          > eventually "color" => "Red"
          > xmlDoc.documentElement.firstChild.nodeName;
          > xmlDoc.documentElement.childNodes[0].nodeName;

          Use nodeValue

          In your example above:
          xmlDoc.documentElement.firstChild.firstChild.firstChild.nodeValue
        • Jim
          Thank you, the info you gave was very helpfull. My goal is to create an HTML table from XML which does not rely on the tag names to get their child elements.
          Message 4 of 22 , Feb 17, 2006
            Thank you, the info you gave was very helpfull.  My goal is to create an HTML table from XML which does not rely on the tag names to get their child elements.  I have come up with this function, but I am still unable to display the values of the elements, I am only getting "null" returned.

            function getDataTable(xmlDoc){
               
                gridTable = "<b>" + xmlDoc.documentElement.nodeName + "</b><br>";
                gridTable += "<table border='1'>";
               
                var rows = xmlDoc.documentElement.childNodes;
               
                for (var i = 1; i < 2; i++) {
                    gridTable += "<tr>";
                    var cols = rows[i].childNodes;
                    for(j = 0; j < cols.length; j++){
                        if (cols[j].nodeType == 1){
                            gridTable += "<th>" + cols[j].nodeName + "</th>";
                        }
                    }
                    gridTable += "</tr>";
                }
               
                for (var i = 0; i < rows.length; i++) {
                    gridTable += "<tr>";
                    var cols = rows[i].childNodes;
                    for(j = 0; j < cols.length; j++){
                        if (cols[j].nodeType == 1){
                            gridTable += "<td>";
                            gridTable += cols[j].nodeValue;
                            gridTable += "</td>";
                        }
                    }
                    gridTable += "</tr>";
                }
                gridTable += "</table>";
               
                return gridTable;
            }


            Again, thanks for you help.


            --- In ydn-javascript@yahoogroups.com, Matt Warden <mwarden@...> wrote:
            >
            > -----BEGIN PGP SIGNED MESSAGE-----
            > Hash: SHA1
            >
            > Jim wrote:
            > > I am pretty much a newbie to XML, atleast with dealing with the XML DOM.
            > > I am having troubling getting the xml tag names and values. Given the
            > > simple xml:
            > >
            > > <apples>
            > > <apple>
            > > <color>Red</color>
            > > <size>Big</size>
            > > </apple>
            > > </apples>
            > >
            > > I get the xml response using YAHOO.util.Connect.asyncRequest
            > > xmlDoc = o.responseXML;
            > >
            > > The following will give me "apples"
            > > xmlDoc.documentElement.nodeName;
            > >
            > > But the following gives me "#text", how would I get "apple" and
            > > eventually "color" => "Red"
            > > xmlDoc.documentElement.firstChild.nodeName;
            > > xmlDoc.documentElement.childNodes[0].nodeName;
            >
            > I'm not sure why you are getting what you're getting, but essentially
            > there are more nodes than you think there are. Node != Element. An
            > element is a type of node.
            >
            > My guess is that somehow you are accessing a text node (TEXT_NODE=3).
            > What you are looking for are elements (ELEMENT_NODE=1).
            >
            > something like (just demonstration, as it only handles the case where
            > there is 1 apple and 1 color node -- and really it won't even get the
            > 'color' node, I don't think, because childNodes is direct children only
            > - -- nevertheless):
            >
            > var children = xmlDoc.documentElement.childNodes;
            > var appleNode = false;
            > var colorNode = 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 == 'apple') {
            > appleNode = children[i];
            > }
            > else if (children[i].nodeName == 'color') {
            > colorNode = children[i];
            > }
            > } // end if element
            > } // end for
            >
            >
            > Another option, depending on what you're doing exactly, is to do this:
            >
            > var apples = xmlDoc.getElementsByTagName('apple');
            > var colors = xmlDoc.getElementsByTagName('color');
            >
            > Or, if you want to keep the association between apples and colors:
            >
            > var apples = xmlDoc.getElementsByTagName('apple');
            > for (var i=0; i<apples.length; i++) {
            > // this returns only colors which are descendents of this apple
            > var colors = apples[i].getElementsByTagName('colors');
            > // .. do something with this color/apple combo ...
            > }
            >
            > - --
            > 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
            >
            > iD8DBQFD9gLarI3LObhzHRMRAhNdAJwKiCqtWey/D5HgJKpFbtW+AKCmaACgi6/v
            > IdxCW1O/yIdV5GJkOWUHnFQ=
            > =YxU5
            > -----END PGP SIGNATURE-----
            >
          • piaffra_woods
            I have the same problem. I only get null returned. Is there any good javascript/dom/xml - Howto?
            Message 5 of 22 , Feb 28, 2006
              I have the same problem. I only get null returned. Is there any good
              javascript/dom/xml - Howto?




              --- In ydn-javascript@yahoogroups.com, "Jim" <jimhartford@...> wrote:
              >
              > Thank you, the info you gave was very helpfull. My goal is to create an
              > HTML table from XML which does not rely on the tag names to get their
              > child elements. I have come up with this function, but I am still
              > unable to display the values of the elements, I am only getting "null"
              > returned.
              >
              > function getDataTable(xmlDoc){
              >
              > gridTable = "<b>" + xmlDoc.documentElement.nodeName + "</b><br>";
              > gridTable += "<table border='1'>";
              >
              > var rows = xmlDoc.documentElement.childNodes;
              >
              > for (var i = 1; i < 2; i++) {
              > gridTable += "<tr>";
              > var cols = rows[i].childNodes;
              > for(j = 0; j < cols.length; j++){
              > if (cols[j].nodeType == 1){
              > gridTable += "<th>" + cols[j].nodeName + "</th>";
              > }
              > }
              > gridTable += "</tr>";
              > }
              >
              > for (var i = 0; i < rows.length; i++) {
              > gridTable += "<tr>";
              > var cols = rows[i].childNodes;
              > for(j = 0; j < cols.length; j++){
              > if (cols[j].nodeType == 1){
              > gridTable += "<td>";
              > gridTable += cols[j].nodeValue;
              > gridTable += "</td>";
              > }
              > }
              > gridTable += "</tr>";
              > }
              > gridTable += "</table>";
              >
              > return gridTable;
              > }
              >
              > Again, thanks for you help.
              >
              >
              > --- In ydn-javascript@yahoogroups.com, Matt Warden <mwarden@> wrote:
              > >
              > > -----BEGIN PGP SIGNED MESSAGE-----
              > > Hash: SHA1
              > >
              > > Jim wrote:
              > > > I am pretty much a newbie to XML, atleast with dealing with the XML
              > DOM.
              > > > I am having troubling getting the xml tag names and values. Given
              > the
              > > > simple xml:
              > > >
              > > > <apples>
              > > > <apple>
              > > > <color>Red</color>
              > > > <size>Big</size>
              > > > </apple>
              > > > </apples>
              > > >
              > > > I get the xml response using YAHOO.util.Connect.asyncRequest
              > > > xmlDoc = o.responseXML;
              > > >
              > > > The following will give me "apples"
              > > > xmlDoc.documentElement.nodeName;
              > > >
              > > > But the following gives me "#text", how would I get "apple" and
              > > > eventually "color" => "Red"
              > > > xmlDoc.documentElement.firstChild.nodeName;
              > > > xmlDoc.documentElement.childNodes[0].nodeName;
              > >
              > > I'm not sure why you are getting what you're getting, but essentially
              > > there are more nodes than you think there are. Node != Element. An
              > > element is a type of node.
              > >
              > > My guess is that somehow you are accessing a text node (TEXT_NODE=3).
              > > What you are looking for are elements (ELEMENT_NODE=1).
              > >
              > > something like (just demonstration, as it only handles the case where
              > > there is 1 apple and 1 color node -- and really it won't even get the
              > > 'color' node, I don't think, because childNodes is direct children
              > only
              > > - -- nevertheless):
              > >
              > > var children = xmlDoc.documentElement.childNodes;
              > > var appleNode = false;
              > > var colorNode = 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 == 'apple') {
              > > appleNode = children[i];
              > > }
              > > else if (children[i].nodeName == 'color') {
              > > colorNode = children[i];
              > > }
              > > } // end if element
              > > } // end for
              > >
              > >
              > > Another option, depending on what you're doing exactly, is to do this:
              > >
              > > var apples = xmlDoc.getElementsByTagName('apple');
              > > var colors = xmlDoc.getElementsByTagName('color');
              > >
              > > Or, if you want to keep the association between apples and colors:
              > >
              > > var apples = xmlDoc.getElementsByTagName('apple');
              > > for (var i=0; i<apples.length; i++) {
              > > // this returns only colors which are descendents of this apple
              > > var colors = apples[i].getElementsByTagName('colors');
              > > // .. do something with this color/apple combo ...
              > > }
              > >
              > > - --
              > > 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
              > >
              > > iD8DBQFD9gLarI3LObhzHRMRAhNdAJwKiCqtWey/D5HgJKpFbtW+AKCmaACgi6/v
              > > IdxCW1O/yIdV5GJkOWUHnFQ=
              > > =YxU5
              > > -----END PGP SIGNATURE-----
              > >
              >
            • Nige White
              ... For HTML nodes: http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/ecma-script-binding.html HTML nodes inherit from the DOM core:
              Message 6 of 22 , Feb 28, 2006
                piaffra_woods wrote:

                > I have the same problem. I only get null returned. Is there any good
                > javascript/dom/xml - Howto?


                For HTML nodes:

                http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/ecma-script-binding.html

                HTML nodes inherit from the DOM core:

                http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html

                So adding, removing nodes, traversing the tree, look at the core
                bindings. For manipulating HTML thingies, look at the HTML binding.

                Each of those pages is just part of a larger document that is well worth
                downloading as a ZIP, and keeping locally for reference!

                Nige

                _____________________________________________________________________
                This message has been checked for all known viruses. Virus scanning
                powered by Messagelabs http://www.messagelabs.com For more information
                e-mail : hostmaster@...
              • piaffra_woods
                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
                Message 7 of 22 , Feb 28, 2006
                  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.

                  --- In ydn-javascript@yahoogroups.com, Nige White <nigelw@...> wrote:
                  >
                  > piaffra_woods wrote:
                  >
                  > > I have the same problem. I only get null returned. Is there any good
                  > > javascript/dom/xml - Howto?
                  >
                  >
                  > For HTML nodes:
                  >
                  >
                  http://www.w3.org/TR/2003/REC-DOM-Level-2-HTML-20030109/ecma-script-binding.html
                  >
                  > HTML nodes inherit from the DOM core:
                  >
                  > http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
                  >
                  > So adding, removing nodes, traversing the tree, look at the core
                  > bindings. For manipulating HTML thingies, look at the HTML binding.
                  >
                  > Each of those pages is just part of a larger document that is well
                  worth
                  > downloading as a ZIP, and keeping locally for reference!
                  >
                  > Nige
                  >
                  > _____________________________________________________________________
                  > This message has been checked for all known viruses. Virus scanning
                  > powered by Messagelabs http://www.messagelabs.com For more information
                  > e-mail : hostmaster@...
                  >
                • Ken Robinson
                  ... Unfortunately, as I discovered, the xml-response from the Yahoo libraries has already been transformed to something else and none of the standard methods
                  Message 8 of 22 , Feb 28, 2006
                    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
                  • johns@worldwinner.com
                    hi ... yes!! unless it s a divide and conquer plan ... [extend specs with proprietary features then lock in those coders that use those features] -- js
                    Message 9 of 22 , Feb 28, 2006
                      hi

                      ( 06.02.28 09:59 -0500 ) Ken Robinson:
                      > 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.


                      yes!!

                      unless it's a divide and conquer plan ... [extend specs with proprietary
                      'features' then lock in those coders that use those features]

                      --
                      \js oblique strategy: are there sections? consider transitions
                    • Nige White
                      ... Didn t you see the second bit? http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html That s how to manipulate an XML DOM with Javascript. get the
                      Message 10 of 22 , Feb 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.

                        Didn't you see the second bit?

                        http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html

                        That's how to manipulate an XML DOM with Javascript.

                        "get the value from"? Do you mean the text inside a text node?

                        A text node is a CharacterData object (see above document), and it has a
                        "data" property.

                        Nige

                        _____________________________________________________________________
                        This message has been checked for all known viruses. Virus scanning
                        powered by Messagelabs http://www.messagelabs.com For more information
                        e-mail : hostmaster@...
                      • Thomas S. Sha
                        ... Actually, the connection manager does not transform or modify the responseXML values from the native object. If you find the responseXML property is null,
                        Message 11 of 22 , Feb 28, 2006
                          --- 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
                        • piaffra_woods
                          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:
                          Message 12 of 22 , Feb 28, 2006
                            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>




                            I wrote in addition a html page, which wants to read out the titel
                            (which is in english, the title),please excuse the offensive language
                            in the code, its an expression of my ongoing frustration.


                            <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);
                            }



                            var responseFailure = function(o){alert('Failure!');}
                            var callback =
                            {
                            success:responseSuccess,
                            failure:responseFailure
                            }



                            any ideas why i get: Titel: undefined? (i tried, .value, .nodeValue,
                            .text usw.)

                            thx.

                            --- 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
                            >
                          • Rob Turnbull
                            Miriam Daill Shouldn t that be: Miriam Daill Cheers, Rob
                            Message 13 of 22 , Feb 28, 2006
                              <a target="show_autor?id=112">Miriam Daill</a>
                               
                              Shouldn't that be:
                              <a href="show_autor?id=112">Miriam Daill</a>
                               
                              Cheers,
                              Rob
                               
                            • 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 14 of 22 , Feb 28, 2006
                                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 15 of 22 , Feb 28, 2006
                                  --- 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 16 of 22 , Feb 28, 2006
                                    -----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 17 of 22 , Feb 28, 2006
                                      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 18 of 22 , Feb 28, 2006
                                        --- 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 19 of 22 , Feb 28, 2006
                                          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 20 of 22 , Feb 28, 2006
                                            -----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 21 of 22 , Mar 1, 2006
                                              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 22 of 22 , Mar 1, 2006
                                                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.