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


Groups > comp.lang.javascript > #22786 > unrolled thread

Data being skipped when parsing xml using javascript

Started bypaul.lee.1971@googlemail.com
First post2014-01-25 11:05 -0800
Last post2014-01-26 11:25 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#22786 — Data being skipped when parsing xml using javascript

Frompaul.lee.1971@googlemail.com
Date2014-01-25 11:05 -0800
SubjectData being skipped when parsing xml using javascript
Message-ID<5516dee4-5c91-4194-877e-cf1cd4ce4bc4@googlegroups.com>
Hi,
I'm trying to develop a small webpage for charity and am experimenting with Javascript and XML, which I have used before with some success. Basically, the user inputs their UK postcode, selects from a drop box and inputs a distance, and the resultant data is obtained via Ajax from a subsidiary data processing page and is then displayed on screen as html as well as on a map. 

This is the experimental page: http://paullee.com/secondchance/geocodetest4.php and the data I am putting in is "Dogs", 40 (miles) and CB23 6XL (the test UK postcode).

The php AJAX programme that I have written can be queried as a standalone programme; for the above set of data parameters, I use the following URL to test the output:

http://www.paullee.com/secondchance/testprocess.php?selected_animal=Dogs&inputted_distance=40&my_address_lat=52.2124688&my_address_long=-0.02406139999993684

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.

[toc] | [next] | [standalone]


#22787

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-01-25 21:19 +0100
Message-ID<4205866.74lTBbsI5X@PointedEars.de>
In reply to#22786
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.

[toc] | [prev] | [next] | [standalone]


#22789

Frompaul.lee.1971@googlemail.com
Date2014-01-25 14:04 -0800
Message-ID<44d93bd1-33e3-45b8-bb31-c7e6a086445b@googlegroups.com>
In reply to#22787
Hi Thomas,
Thanks for your reply. My comments are below.

> 
> 
> 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.  

I've fixed this. I got the data from several dozen sources and the scraping routine put in trailing whitespace for God knows what reason. I've put a trim(...) call in the called php function to strip it out. But it doesn't seem to fix the problem of data being omitted..

> 
> 2. Watch for the scope of your identifiers.  
>


Thanks, I'll put this in in the next iteration of my code. At the moment I just want to get something working before prettying it up. My knowledge of Ajax goes back at least five years and I've rarely touched it since then, so this is a good opportunity for me to revisit all the code I've written, which isn't much.


> 
> 3. Your approach at processing the generated XML markup is both inefficient
>    and logically in error:
> 
> 
OK, thanks, I'm working on this now. It doesn't explain why data is being skipped which is infuriating. Remember that my knowledge of Ajax and its associated gubbins is archaic. Incidentally, the php function I call ensures
that each parent node has all the necessary data below it, even if its blank (you noted that my old code would miscount if "sanctuary" did not have a "website" node, but because of the way the data is outputted, this won't happen.)

> 
> 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:
> 
> 
OK, this is something for a later iteration. As I said, I just want to get the darned thing working ;)



> 
> 
> Next time, please post *some* code here (where you would replace tabs with  
> two spaces).  This is not the Web.
> 
> 
OK fair enough. In the past (not here) I have been lambasted for posting too much code. When I said that all the code I put up was relevant, it evoked a flame war. Now I just put links up to the relevant pages. I've had my fingers bitten before, I don't want that to happen again. Hope you understand :(

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


Are these groups "broken"? They seem OK to me. I rarely post on the groups, so much so that I haven't even set my real name, but I hope that it would be obvious from my address.

Best wishes

Paul

[toc] | [prev] | [next] | [standalone]


#22790

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-01-26 00:41 +0100
Message-ID<4966154.XEByQThCZJ@PointedEars.de>
In reply to#22789
paul.lee.1971@googlemail.com wrote:

[restored attribution]

> [Thomas 'PointedEars' Lahn wrote: ]
>> 2. Watch for the scope of your identifiers.
> 
> Thanks, I'll put this in in the next iteration of my code. At the moment I
> just want to get something working before prettying it up. My knowledge of
> Ajax goes back at least five years and I've rarely touched it since then,
> so this is a good opportunity for me to revisit all the code I've written,
> which isn't much.

It is important for the correct operation of your program, especially as it 
uses a hand-crafted XHR (which you call “Ajax”) wrapper, that your 
identifiers do not have too much scope; otherwise you could end up reusing 
them, with side-effects.
 
>> 3. Your approach at processing the generated XML markup is both
>> inefficient
>>    and logically in error:
> 
> OK, thanks, I'm working on this now. It doesn't explain why data is being
> skipped […]

Why not?

>> 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:
> 
> OK, this is something for a later iteration. As I said, I just want to get
> the darned thing working ;)

You might generate invalid HTML if you use “innerHTML” and do not escape 
special characters in the retrieved element content.  This can cause content 
not to be displayed.

Creating and appending element objects prevents code injection and makes 
escaping unnecessary.  There are wrappers like my 
jsx.dom.createNodesFromObj() that reduce the amount of code that you have to 
write for this.

>> Next time, please post *some* code here (where you would replace tabs
>> with two spaces).  This is not the Web.
> 
> OK fair enough. In the past (not here) I have been lambasted for posting
> too much code.

And rightly so.  You had wasted precious resources of other people.

> When I said that all the code I put up was relevant, it evoked a flame
> war. Now I just put links up to the relevant pages. I've had my fingers
> bitten before, I don't want that to happen again. Hope you understand :(

I don't.  Emphasis was on “some”.  If you can post a reduced test case, it 
shows that you have basically understood what the code is doing.  Therefore, 
the assumption is that you are much more likely to find the bug yourself, 
while you are doing that, or after you have been given advice of which you 
have proven yourself worthy then.  Free time is a precious resource, too.

>> Finally, please avoid posting using the b0rken Google Groups, and post
>> using your real name.  TIA.
> 
> Are these groups "broken"? They seem OK to me.

If you are using Google Groups, you are using a Usenet archive that has been 
extended with an as yet Usenet-incompatible mail-to-news gateway; you are 
not using Usenet (as it was intended).  It also has built-in error-
correction, so that you see its errors only when you read those b0rken 
articles on Usenet.

Among other problems, it does not impose the customary 80-characters limit 
on line length or even tells you that you should do that; each of your 
paragraphs was posted as one looong line (> 230 characters).  Only lucky for 
you, my newsreader can correct this problem when *replying* (reading the 
original text was a PITA, though, despite client-side word-wrap).  Other of 
its bugs are not as easily corrected; for example, empty quoted lines are 
sometimes inserted between each quoted line, bloating the article and making 
it virtually unreadable.

And then there is the spam problem.  Google Groups is a source of spam as 
spam is not regulated/deleted there in such a way that *all* Usenet users 
can benefit from it.  Spammer's accounts are not canceled there even upon 
numerous complaints; spam and other abuse is only hidden from Google Groups 
users.

> I rarely post on the groups, so much so that I haven't even set my real
> name, but I hope that it would be obvious from my address.

Addresses are for mailboxes; names are for people.  I do not communicate 
with mailboxes on the Net; I communicate with people.

Please also leave the attribution line next time, shorten it if possible 
(yet another GG bug to work around).

-- 
PointedEars

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

[toc] | [prev] | [next] | [standalone]


#22792

FromMartin Honnen <mahotrash@yahoo.de>
Date2014-01-26 11:25 +0100
Message-ID<52e4e29b$0$6670$9b4e6d93@newsspool2.arcor-online.net>
In reply to#22786
paul.lee.1971@googlemail.com wrote:

> I'm trying to develop a small webpage for charity and am
> experimenting with Javascript and XML, which I have used before with
> some success. Basically, the user inputs their UK postcode, selects
> from a drop box and inputs a distance, and the resultant data is
> obtained via Ajax from a subsidiary data processing page and is then
> displayed on screen as html as well as on a map.
>
> This is the experimental page:
> http://paullee.com/secondchance/geocodetest4.php and the data I am
> putting in is "Dogs", 40 (miles) and CB23 6XL (the test UK
> postcode).


I think one reason for the problem is the use of the same variable named 
"i" in an outer and in an inner for loop, make sure you use different 
variables names as in Javascript there is no block scope with "var i".

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web