WEB Turning an XML file into an array in JavaScript?

Discussion in 'OT Technology' started by Insert Tokens, Sep 18, 2009.

  1. Insert Tokens

    Insert Tokens Making Cancer My Bitch OT Supporter

    Joined:
    Jan 12, 2006
    Messages:
    8,297
    Likes Received:
    61
    Location:
    Tasmania
    Googled around, but most of the methods seem overly complicated, using libraries etc.

    There's not an easy way to turn a basic XML file into an array in JS?

    XML file looks something like this:

    Code:
    <goods>
        <unit id="4325" loc1="2hrs" loc2="4hrs" loc3="12hrs" desc="Container 4325" />
        <unit id="4326" loc1="2.5hrs" loc2="5hrs" loc3="12hrs" desc="Container 4326" />
    </goods>
    
     
  2. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    Wirelessly posted via wap.offtopic.com (BB 8830: BlackBerry8830/4.5.0.135 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/104)

    Do you just need the xml parser for like a textbox, or are you trying to read a local/remote file?
     
  3. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    if you use jQuery, its really easy:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script src="http://www.google.com/jsapi"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
    	function parseXml(xml)
    	{
    		
    		$(xml).find("unit").each(function()
    		{
    		  var xmlUnit = $(this);
    		  var retStr = '';
    		  
    		  retStr = retStr+ "id: "+xmlUnit.attr("id")+"\n";
    		  retStr = retStr+ "loc1: "+xmlUnit.attr("loc1")+"\n";
    		  retStr = retStr+ "loc2: "+xmlUnit.attr("loc2")+"\n";
    		  retStr = retStr+ "loc3: "+xmlUnit.attr("loc3")+"\n";
    		  retStr = retStr+ "desc: "+xmlUnit.attr("desc")+"\n";
    		  
    		  alert(retStr);
    		});
    	}
    	
    	$("#btnParse").click(function() {
    		parseXml($("#theXml").val());
    	});
    });
    </script>
    
    <style type="text/css">
    #content { width: 600px; margin: 0 auto; text-align: center; }
    #theXml { width: 550px; height: 550px; }
    </style>
    </head>
    
    <body>
         <div id="content">
              <form id="xmlForm" name="xmlForm" action="">
              
                   <p><textarea id="theXml" name="theXml"></textarea></p>
    
                   <p><input type="button" name="btnParse" id="btnParse" value=" Parse XML " /></p>
              
              </form>
         </div>
    </body>
    </html>
    
     
  4. sandor3kgt

    sandor3kgt New Member

    Joined:
    Nov 1, 2006
    Messages:
    646
    Likes Received:
    0
    "overly complicated" and "using libraries" don't really go together. Libraries exist to solve complicated, common problems...like this one! Try the library noon suggested, it's a popular one.
     
  5. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    Easiest way might to convert the XML into a JSON object

    JSON is a javascript Array
     

Share This Page