Anyone know how to go through an XML file using Jquery and print the tag names and text values from only the tags that have a text value in them?
Example, html that outputs like:
id: 00130000004EQCpAAO
updated: 2011-08-16 19:40:51
account_type: Vendor Other
active: 1
someprop_d: 0
that comes from XML like:
<accounts>
<id>00130000004EQCpAAO</id>
<updated>2011-08-16 19:40:51</updated>
<account_type>Vendor Other</account_type>
<someprop_a/>
<active_c>1</active_c>
<someprob_b/>
<someprop_c/>
<someprop_d>0</someprop_d>
</accounts>
The Jquery:
var mystr = $.ajax({
type: 'GET',
url: '/js/data_account.xml',
async: false,
data: {
key: "value"
},
dataType: "xml",
success: function(data) {
var xml = $(data)
//var $accountid = xml.find("id").text();
// do something relevant here
//return something;
},
error: function() {
alert("error: call failed");
}
});
$('parentElement').append(mystr);
You can look at the value of the
.nodeNameproperty to get the tag name and use.text()to get the text content. Eg:You can iterate the xml and get the output you want:
Working demo: http://jsfiddle.net/gilly3/KYWCK/