locked
Importing Data From xml RRS feed

  • Question

  • User1324715958 posted

    Having trouble importing data from xml. Chrome wont let me access the xml locally so I have to use firefox to debug and console is not returning anything. 

    code:

    $.ajax({
    	url: 'xml/timeline.xml',
    	dataType: 'xml',
    	success: function(data) 
    	{
    		alert("found");
    		$("ul").children().remove();
    		alert("remove children");
    		$(data).find('statues status').each(function() 
    		{
    			alert("step 1");			
    			var status = $(this).find('text').text();
    			$('.timeline ul').append
    			(
    				$('<li />', 
    				{
    					text: status
    				})
    			);
    		});
    	},
    	error: function() {
    		$('.timeline').text('Failed to load xml file');
    	}
    });

    Alert indicates that everything works until it hits step 1. It never displays step 1 alert. 

    html index:

    <!doctype html>
    <html lang="en">
    <meta charset="utf-8"/>
    <body> 
    	<div class="timeline">
    		<ul></ul>
    	</div>
    	
    	<link rel="stylesheet" href="js/jquery-ui.css">
      <script src="js/jquery-1.12.4.js"></script>
      <script src="js/jquery-ui.js"></script>
      <script src="js/primary.js"></script>
    </body>
    </html>

    Thursday, July 26, 2018 5:59 PM

Answers

  • User-369506445 posted

    Based your xml will be

    $xml.find('status').each(function () {
                $("#timeline").append($(this).text() + "<br />");
            });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:01 PM
  • User-369506445 posted

    Change your success function to

    success: function(xml) {
                    $(xml).find('status').each(function(){
                        $(this).find("id").each(function(){
                            var name = $(this).text();
                            alert(name);
                        });
                    });
                }

    Your complete code

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "xml/timeline.xml",
                cache: false,
                dataType: "xml",
                success: function(xml) {
                     $(xml).find('status').each(function(){
                        $(this).find("id").each(function(){
                            var name = $(this).text();
                            alert(name);
                        });
                    });
                }
            });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:07 PM
  • User1324715958 posted

    Clearly I'm not getting it. I made some adjustments and have been playing with this sample but nothing appears:

    $.ajax({
    	type: "GET",
    	url: "xml/timeline.xml",
    	cache: false,
    	dataType: "xml",
    	success: function(xml) {
    		 $(xml).find('status').each(function(){
    			 $("#timeline").append("<p>Test 1</p>");
    			$(this).find("text").each(function(){
    				var name = $(this).text();
    				 $("#timeline").append("<p>Test 2</p>");
    			});
    		});
    	}
    });

    No errors no nothing. What did I do wrong? 

    EDIT

    Ok I found my mistake. I needed to update my index.html.

    <!doctype html>
    <html lang="en">
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="js/jquery-ui.css">
    <script src="js/jquery-1.12.4.js"></script>
    <script src="js/jquery-ui.js"></script>
    <body> 
    	<div class="timeline">
    		<ul id="mylist">
    		</ul>  
    	</div>	
    <script src="js/primary.js"></script>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:51 PM

