Solved

add empty xml node?

Posted on 2010-08-31
8
428 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:pillmill
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to count occurrences of each item in an array.
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 …

772 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

11 Experts available now in Live!

Get 1:1 Help Now