Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22787

Re: Data being skipped when parsing xml using javascript

Message-ID <4205866.74lTBbsI5X@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-01-25 21:19 +0100
Subject Re: Data being skipped when parsing xml using javascript
Newsgroups comp.lang.javascript
References <5516dee4-5c91-4194-877e-cf1cd4ce4bc4@googlegroups.com>

Show all headers | View raw


paul.lee.1971@googlemail.com wrote:

> […] http://paullee.com/secondchance/geocodetest4.php and the data I am 
putting in is "Dogs", 40 (miles) and CB23 6XL (the test UK postcode).
> […]
> 
http://www.paullee.com/secondchance/testprocess.php?selected_animal=Dogs&inputted_distance=40&my_address_lat=52.2124688&my_address_long=-0.02406139999993684
> There's a screengrab of the problem here: paullee.com/xml-missing.jpg
> […]
> The Ajax code seems to be working OK and returns a full dataset, which you
> can see from the testprocess.php page link above. But some of the xml data
> is not being parsed and displayed on screen (the calls to the maps API
> seems to be OK). There's a screengrab of the problem here:
> paullee.com/xml-missing.jpg - The raw xml from textprocess.php is on the
> right; the output to the screen via Javascript manipulation is on the
> left. Some of the data is missing on the html page which I have indicated.
> 
> I know that the html, javascript etc. in these links is not A+ but as I
> said I am still experimenting and wanted to get a proof of concept working
> before taking the idea any further.

1. I am not certain whether that is the reason for the problem (my debugging 
   has not showed it yet), but you should fix your markup first, both the 
   generated XML and the generated HTML, at least.

   The element content in your generated XML document often begins with
   whitespace (you can see that if you read the *source*, e.g. with Ctrl+U). 
   Because you use the element content verbatim, that whitespace also shows
   up in the HTML markup that you generate from it.  You can see that if you
   use one of the tools described at <http://devtoolsecrets.com/> to inspect
   the value of your “generated_html” property while debugging.

2. Watch for the scope of your identifiers.  Some of them are not declared
   as variables which makes them global properties in non-strict mode,
   unnecessarily.  Insert

     "use strict";

   at the top of your script code in order to see that with more recent 
   ECMAScript implementations.

3. Your approach at processing the generated XML markup is both inefficient
   and logically in error:

| x=response.getElementsByTagName("sanctuary");
| generated_html="";
| 
| for (i=0;i<x.length;i++)
| {
|   //var sanc_name = x[i].childNodes[0].nodeValue; // ORIGINAL
|   var sanc_name =
|     response.getElementsByTagName("name")[i].childNodes[0].nodeValue;
|   var sanc_address =
|     response.getElementsByTagName("address")[i].childNodes[0].nodeValue;
|   var sanc_website =
|     response.getElementsByTagName("website")[i].childNodes[0].nodeValue;
|   [...]
| }

   You need to look for child elements relative from each “sanctuary” 
   element, not the document root.  For example, with your approach you will
   miscount if one “sanctuary” element does not have a “website” child
   element.  See also XPath.

4. You should not fill a variable with concatenated string values in each
   iteration in order to assign to “innerHTML” later, but you should create
   and append element objects as child nodes:

---------------------------------------------------------------------------
  var list = document.createElement("ol");
  var sanctuaries = response.getElementsByTagName("sanctuary");
 
  for (var i = 0, len = sanctuaries.length; i < len; ++i)
  {
    var sanctuary = sanctuaries[i];

    var sanc_name = sanctuary.getElementsByTagName("name")[0].textContent;

    var sanc_address =
      sanctuary.getElementsByTagName("address")[0].textContent;

    var sanc_website =
      sanctuary.getElementsByTagName("website")[0].textContent;

    var sanc_telephone =
      santuary.getElementsByTagName("telephone")[0].textContent;

    var sanc_lat = sanctuary.getElementsByTagName("lat")[0].textContent;
    var sanc_lon = sanctuary.getElementsByTagName("lon")[0].textContent;
    var sanc_dist = sanctuary.getElementsByTagName("dist")[0].textContent;

    var li = document.createElement("li");
    var link = document.createElement("a");
    link.appendChild(document.createTextNode(sanc_name));

    // …

    var italic = document.createElement("i");
    
    italic.appendChild(link);
    li.appendChild(italic);

    // …

    list.appendChild(li);
  }
   
  document.body.appendChild(list);
---------------------------------------------------------------------------

   (At which point you could get rid of your #search_results element, or
   append to that element instead of document.body.)

   However, see also XSLT on how you can simply *transform* XML markup into
   HTML markup of your liking, both server-side and client-side.

There is no “javascript”, see <http://PointedEars.de/es-matrix> (when it is 
back on line).

Next time, please post *some* code here (where you would replace tabs with 
two spaces).  This is not the Web.

Finally, please avoid posting using the b0rken Google Groups, and post using 
your real name.  TIA.

<http://PointedEars.de/faq#posting>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Data being skipped when parsing xml using javascript paul.lee.1971@googlemail.com - 2014-01-25 11:05 -0800
  Re: Data being skipped when parsing xml using javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-25 21:19 +0100
    Re: Data being skipped when parsing xml using javascript paul.lee.1971@googlemail.com - 2014-01-25 14:04 -0800
      Re: Data being skipped when parsing xml using javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-01-26 00:41 +0100
  Re: Data being skipped when parsing xml using javascript Martin Honnen <mahotrash@yahoo.de> - 2014-01-26 11:25 +0100

csiph-web