dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: XML Parsing: what am i doing wrong?

  1. #1
    Join Date
    Dec 2009
    Location
    Pretoria, South Africa
    Posts
    7

    Unhappy XML Parsing: what am i doing wrong?

    hi there,
    here is the xml file. i successfully retrieve it, no problems, it displays N number of services, each with a few values and and multiple attribute, supplier and region tags, each with subtags enclosing text.
    i don't think i should change 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.

    Code:
    <?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.

    Code:
    arr = new Array(1,Test Service,20,"1;Test Attribute","1;Test Supplier","1;0;Test Region;0.2;1;Test Region 1;1");
    Code:
    // xml parser
    function 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++;
                                  }
                             }
                        }
                        // service attributes work join(".") in
                        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++;
                        // service suppliers
                        // service regions
                        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. 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.
    Last edited by Greywacke; 12-14-2009 at 11:16 AM.

  2. #2
    Join Date
    Dec 2009
    Location
    Pretoria, South Africa
    Posts
    7
    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.

    Code:
    <?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>
    Code:
    // xml parser
    function 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";
    	}
    }

Similar Threads

  1. Parsing an XML document using SAX API
    By river22_34 in forum XML
    Replies: 3
    Last Post: 02-03-2008, 06:12 AM
  2. Try XML Junction
    By Tim in forum xml.announcements
    Replies: 0
    Last Post: 10-11-2001, 04:00 PM
  3. Data Junction Announces XML Junction 7.51
    By Tim Frost in forum xml.announcements
    Replies: 0
    Last Post: 04-02-2001, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center