locked
Read XML from a web service and show the content(WinJS/Javascript) RRS feed

  • Question

  • Hello,

    Can someone pleas tell me how can i read an xml document from a remote web service and display its content in HTMl.

    What I have done so far: I am able to call the web service and get the XML file. But I am unable(or simply dont know) to show the xml contents in the HTML. For example I want to retrieve the location from the xml file and show it in a div.

    My code

     function buttonClickHandler(eventInfo) {
    
    
    
            var userName = document.getElementById("nameInput").value;
            var greetingString = "Hello, " + userName + "!";
            document.getElementById("greetingOutput").innerText = greetingString;
    
            WinJS.xhr({
                url: 'http://api.eventfinder.co.nz/events/get/id/15000',
                user: 'xxxxxxxx',
                password: 'xxxxxxx'
    
            }).then(function (xhr) {
    
                var text = xhr.responseText;
                var subText = text.search('location');
                document.getElementById("output").innerText = subText;
    
                
            });
    
        }
       

    My XML

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

    Tuesday, July 17, 2012 9:51 AM

Answers

  • Hi

    You can use the XmlDocument

    var xmlDocument = new Windows.Data.Xml.Dom.XmlDocument();
      xmlDocument.loadXml(data);

    Then use code like this to get what you want.

     var title = Nodelist[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;

    Update:

    This sample will show you how to load the XML file, and How to get the child node value:

    Windows Runtime XML data API sample

    Hope it helpful

    • Edited by Dino He Friday, July 20, 2012 7:47 AM
    • Marked as answer by Dino He Monday, July 23, 2012 10:44 AM
    Wednesday, July 18, 2012 1:46 PM

All replies

  • Can you narrow this down?  Is that no text shows?  What does the debugger show as you step thorough this?  What if you don't do .search and display 'text' do you see anything?  What troubleshooting and debugging have you done so far?

    Jeff Sanders (MSFT)

    Tuesday, July 17, 2012 7:38 PM
    Moderator
  • Hi

    You can use the XmlDocument

    var xmlDocument = new Windows.Data.Xml.Dom.XmlDocument();
      xmlDocument.loadXml(data);

    Then use code like this to get what you want.

     var title = Nodelist[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;

    Update:

    This sample will show you how to load the XML file, and How to get the child node value:

    Windows Runtime XML data API sample

    Hope it helpful

    • Edited by Dino He Friday, July 20, 2012 7:47 AM
    • Marked as answer by Dino He Monday, July 23, 2012 10:44 AM
    Wednesday, July 18, 2012 1:46 PM