Connecting Tech Pros Worldwide Forums | Help | Site Map

How parse XML variable to Javascript

Newbie
 
Join Date: Sep 2007
Posts: 31
#1: Oct 1 '07
Your guidance please.
I currently have a Javascript application, that starts by reading some simple XML data, but it is getting more complex the more browsers I try to incorporate.

Then I read ... that php takes care of browser differences :-)

I found a simpleXmp example:

[PHP]<?php
$xml = simplexml_load_file("test.xml");echo $xml->getName() . "<br />";foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?> [/PHP]

Before I go too far, could someone guide me...
a) I know the names of my XML variables - is there a PHP construct ... to readin variable X
b) once I have read variable X - is there a way to pass it to Javascript for further (client-side) calculation?
Thanks

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,753
#2: Oct 2 '07

re: How parse XML variable to Javascript


Hi.

Check out the SimpleXML functions in the PHP Manual.

You can use PHP to echo any output your client-side scripts may need. For example:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. <?php
  3.   $msg = "This is a message from PHP";
  4.   echo "var msg = \"$msg\";";
  5.   echo "alert(msg);";
  6. ?>
  7. </script>
  8.  
Should bring up a message box with the given message.
Newbie
 
Join Date: Sep 2007
Posts: 31
#3: Oct 2 '07

re: How parse XML variable to Javascript


That's encouraging thanks.
I looked at your link (which is clearer than ones I'd found before), but although I've got lots of languages, PHP isnt one of them ;-(

Can you spare a little more help please. The example from your link shows:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include 'example.php';
  3. $xml = new SimpleXMLElement($xmlstr);
  4. echo $xml->movie[0]->plot; // "So this language. It's like..."
  5. ?> 
  6.  
What' does "$xmlstr" come from/refer to in line 3 ?
Ah ... just seen the include in the line above !!! Please ignore this post .... i'll come back later & let you know how I got on.

Thanks again 4 your help.
Newbie
 
Join Date: Sep 2007
Posts: 31
#4: Oct 3 '07

re: How parse XML variable to Javascript


I tried SimpleXML, but my host is still running PHP 4.4.0 ;-(
so I tried here: http://www.criticaldevelopment.net/xml/doc.php
where there's a PHP4 parser, but I get no output
(well other than the 'Hello" I put in to test it was running.)

[HTML]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>
<?php
include 'xml_parser.php';
//Get the XML document loaded into a variable
$xml = file_get_contents('myXMLfile.xml'); //which has "var1" as the 1st variable"
//Set up the parser object
$parser = new XMLParser($xml);

//Work the magic...
//Echo the plot of the first <movie>
echo "hello";
echo $parser->document->var1[0]->tagData;
?>

</body>
</html>[/HTML]

Can anyone spot where I'm going wrong? - thanks
---
EDITED UPDATE: Well I continued (trial & error) testing, ... inserted:
$parser->Parse(); // which I hadnt understood from the exaple was needed.

It still didnt work ;-?
Then I removed my "echo "hello" line and mysteriously it now works.

Why would the echo "hello" stop it working? / what's the scope of a variable in PHP? - thanks
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,753
#5: Oct 3 '07

re: How parse XML variable to Javascript


I'm glad you got it working!

The most likely reason why your echo statements would mess up your code would be a simple syntax error. They are so easy to overlook.
But there is really no way of knowing without seeing the code.

I would seriously recommend upgrading to PHP5 hosting. Support for PHP4 will be discontinued at the start of 2008, not to mention that PHP6 is (somewhere) around the corner, so PHP4 is rapidly becoming extinct.

As I say, I strongly urge you (and everybody) to upgrade to PHP5, even if that requires a switch to a more up-to-date host.
Reply