All replies

  • User-369506445 posted

    Hi

    Here is a sample that shows how to load a xml file 

    https://www.w3schools.com/xml/tryit.asp?filename=tryajax_xml2

    And 

    Try this:

    // Load the xml file using ajax 
    $.ajax({
        type: "GET",
        url: "something.xml",
        dataType: "xml",
        success: function (xml) {
    
            // Parse the xml file and get data
            var xmlDoc = $.parseXML(xml),
                $xml = $(xmlDoc);
            $xml.find('category[name="My t"] logo').each(function () {
                $("#news-container").append($(this).text() + "<br />");
            });
        }
    });
    Thursday, July 26, 2018 6:27 PM
  • User1324715958 posted

    Maybe I'm doing something wrong here but no errors and nothing displayed:

    // Load the xml file using ajax 
    $.ajax({
        type: "GET",
        url: "xml/timeline.xml",
        dataType: "xml",
        success: function (xml) {
    
            // Parse the xml file and get data
            var xmlDoc = $.parseXML(xml),
                $xml = $(xmlDoc);
            $xml.find('status').each(function () {
                $("#timeline").append($(this).text() + "<br />");
            });
        }
    });

    based on your sample

    Im not sure what these lines are ref in the xml?

    $xml.find('category[name="My t"] logo').each(function () {
                $("#news-container").append($(this).text() + "<br />");

    xml doc:

    <?xml version="1.0" encoding="UTF-8"?>
    <statuses type="array">
    <status>
    <created_at>Tue Jul 10 01:42:10 +0000 2012</created_at>
    <id>222505965281488898</id>
    <text>We will be performing #D3 maintenance Tuesday, July 10 beginning at 5am PDT: http://t.co/2jr91xDu</text>
    <source>&lt;a href=&quot;http://www.radian6.com&quot; rel=&quot;nofollow&quot;&gt;Radian6 &lt;/a&gt;</source>
    <truncated>false</truncated>
    <in_reply_to_status_id></in_reply_to_status_id>
    <in_reply_to_user_id></in_reply_to_user_id>
    <in_reply_to_screen_name></in_reply_to_screen_name>
    <possibly_sensitive>false</possibly_sensitive>
    <user>
    <id>174307074</id>
    <name>BlizzardCS</name>
    <screen_name>BlizzardCS</screen_name>
    <location>Irvine CA and Austin TX</location>
    <description>Blizzard Entertainment North America Customer Support</description>
    <profile_image_url>http://a0.twimg.com/profile_images/1094734715/BlizzLogo_normal.jpg</profile_image_url>
    <profile_image_url_https>https://si0.twimg.com/profile_images/1094734715/BlizzLogo_normal.jpg</profile_image_url_https>
    <url>http://blizzard.com/support/</url>
    <protected>false</protected>
    <followers_count>220560</followers_count>
    <profile_background_color>C0DEED</profile_background_color>
    <profile_text_color>333333</profile_text_color>
    <profile_link_color>0084B4</profile_link_color>
    <profile_sidebar_fill_color>DDEEF6</profile_sidebar_fill_color>
    <profile_sidebar_border_color>C0DEED</profile_sidebar_border_color>
    <friends_count>32</friends_count>
    <created_at>Tue Aug 03 16:26:52 +0000 2010</created_at>
    <favourites_count>0</favourites_count>
    <utc_offset>-28800</utc_offset>
    <time_zone>Pacific Time (US &amp; Canada)</time_zone>
    <profile_background_image_url>http://a0.twimg.com/profile_background_images/168329781/Twitter-BG-cs.jpg</profile_background_image_url>
    <profile_background_image_url_https>https://si0.twimg.com/profile_background_images/168329781/Twitter-BG-cs.jpg</profile_background_image_url_https>
    <profile_background_tile>false</profile_background_tile>
    <profile_use_background_image>true</profile_use_background_image>
    <geo_enabled>true</geo_enabled>
    <verified>false</verified>
    <statuses_count>12121</statuses_count>
    <lang>en</lang>
    <contributors_enabled>false</contributors_enabled>
    <is_translator>false</is_translator>
    <listed_count>2008</listed_count>
    <show_all_inline_media>false</show_all_inline_media>
    <default_profile>false</default_profile>
    <default_profile_image>false</default_profile_image>
    <following>true</following>
    <follow_request_sent></follow_request_sent>
    <notifications></notifications>
    </user>
    <geo/>
    <coordinates/>
    <place/>
    <contributors/>
    <retweet_count>3</retweet_count>
    <favorited>false</favorited>
    <retweeted>false</retweeted>
    </status>
    </statuses>
    Thursday, July 26, 2018 6:39 PM
  • User-369506445 posted

    Based your xml will be

    $xml.find('status').each(function () {
                $("#timeline").append($(this).text() + "<br />");
            });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:01 PM
  • User-369506445 posted

    Change your success function to

    success: function(xml) {
                    $(xml).find('status').each(function(){
                        $(this).find("id").each(function(){
                            var name = $(this).text();
                            alert(name);
                        });
                    });
                }

    Your complete code

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "xml/timeline.xml",
                cache: false,
                dataType: "xml",
                success: function(xml) {
                     $(xml).find('status').each(function(){
                        $(this).find("id").each(function(){
                            var name = $(this).text();
                            alert(name);
                        });
                    });
                }
            });
        });
    </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:07 PM
  • User1324715958 posted

    Clearly I'm not getting it. I made some adjustments and have been playing with this sample but nothing appears:

    $.ajax({
    	type: "GET",
    	url: "xml/timeline.xml",
    	cache: false,
    	dataType: "xml",
    	success: function(xml) {
    		 $(xml).find('status').each(function(){
    			 $("#timeline").append("<p>Test 1</p>");
    			$(this).find("text").each(function(){
    				var name = $(this).text();
    				 $("#timeline").append("<p>Test 2</p>");
    			});
    		});
    	}
    });

    No errors no nothing. What did I do wrong? 

    EDIT

    Ok I found my mistake. I needed to update my index.html.

    <!doctype html>
    <html lang="en">
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="js/jquery-ui.css">
    <script src="js/jquery-1.12.4.js"></script>
    <script src="js/jquery-ui.js"></script>
    <body> 
    	<div class="timeline">
    		<ul id="mylist">
    		</ul>  
    	</div>	
    <script src="js/primary.js"></script>
    </body>
    </html>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 7:51 PM