Solved

add empty xml node?

Posted on 2010-08-31
8
435 Views
Last Modified: 2012-08-13
If I add an extra node that has no data, I get a
compiler error: Warning: SimpleXMLElement::addChild() [simplexmlelement.addchild]: Element name is required in

How do I fix this?
$xml= simplexml_load_string( "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget</body></note>");


$newElement = simplexml_load_string('<address></address>');


$xml->addChild($newElement);

Open in new window

0
Comment
Question by:pillmill
  • 4
  • 2
  • 2
8 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33568915
Try

<?php
$xml= simplexml_load_string( "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget</body></note>");


$xml->addChild('address');
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 33571350
I thought we had answered this recently...
<?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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33571360
Here is another example with more comments, etc...  Please read the comments line-by-line, then install it and run it to see the output.  Post back if you have any questions.  Best, ~Ray
<?php // RAY_temp_pillmill_again.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// SET UP THE XML STRING
$xml = <<<EOXML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don&apos;t forget</body>
</note>
EOXML;

// LOAD THE STRING INTO AN OBJECT
$obj = SimpleXML_Load_String($xml);

// SHOW THE OBJECT BEFORE ADDING THE CHILD
var_dump($obj);

// ADD THE EMPTY CHILD NODE
$obj->addChild('address');

// SHOW THE OBJECT AFTER ADDING THE EMPTY CHILD
var_dump($obj);

// ADD SOME DATA TO THE CHILD
$obj->address = '1446 Colleen Lane';

// SHOW THE OBJECT AFTER ADDING DATA TO THE CHILD NODE
var_dump($obj);

// RECOVER THE XML STRING
$new = $obj->AsXML();
echo htmlentities($new);

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33571727
@Ray "I thought we had answered this recently..."

It seems like XML and strtotime are the "flavours of the month" at present. Chances are you answered one very similar to it.
0
DevOps Toolchain Recommendations

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

 

Author Comment

by:pillmill
ID: 33572632
Thanks. That solved the problem.

Now, instead of one node, I want to "append" a whole xml string to
the original structure. addChild throws an error: "Element name is required".

Is there a way to append with simpleXML?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33576872
Do you mean that you just want to tack on another chunk of XML after the last element like so

(before)
</lastElement>
?>

(after)
</lastElement>
<newStuff>...........
?>

I would do this by converting the current XML to a string, doing the append and then creating a new XML. It is not liable to be successful as you will probably break the well-formedness rules for XML

$temp = $xml->asXml();
$temp .= $moreXml;
$newXml = simplexml_load_string( $temp );

Personally I would just create a new child node and put it in there

$xml->addChild("moreXml", $moreXml );


0
 

Author Comment

by:pillmill
ID: 33581992
Thanks. Also worked. But some of the entities are with
html characters and some not. How do I change all of the
> and < and still keeps this as an xml object?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 33586750
Your best option is to encode it as CDATA so that XML does not interpret it. See

http://www.w3schools.com/xml/xml_cdata.asp

You could also try encoding it with htmlentities before inserting in the XML, but CDATA is the "official" way

http://www.php.net/htmlentities
http://www.php.net/html_entity_decode
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now