Cannot parse xml content in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michaelh613
Forum Commoner
Posts: 38
Joined: Sun Mar 16, 2008 1:35 pm

Cannot parse xml content in php

Post 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";
?>
 
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Cannot parse xml content in php

Post by Zoxive »

Looks like your looking for simplexml.

Code: Select all

$xml = new SimpleXMLElement($ab);
 
echo $xml->person[0]->firstname;
Last edited by Zoxive on Thu Mar 20, 2008 3:01 pm, edited 1 time in total.
hayson1991
Forum Newbie
Posts: 14
Joined: Thu Mar 20, 2008 1:19 pm

Re: Cannot parse xml content in php

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