Jump to content

Xml Parsing: What Am I Doing Wrong?


Greywacke

Recommended Posts

hi there,here is the xml file. i successfully retrieve it, no problems, it contains N number of services, each with 3 values in text nodes and and multiple attribute, supplier and region tags, each with subtags enclosing text, regions with 4 and suppliers and attributes with 2.i don't think i should think of changing the xml file, i need to parse it as it is / will be. here is the xml with testing data, one supplier, with one region (and its children), and one attribute. the sql secondary tier i can also retrieve no problems for debugging.

<?xml version="1.0" encoding="ISO-8859-1"?><root>     <service>          <serviceid>1</serviceid>          <servicedescription>Test Service</servicedescription>          <costperlead>20</costperlead>          <attribute>               <attributeid>1</attributeid>               <attributedesc>Test Attribute</attributedesc>          </attribute>          <supplier>               <supplierid>1</supplierid>               <suppliername>Test Supplier</suppliername>          </supplier>          <region>               <regionid>1</regionid>               <regionparent>0</regionparent>               <regionname>Test Region</regionname>               <indent>0</indent>          </region>          <region>               <regionid>2</regionid>               <regionparent>1</regionparent>               <regionname>Test Region 1</regionname>               <indent>1</indent>          </region>     </service>     <sql>          SELECT * FROM 2_servicescatalogue ORDER BY text_ServiceDescription ASC;          SELECT * FROM 3_serviceattributes WHERE bigint_AttributeServiceID = 1 ORDER BY text_AttributeDescription ASC;          SELECT DISTINCT bigint_SupplierID FROM 4_servicesuppliers WHERE bigint_ServiceID = 1 ORDER BY bigint_SupplierID ASC;          SELECT * FROM 5_suppliers WHERE bigint_SupplierID = 1 ORDER BY text_SupplierName ASC;          SELECT DISTINCT bigint_RegionID FROM 4_servicesuppliers WHERE bigint_ServiceID = 1 ORDER BY bigint_RegionID ASC;          SELECT * FROM 1_regions WHERE bigint_RegionID = 1 ORDER BY text_RegionDescription ASC;          SELECT * FROM 1_regions WHERE bigint_ParentRegionID = 1 ORDER BY text_RegionDescription ASC;          SELECT * FROM 1_regions WHERE bigint_ParentRegionID = 2 ORDER BY text_RegionDescription ASC;     </sql></root>

the javascript alertContents function i have sofar, follows below the format of the array passed to the addrecord() function that i'm trying to establish. i have been able to parse the supplier nodes and children, except of course the attribute nodes and children, which is why i am please asking for help, all i get passed is an array of 1,Test Service,20.

// xml parserfunction alertContents() {	 if (http_request.readyState == 4) {		  if (http_request.status == 200) {			   var xmldoc = http_request.responseXML;			   // parse services			   var rows = xmldoc.getElementsByTagName("service");			   for (var r = 0; r < rows.length; r++) {					var i = 0;					var arr = new Array();					for (var c = 0; c < rows[r].childNodes.length; c++) {						 var servicedetail = rows[r].childNodes[c];						 if (servicedetail.childNodes.length > 0) {							  if (trim(servicedetail.firstChild.data) != "") {								   arr[i] = servicedetail.firstChild.data;								   i++;							  }						 }					}                                        // BEGIN SERVICE ATTRIBUTES ******************************					var attributes = rows[r].getElementsByTagName("attibute");					var x = 0;					var arr1 = new Array();					for (var a = 0; a < attributes.length; a++) {						 var y = 0;						 var arr2 = new Array();						 for (var aa = 0; aa < attributes[a].childNodes.length; aa++) {							  attribute = attributes[a].childNodes[aa];							  if (attribute.childNodes.length > 0) {								   if (trim(attribute.firstChild.data) != "") {										arr2[y] += attribute.firstChild.data;										y++;								   }							  }						 }						 arr1[x] = arr2.join(";");						 x++;					}					arr[i] = arr1.join(".");					i++;                                        // END SERVICE ATTRIBUTES ********************************					// service suppliers to come					// service regions to come					alert(arr);					addrecord("list_services", arr);			   }			   //if (xmldoc.getElementsByTagName("sql")[0]) if (xmldoc.getElementsByTagName("sql")[0].firstChild)                           //var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != "") alert(sql);		  } else {			   alert('There was a problem with the request.');		  }		  document.getElementById("ajaxbg").style.visibility = "hidden";	 }}

