Solved

add empty xml node?

Posted on 2010-08-31
8
459 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 109

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 109

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
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.

 
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
 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.

829 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