Solved

append child to simple_xml_string

Posted on 2010-08-23
3
549 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

777 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