Page 1 of 1

SimpleXML

Posted: Fri Jul 28, 2006 2:27 pm
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]

Posted: Fri Jul 28, 2006 2:54 pm
by timvw
ENOCRYSTALBALL And the problem is?

Posted: Fri Jul 28, 2006 3:31 pm
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()));

Posted: Fri Jul 28, 2006 3:32 pm
by Ollie Saunders
timvw wrote:ENOCRYSTALBALL And the problem is?
Sheep wrote:does nothing...

Posted: Fri Jul 28, 2006 5:15 pm
by feyd
The problem is, "does nothing" can mean many things.

Posted: Fri Jul 28, 2006 8:41 pm
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');