jQuery XML Parsing

gdpotter
gdpotter used Ask the Experts™
on
Hi all, I'm working on a project right now in JavaScript (and jQuery) and I just can't seem to find a solid online example or documentation for what I am trying to do.  I am trying to connect to an XML file and load it into an object literal array.  For example, if the file is:
<example>
 <row>
  <Column1>Data1</Column1>
  <Column2>Data2</Column2>
  <Column4>Data4</Column4>
  <Column5>Data5</Column5>
 </row>
 <row>
  <Column1>Data1</Column1>
  <Column2>Data2</Column2>
  <Column3>Data3</Column3>
  <Column5>Data5</Column5>
 </row1>
 </example>

Open in new window


I need that to become:
[
            {
                column1:'Data1',
                column2:'Data2',
                column4:'Data4',
                column5:'Data5'
            },
            {
                column1:'Data1',
                column2:'Data2',
                column3:'Data3',
                column5:'Data5'
            }
]

Open in new window


I can already load in the xml file with $.get but I just need some help parsing and dumping that data into an object literal array.  Because this data ends up in a table, there aren't going to be any additional sub-nodes to worry about, so the structure will be just as I specified above.  However, we also have no idea at run time what the actual tags are going to be called.  It might not be called "example" or "row".

I'll make my issue worth 500 points so if you can assist with writing something in addition to just pointing me at a website, that would be great.  I'm in a bit of a time crunch right now.

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:

Author

Commented:
I was on that page earlier, but I don't like the way that it formats the output.  It goes curly-brace, curly-brace, bracket, curly-brace, but I need to be bracket, curly-brace.  This same project reads data from other sources (CSV, JSON, etc.) so I can't change my format now.  I'm adding XML support after the fact.
leakim971Multitechnician
Top Expert 2014

Commented:
Use it like this : var json = xml2json.parser(xmlStr,'example').row;

"json" contain the object lie you want it.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

leakim971Multitechnician
Top Expert 2014

Commented:
Test page :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="xml2json.js"></script>
<script language="javascript">
    window.onload = function() {
        if(window.XMLHttpRequest) xmlhttp = new XMLHttpRequest(); else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        if(xmlhttp) {
            xmlhttp.open("GET", "example.xml", false);
            xmlhttp.send();
			xmlStr = (new XMLSerializer()).serializeToString(xmlhttp.responseXML);
			var json = xml2json.parser(xmlStr,'example').row;
			
			alert(json[0].column4); // alert Data4
			alert(json[1].column3); // alert Data4
			
        }
        else {
            alert("your browser don't support Ajax!\nBye!");
        }
    }
</script>
</head>
<body>
</body>
</html>

Open in new window

Author

Commented:
when I try to add this script to my JS file, the internet explorer script console thing tells me "Access Denied" for line the line which is #9 in your example.  So I tried to use jQuery's $.get() and was able to load the data in manually.  I was successful to load the data but the code still doesn't work.  I got the data loaded into a variable, so that's not my problem.  My problem is to parse the string which is the XML file.
Multitechnician
Top Expert 2014
Commented:
you've an error in your xml file, line 13 : < /row1>/b> > instead < /row >

if you hav jquery inside :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="xml2json.js"></script>
<script language="javascript">
	$(document).ready(function() {
		$.ajax({
			"url" : "example.xml",
			"dataType": "text",
			"success" : function(xmlText) {
				var json = xml2json.parser(xmlText, 'example').row;
				alert(json[0].column4); // alert Data4
				alert(json[1].column3); // alert Data3				
			}
		});
			
	});
</script>
</head>
<body>
</body>
</html>

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
Thanks for the points!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial