Reading xml data using php in an array of Javascript structs

  • Thread starter The Mysterious Mr. Rocco
  • Start date
T

The Mysterious Mr. Rocco

Hi,
I have an xml file which has user defined data, and I'm trying to use
php to write the data
into an array of Javascript structs. The code I have as follows is:

<html>
<head>
</head>
<body>

<script type="text/javascript">

var foundresults=new Array();

<?php
$doc = new DOMDocument();
$doc->load( 'results.xml' );

$counter=0;

$results = $doc->getElementsByTagName( "company" );
foreach( $results as $company )
{
$authors = $company->getElementsByTagName( "name" );
$name = $authors->item(0)->nodeValue;

$publishers = $company->getElementsByTagName( "postcode" );
$postcode = $publishers->item(0)->nodeValue;

$titles = $company->getElementsByTagName( "url" );
$url = $titles->item(0)->nodeValue;

echo "foundresults[$counter] = {company: '$name', postcode:
'$postcode', url: '$url'}; ";

$counter++;
}
?>

</script>


<script type="text/javascript">

for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}

</script>

</body>
</html>

The document.write(...) statement is simply to test that the
"foundresults" array is being populated and accessible outside the
first javascript brace. However, nothing is being outputted to screen.
But when I view source of the document, I get:

<html>
<head>
</head>
<body>

<script type="text/javascript">

var foundresults=new Array();

foundresults[0] = {company: '
Webaholics
', postcode: '
KT12 3NY
', url: '
http://www.webaholics.com
'}; foundresults[1] = {company: '
Webfiends
', postcode: '
KT12 3PP
', url: '
http://www.webfiends.com
'};
</script>


<script type="text/javascript">

for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}

</script>

</body>
</html>

Despite the strange line breaks in the population of the foundresults
array in the javascript, the code looks fine to me, and I am
mystified as to why the code isn't working. The php part is definitely
reading the data in correcttly (I tested that thoroughly!) but the
javascript is in a sulk.
Can anyone please help?

Best wishes

Trev
 
M

Matthias Reuter

The said:
<script type="text/javascript">

var foundresults=new Array();

foundresults[0] = {company: '
Webaholics
', postcode: '
KT12 3NY
', url: '
http://www.webaholics.com
'}; foundresults[1] = {company: '
Webfiends
', postcode: '
KT12 3PP
', url: '
http://www.webfiends.com
'};
</script>


<script type="text/javascript">

for(i=0;i<foundresults.length;i++)
{
document.write(foundresults.name + ' ' + foundresults.postcode +
' ' + foundresults.url +'<br>');
}

</script>

</body>
</html>

Despite the strange line breaks in the population of the foundresults
array in the javascript, the code looks fine to me,


Hooray, you found the problem. A linebreak within a Javascript string
leads to a syntax error (unterminated literal string). You would have
found that error if you had looked into Firefox' error console or with the
Firebug plugin. In Firebug's DOM tab you could have inspected the
foundresults array without document.write. And you might try the array
literal notation:

var foundentries = [{
company : "foo",
url : "http://example.com"
},
{
company : "bar",
url : "http://example.com"
}];

My assumption of why there are linebreaks is that they already are in the
xml document. So you'd have to escape them. This generally is a good idea.
What happens if $name contains a ' (single quote)?

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top