Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Ajax js xml

Hey guys, it is nice to be a part of the treehouse family and i really appreciate it.

Ive done some tracks and now i need help concerning data extraction from xml.

here are my codes

js------

var XMLHttpRequestObject = false; if (window.XMLHttpRequest) { XMLHttp = new XMLHttpRequest(); }

    function getData (datasource, divID) {

    if(XMLHttp) 
    var obj = document.getElementById(divID);
    XMLHttp.open ("GET", datasource);

    XMLHttp.onreadystatechange = function()
    {
        if (XMLHttp.readyState == 4 && XMLHttp.status == 200)
        {
         obj.innerHTML = XMLHttp.responseXML;   
            }
        }

        XMLHttp.send (null);
        }

        // 

xml-----

<?xml version="1.0" encoding="UTF-8"?> <musik> <agmkoeln> <erzeuger> <le> <sparte> <menuemusic> <mstyleone> <available> <value> 3 </value> </available> </mstyleone> <mstyletwo> <available> <value> 4 </value> </available> </mstyletwo> <mstylethree> <available> <value> 0 </value> </available> </mstylethree> <mstylefour> <available> <value> 0 </value> </available> </mstylefour> <mstylefive> <available> <value> 1 </value> </available> </mstylefive> </menuemusic> <ingamemusic> <jumpnrun> <available> <value> 2 </value> </available> </jumpnrun> <action> <available> <value> 3 </value> </available> </action> <activity> <available> <value> 2 </value> </available> </activity> <slowbow> <available> <value> 4 </value> </available> </slowbow> <scarytunes> <available> <value> 5 </value> </available> </scarytunes> <comcom> <available> <value> 3 </value> </available> </comcom> </ingamemusic> </sparte> </le> </erzeuger> <erzeuger> <ro> <sparte> <menuemusic> <mstyleone> <available> <value> 3 </value> </available> </mstyleone> <mstyletwo> <available> <value> 4 </value> </available> </mstyletwo> <mstylethree> <available> <value> 3 </value> </available> </mstylethree> <mstylefour> <available> <value> 2 </value> </available> </mstylefour> <mstylefive> <available> <value> 7 </value> </available> </mstylefive>

                </menuemusic>
                <ingamemusic>
                        <jumpnrun>
                            <available>
                            <value>
                                23
                            </value>
                            </available>
                        </jumpnrun>
                        <action>
                             <available>
                            <value>
                                14
                            </value>
                            </available>
                        </action>
                        <activity>
                             <available>
                            <value>
                            </value>
                            24
                            </available>
                        </activity>
                        <slowbow>
                             <available>
                            <value>
                                5
                            </value>
                            </available>
                        </slowbow>
                        <scarytunes>
                             <available>
                            <value>
                                4
                            </value>
                            </available>
                        </scarytunes>
                        <comcom>
                             <available>
                            <value>
                                4
                            </value>
                            </available>
                        </comcom>
                </ingamemusic>
            </sparte>
        </ro>
         </erzeuger>
          <erzeuger>
        <ti>
             <sparte>
                <menuemusic>
                    <mstyleone>
                        <available>
                            <value>
                                5
                            </value>
                        </available>
                    </mstyleone>
                     <mstyletwo>
                         <available>
                            <value>
                                21
                            </value>
                        </available>
                    </mstyletwo>
                     <mstylethree>
                         <available>
                            <value>
                                6
                            </value>
                        </available>
                    </mstylethree>
                     <mstylefour>
                         <available>
                            <value>
                                24
                            </value>
                        </available>
                    </mstylefour>
                     <mstylefive>
                         <available>
                            <value>
                                5
                            </value>
                        </available>
                    </mstylefive>

                </menuemusic>
                <ingamemusic>
                        <jumpnrun>
                            <available>
                            <value>
                                8
                            </value>
                            </available>
                        </jumpnrun>
                        <action>
                             <available>
                            <value>
                                31
                            </value>
                            </available>
                        </action>
                        <activity>
                             <available>
                            <value>
                                4
                            </value>
                            </available>
                        </activity>
                        <slowbow>
                             <available>
                            <value>
                                12
                            </value>
                            </available>
                        </slowbow>
                        <scarytunes>
                             <available>
                            <value>
                                42
                            </value>
                            </available>
                        </scarytunes>
                        <comcom>
                             <available>
                            <value>
                                6
                            </value>
                            </available>
                        </comcom>
                </ingamemusic>
            </sparte>
        </ti>
        </erzeuger>
    </agmkoeln>

</musik>

html-----

 </ul>
  </nav>
</header>
<form>
  <input type="button" value="Anzeigen" onclick="getData('tracks.xml','HierEintragen');"/>
</form>
<div id="HierEintragen">
    <p>Hier erscheinen nach dem Klick die kaufbaren Artikel und deren Menge.</p>
</div>
  </section>

however, i would like to tell the code, that it has to display all the available music songs from three different producers (in xml -> <erzeuger> ) like this:

le= ingamemusic=3, menuemusic=4 .... ro=ingamemusic=2, menuemusic=7.... ti=ingamemusic=5, menuemusic=9 ....

I make music and would like to show a guest on my site, how much of each category is available at the moment.

thanks a lot and greetings from germany, Roman

1 Answer

Have you tried using jQuery.parseXml?