SimpleXML

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
Sheep
Forum Newbie
Posts: 1
Joined: Fri Jul 28, 2006 2:22 pm

SimpleXML

Post by Sheep »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've got a problem:

Code: Select all

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<root>
	<element>Element1</element>
	<element>Element2</element>
</root>
XML;

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addChild("test", "Test!!!");

echo nl2br(str_replace(array("<", ">"), array("<", ">"), $sxe->asXML()));
does nothing... Does anyone know what I'm doing wrong?

Greetz,
Vincent


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

ENOCRYSTALBALL And the problem is?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

error_reporting(E_ALL); // so you will see the errors
// not valid XML without double quotes
// also when tested it standalone caused problems, so i've removed
$xmlstr = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
        <element>Element1</element>
        <element>Element2</element>
</root>
XML;

$sxe = new SimpleXMLElement($xmlstr);
// use single quotes for all strings without escape characters or embedded variables
$sxe->addChild('test', 'Test!!!'); // in my version of php 5.1.2 this function does not exist.

echo nl2br(htmlentities($sxe->asXML()));
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

timvw wrote:ENOCRYSTALBALL And the problem is?
Sheep wrote:does nothing...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The problem is, "does nothing" can mean many things.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Long shot, haven't got the right version of PHP here at home.... give it a go anyways ...

Code: Select all

$sxe->root->addChild('foo', 'bar');
Post Reply