Omaruchan
Omaruchan

Reputation: 423

PHP adding child and attributes on existing XML file

I've been reading about SimpleXML and some other stuff but I have a problem I can't figure out.

So I have this simple almost empty XML:

<?xml version="1.0" encoding="UTF-8"?>
<imageData> 
</imageData>

What I want to do is every time I click a button, the XML document opens and appends a new child so it will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<imageData>
   <image id="myID"> <--- (Note the attribute in here)
      <person>MyPersonName</person>
      <number>MyNumber</number>
   </image> 
</imageData>

So far, I've been able to make it work a bit using something like this, yet I can't seem to find a way to append an attribute to the image tag which I need to be able to insert different child 'images' tags based on the ID attribute, as I'm getting very confused on how to use the $xml=new SimpleXMLElement($imageData); function on an external XML document:

$xml = simplexml_load_file('myXML.xml');
$xml->addChild('image'); <--Want to add an id="myID" attribute to this child
$xml->image->addChild('person', 'myPersonName'); <--Want to add this child to the image tag with the attribute I added up there)
$xml->image->addChild('number','Mynumber');
file_put_contents('myXML.xml', $xml->asXML());

Any help or pointing in the right direction would be greatly appreciated.

Upvotes: 3

Views: 1897

Answers (2)

VolkerK
VolkerK

Reputation: 96159

SimpleXMLElement::addChild() returns the newly created element as another SimpleXMLElement instance you can work on

<?php
$imageData = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><imageData />');

onClick( $imageData ); echo $imageData->asXML(); echo "\r\n----\r\n";
onClick( $imageData ); echo $imageData->asXML(); echo "\r\n----\r\n";
onClick( $imageData ); echo $imageData->asXML(); echo "\r\n----\r\n";
onClick( $imageData ); echo $imageData->asXML(); echo "\r\n----\r\n";


function onClick($imageData) {
    static $id = 0;
    $img = $imageData->addChild('image');
    $img['id'] = ++$id;
    $img->person = 'person #'.$id;
    $img->number = '47'.$id;
}

prints

<?xml version="1.0" encoding="UTF-8"?>
<imageData><image id="1"><person>person #1</person><number>471</number></image></imageData>

----
<?xml version="1.0" encoding="UTF-8"?>
<imageData><image id="1"><person>person #1</person><number>471</number></image><image id="2"><person>person #2</person><number>472</number></image></imageData>

----
<?xml version="1.0" encoding="UTF-8"?>
<imageData><image id="1"><person>person #1</person><number>471</number></image><image id="2"><person>person #2</person><number>472</number></image><image id="3"><person>person #3</person><number>473</number></image></imageData>

----
<?xml version="1.0" encoding="UTF-8"?>
<imageData><image id="1"><person>person #1</person><number>471</number></image><image id="2"><person>person #2</person><number>472</number></image><image id="3"><person>person #3</person><number>473</number></image><image id="4"><person>person #4</person><number>474</number></image></imageData>

----

Upvotes: 3

Cuagau
Cuagau

Reputation: 548

You don't want to use new SimpleXMLElement syntax. Just do

$xml->image->addAttribute('id', 'myID');

Upvotes: 0

Related Questions