Page 1 of 1

Cannot parse xml content in php

Posted: Thu Mar 20, 2008 2:30 pm
by michaelh613
Essentially I have some basic code out of the PHP Cookbook that should parse a xml document and print the firstname in this case. However clearly I have made a mistake. I originally tried using this technique on a much more complex xml and founcd that didn't work so I went t simplify it. K just get no output

Code: Select all

 
<?php
$ab='<?xml version="1.0">';
$ab.='<address-book>';
$ab.='<person id="1"';
$ab.='<firstname>David</firstname>';
$ab.='<lastname>Sklar</lastname>';
$ab.='</person>';
$ab.='</address-book>';
 
//echo $ab;
print $ab->person['firstname'] ."\n";
?>
 

Re: Cannot parse xml content in php

Posted: Thu Mar 20, 2008 2:56 pm
by Zoxive
Looks like your looking for simplexml.

Code: Select all

$xml = new SimpleXMLElement($ab);
 
echo $xml->person[0]->firstname;

Re: Cannot parse xml content in php

Posted: Thu Mar 20, 2008 2:59 pm
by hayson1991
michaelh613 wrote:Essentially I have some basic code out of the PHP Cookbook that should parse a xml document and print the firstname in this case. However clearly I have made a mistake. I originally tried using this technique on a much more complex xml and founcd that didn't work so I went t simplify it. K just get no output

Code: Select all

 
<?php
$ab='<?xml version="1.0">';
$ab.='<address-book>';
$ab.='<person id="1"';
$ab.='<firstname>David</firstname>';
$ab.='<lastname>Sklar</lastname>';
$ab.='</person>';
$ab.='</address-book>';
 
//echo $ab;
print $ab->person['firstname'] ."\n";
?>
 
And it looks like you forgot the ">" at the end of line 5.