Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

add empty xml node?

Posted on 2010-08-31
8
Medium Priority
?
506 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

 
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

Docker-Compose to Simplify Multi-Container Builds

Our veteran DevOps Author takes you through how to build a multi-container environment, managed with a single utility in order to simplify your deployments.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

721 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