i know i'm not approaching the attribute tags correctly, but i might be close - i unfortunately have no clue how close or which way to go from now. i've literally hit the virtual horizon to horizon wall and can't see the bricks that are not there.many thanks in advance, even if its a nudge in the right direction - Pierre.

Link to comment
Share on other sites

nevermind, i've decided to ###### with the xml structure - why battle to parse down endless tiers of nodes if you can minimize those by using nodes with attributes. below is the xml structure and below that is the alertContents function.

<?xml version="1.0" encoding="ISO-8859-1"?><root>	<service>		<serviceid>1</serviceid>		<servicedescription>Test Service</servicedescription>		<costperlead>20</costperlead>		<attribute id="1" description="Test Attribute" />		<supplier id="1" name="Test Supplier" />		<region id="1" parent="0" name="Test Region" indent="0" />		<region id="2" parent="1" name="Test Region 1" indent="1" />	</service>	<sql>SELECT * FROM 2_servicescatalogue ORDER BY text_ServiceDescription ASC;SELECT * FROM 3_serviceattributes WHERE bigint_AttributeServiceID = 1 ORDER BY text_AttributeDescription ASC;SELECT DISTINCT bigint_SupplierID FROM 4_servicesuppliers WHERE bigint_ServiceID = 1 ORDER BY bigint_SupplierID ASC;SELECT * FROM 5_suppliers WHERE bigint_SupplierID = 1 ORDER BY text_SupplierName ASC;SELECT DISTINCT bigint_RegionID FROM 4_servicesuppliers WHERE bigint_ServiceID = 1 ORDER BY bigint_RegionID ASC;SELECT * FROM 1_regions WHERE bigint_RegionID = 1 ORDER BY text_RegionDescription ASC;SELECT * FROM 1_regions WHERE bigint_ParentRegionID = 1 ORDER BY text_RegionDescription ASC;SELECT * FROM 1_regions WHERE bigint_ParentRegionID = 2 ORDER BY text_RegionDescription ASC;	</sql></root>

// xml parserfunction alertContents() {	if (http_request.readyState == 4) {		if (http_request.status == 200) {			var xmldoc = http_request.responseXML;			// parse services			var rows = xmldoc.getElementsByTagName("service");			for (var x = 0; x < rows.length; x++) {				var i = 0;				var arr = new Array();				var aa = 0;				var arr1 = new Array();				var ss = 0;				var arr3 = new Array();				var rr = 0;				var arr5 = new Array();				for (var c = 0; c < rows[x].childNodes.length; c++) {					var servicedetail = rows[x].childNodes[c];					if (servicedetail.childNodes.length > 0) {						if (trim(servicedetail.firstChild.data) != "") {							arr[i] = servicedetail.firstChild.data;							i++;						}					} else if (servicedetail.nodeName == "attribute") {						var arr2 = new Array();						for (var a = 0; a < servicedetail.attributes.length; a++) {							arr2[a] = servicedetail.attributes[a].value;						}						arr1[aa] = arr2.join(";");						aa++;					} else if (servicedetail.nodeName == "supplier") {						var arr4 = new Array();						for (var s = 0; s < servicedetail.attributes.length; s++) {							arr4[s] = servicedetail.attributes[s].value;						}						arr3[ss] = arr4.join(";");						ss++;					} else if (servicedetail.nodeName == "region") {						var arr6 = new Array();						for (var r = 0; r < servicedetail.attributes.length; r++) {							arr6[r] = servicedetail.attributes[r].value;						}						arr5[rr] = arr6.join(";");						rr++;					}				}				arr[i] = arr1.join(".");				i++;				arr[i] = arr3.join(".");				i++;				arr[i] = arr5.join(".");				i++;				alert(arr);				addrecord("list_services", arr);			}			//if (xmldoc.getElementsByTagName("sql")[0]) if (xmldoc.getElementsByTagName("sql")[0].firstChild)var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != "") alert(sql);		} else {			alert('There was a problem with the request.');		}		document.getElementById("ajaxbg").style.visibility = "hidden";	}}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...