3

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.

2 Answers 2

3

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>

----
Sign up to request clarification or add additional context in comments.

2 Comments

This actually answers my question except for the part that I still don't know how to call my XML file to add a new child to the imageData tag, as in, I have an XML in "C://Users/user/Desktop/directory" and I want to be able to open it and add my data there.
see docs.php.net/manual/en/function.simplexml-load-file.php | and once the file is read you just do what onClick() does. But how to avoid race conditions is up to you.
0

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

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

2 Comments

I tried that but it throws Fatal error: Call to undefined method SimpleXMLElement::addAtribute()
Attribute - with two ts. But the code as it is will always work on the first <image> element - not what you want as far as I understood the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.