Solved

append child to simple_xml_string

Posted on 2010-08-23
3
553 Views
Last Modified: 2013-12-13
Is there a way to append a child to a xml string.

For example, I have:

$Element = simplexml_load_string('<tree></tree>');

I want to append a child to the node "tree".
0
Comment
Question by:pillmill
  • 2
3 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 33506521
<?php

$Element = simplexml_load_string('<tree></tree>');
$Element->addChild("fred");

echo htmlentities( $Element->asXml() );

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33506531
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33511631
This will not only add the child node, but it will give it some content.
<?php // RAY_temp_pillmill.php

$obj = simplexml_load_string('<tree></tree>');
$obj->addChild("fred");
$obj->fred = 'Flintstone';

echo htmlentities( $obj->asXml() );

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question