Hi there,
I'm trying to parse a larger XML file with jQuery.
Unfortunately it takes a little bit longer (cca 1.5sec). Are there better ways too speed-up the parsing part? I really need some advice. THX
- the XML is dynamically created (REST web service on the app. server impemented)
- I have a basic ajax (GET) request and on success it calls the ParseXML function.
- Example structure of the XML is under the parseXML function (note: there are about 1500 <contact> nodes
- function parseXML(xml)
{
- var jxd = $(xml);
if(!jxd) return "";
- jxd.find('contact').each(function() {
- var $node = $(this); // is this good practice?
var type = $node.find('T_TYPE').text();
var Fname = $node.find('T_FIRSTNAME').text()+' ';
var Lname = $node.find('T_LASTNAME').text()+'</a>';
var name = Fname+Lname;
var idk = $node.find('T_ID').text();
var output = '<a href="contact_detail.html?id='+idk+'">'+name+'<br />';
var org = $node.find('T_ORGANIZATIONNAME').text() + '<br />';
if (type == '1') {
$("<li />")
.addClass("style1")
.html(output)
.appendTo('ul');
}
else { //type == 2
$("<li />")
.addClass("style2")
.html(org)
.appendTo('ul');
}
});
- }
As you see, there is a lot of
variable declarations. Are they slowing me down??
I have seen some examples on the Web, people where using keywords like: children, child, nodes, children[0], some where putting things in Arrays[...]. .. But haven't found any good docs that explaines them.
- <?xml version="1.0" encoding="UTF-8"?>
<RESPONSE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://0.0.0.0:9999/rest/schemas/contacts_response.xsd">
<PARAMETERS>
<Contacts>
<contact url="http://0.0.0.0:9999/rest/contact/1">
<T_ID>1</T_ID>
<T_LASTNAME>Fox</T_LASTNAME>
<T_FIRSTNAME>Fire</T_FIRSTNAME>
<T_ORGANIZATIONNAME>Mozilla</T_ORGANIZATIONNAME>
<T_TYPE>1</T_TYPE>
<T_NAME>Fire Fox, Mozilla</T_NAME>
</contact>
...
...
<contact url="http://0.0.0.0:9999/rest/contact/14214">
<T_ID>14214</BCNT_ID>
<T_LASTNAME>Something</T_LASTNAME>
<T_FIRSTNAME>Something</T_FIRSTNAME>
<T_ORGANIZATIONNAME>In the month of May</T_ORGANIZATIONNAME>
<T_TYPE>1</T_TYPE>
<T_NAME>Something something, In the month of May</T_NAME>
</contact>
Summary:
Extract every: FirstName, LastName. Add a new <li> to the <ul> (wich is in the body) and insert the extracted data. Basically, creating DOM elements with dynamic data.
If a type == 2 then only print organization name.
that's all.