locked
How to parse XML file(from web api) in metrostyle application with javascript

    Question

  • Here is my code

    function buttonClickHandler(eventInfo) {
    
              
            WinJS.xhr({
                url: 'http://api.xxx/xxx/get/id/15000',
                user: 'xxxx',
                password:'xxxxx'
    
            }).then(function (xhr) {
    
                var obtainedData = xhr.responseText;
    
            //Code to Parse obtainedData ????????
                
    
                document.getElementById('xmlout').innerText =obtainedData ;
                  
            });
    
    
    
           
        }

    In above code the variable 'obtainedData'  contain the xml string . How to parse the 'obtainedData' ? 

    Tuesday, July 17, 2012 9:40 AM

Answers

  • var xml = xhr.responseXML;

    xml will be a Document object.  You can use the normal DOM methods to extract the data.

    It easier, however, to return JSON if you have control over what the API returns.  If you do that you only need:

    var obj = JSON.parse(xhr.responseText);

    And obj is a normal JavaScript object.
    Tuesday, July 17, 2012 2:35 PM

All replies

  • var xml = xhr.responseXML;

    xml will be a Document object.  You can use the normal DOM methods to extract the data.

    It easier, however, to return JSON if you have control over what the API returns.  If you do that you only need:

    var obj = JSON.parse(xhr.responseText);

    And obj is a normal JavaScript object.
    Tuesday, July 17, 2012 2:35 PM
  • Hai,

    Here is my xml data

    <event id="15000" slug="2008/jul/auckland-central/the-sky-tonight" url="http://www.eventfinder.co.nz/2008/jul/auckland-central/the-sky-tonight">
    <category_id>197</category_id>
    <category id="197" slug="museums">
    <name>
    <![CDATA[ Museums ]]>
    </name>
    <parent id="11" slug="exhibitions">
    <name>
    <![CDATA[ Exhibitions ]]>
    </name>
    <parent id="246" slug="whatson">
    <name>
    <![CDATA[ All Events ]]>
    </name>
    </parent>
    </parent>
    </category>
    <location id="187" slug="auckland-city-royal-oak">
    <name>
    <![CDATA[ Royal Oak ]]>
    </name>
    <parent id="28" slug="auckland-central">
    <name>
    <![CDATA[ Auckland Central ]]>
    </name>
    <parent id="2" slug="auckland">
    <name>
    <![CDATA[ Auckland ]]>
    </name>
    <parent id="574" slug="new-zealand">
    <name>
    <![CDATA[ New Zealand ]]>
    </name>
    </parent>
    </parent>
    </parent>
    <venue_name>
    <![CDATA[ Stardome Observatory ]]>
    </venue_name>
    <venue_address>
    <![CDATA[ One Tree Hill Domain ]]>
    </venue_address>
    </location>
    <name>
    <![CDATA[ The Sky Tonight ]]>
    </name>
    <image_url>
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-1.jpg
    </image_url>
    <dates>
    <date>
    <location_id>187</location_id>
    <venue_name>
    <![CDATA[ Stardome Observatory ]]>
    </venue_name>
    <venue_address>
    <![CDATA[ One Tree Hill Domain ]]>
    </venue_address>
    <start_date>2008-07-02</start_date>
    <end_date>2008-07-31</end_date>
    <start_time>20:00</start_time>
    <start_timestamp>1214985600</start_timestamp>
    <end_time>21:30</end_time>
    </date>
    </dates>
    <featured>0</featured>
    <different>0</different>
    <free>0</free>
    <tickets>
    <pricing>
    <cost value="16.00">
    <![CDATA[ Adult ]]>
    </cost>
    <cost value="8.00">
    <![CDATA[ Child ]]>
    </cost>
    <cost value="14.00">
    <![CDATA[ Concession ]]>
    </cost>
    <cost value="45.00">
    <![CDATA[ Family Pass ]]>
    </cost>
    </pricing>
    <on_sale_date_time>2008-07-02 09:15:07</on_sale_date_time>
    </tickets>
    <description>
    <![CDATA[
    Join us at 8.00pm to learn about which planets, stars and constellations are visible in the night sky in July.
     
     Additional screenings will also be available on Friday and Saturday nights at 10.00pm.
     
     Showing ...
    ]]>
    </description>
    <images>
    <image id="17376" is_primary="1">
    <image_transformation transform_type_id="1" filename="17376-15000-1.jpg" mime_type="image/jpeg" width="425" height="186" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-1.jpg
    </image_transformation>
    <image_transformation transform_type_id="2" filename="17376-15000-2.jpg" mime_type="image/jpeg" width="80" height="35" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-2.jpg
    </image_transformation>
    <image_transformation transform_type_id="3" filename="17376-15000-3.jpg" mime_type="image/jpeg" width="100" height="44" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-3.jpg
    </image_transformation>
    <image_transformation transform_type_id="4" filename="17376-15000-4.jpg" mime_type="image/jpeg" width="183" height="80" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-4.jpg
    </image_transformation>
    <image_transformation transform_type_id="5" filename="17376-15000-5.jpg" mime_type="image/jpeg" width="60" height="26" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-5.jpg
    </image_transformation>
    <image_transformation transform_type_id="6" filename="17376-15000-6.jpg" mime_type="image/jpeg" width="50" height="50" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-6.jpg
    </image_transformation>
    <image_transformation transform_type_id="8" filename="17376-15000-8.jpg" mime_type="image/jpeg" width="190" height="127" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-8.jpg
    </image_transformation>
    <image_transformation transform_type_id="9" filename="17376-15000-9.jpg" mime_type="image/jpeg" width="190" height="83" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-9.jpg
    </image_transformation>
    <image_transformation transform_type_id="10" filename="17376-15000-10.jpg" mime_type="image/jpeg" width="190" height="85" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-10.jpg
    </image_transformation>
    <image_transformation transform_type_id="13" filename="17376-15000-13.jpg" mime_type="image/jpeg" width="60" height="60" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-13.jpg
    </image_transformation>
    <image_transformation transform_type_id="14" filename="17376-15000-14.jpg" mime_type="image/jpeg" width="425" height="186" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-14.jpg
    </image_transformation>
    <image_transformation transform_type_id="15" filename="17376-15000-15.jpg" mime_type="image/jpeg" width="75" height="75" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-15.jpg
    </image_transformation>
    <image_transformation transform_type_id="16" filename="17376-15000-16.jpg" mime_type="image/jpeg" width="300" height="80" version="1">
    http://s1.eventfinder.co.nz/uploads/events/transformed/17376-15000-16.jpg
    </image_transformation>
    </image>
    </images>
    </event>


    I tried this  code  to retrieve values in the name tag

    var restXML = xhr.responseXML;  
                var count = 0;
                var category = restXML.getElementsByTagName("name");
                for (var i = 0; i < category.length ; i++) {
                    count++;
    
                    var _maincategory = restXML.getElementsByTagName("name")[i].childNodes[0].nodeValue;
    
                }

    In  variable category i get all the value that under name tag, Actually i want to retrieve Category and Location separately, How do i do?

    Please help me...

    Thanks

    Tuesday, July 31, 2012 1:15 PM