Results 1 to 2 of 2

Thread: Read the XML file using javascript

  1. #1
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Read the XML file using javascript

    Hi,

    i am new to scripting and i would like you ask a help here. i want a javascript to read the following xml and the end result should display

    Name = name1
    int = 1
    ct = 3
    Act = 2
    Ex = 1
    Sum = 1
    Ext = 0

    the xml content is

    <lesson id="52" name = "name1" >
    <page id="01" name="int" count="1" />
    <page id="02" name="ct" count="3" />
    <page id="03" name="Act" count="2"/>
    <page id="04" name="Ex" count="1" />
    <page id="05" name="Sum" count="1" />
    <page id="06" name="Ext" count="0" />
    </lesson>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    For this demo, I'm calling the xml file 'lesson.xml'.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Read XML - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	var $r = $('#result');
    	$.ajax({
    		url: 'lesson.xml',
    		dataType: 'xml',
    		cache: false,
    		success: function(data){
    			var r = [], c;
    			$('*', data).each(function(i, t){
    				if((c = t.getAttribute('count'))){
    					r.push(t.getAttribute('name') + ' = ' + c);
    				} else {
    					r.push('Name = ' + t.getAttribute('name'));
    				}
    			});
    			$r.append(r.join('<br>'));
    		}
    	});
    });
    </script>
    </head>
    <body>
    <div id="result"></div>
    </body>
    </html>
    Last edited by jscheuer1; 05-24-2014 at 03:43 PM. Reason: improve backward compatibility
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2010, 01:31 PM
  2. read xml file
    By lay in forum PHP
    Replies: 4
    Last Post: 03-12-2009, 12:13 PM
  3. how to read pdf file using php?
    By khoulong in forum PHP
    Replies: 1
    Last Post: 03-12-2009, 03:33 AM
  4. Resolved how to read data from xml file to javascript array?
    By onoprise in forum JavaScript
    Replies: 3
    Last Post: 12-06-2008, 09:37 PM
  5. asp read file
    By midhul in forum ASP
    Replies: 1
    Last Post: 02-23-2008, 06:37 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •