Page 1 of 1
SimpleXML object back to string
Posted: Tue Jul 21, 2009 5:08 pm
by jkwok
Hi,
I have a blank XML document that I assign to a variable and use simplexml_load_string on it to assign values to variables.
Here is an XML file called test.xml
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Here is my code using it:
Code: Select all
<?php
$xml = simplexml_load_file("test.xml");
$xml->to = "Jason";
$xml->from = "Toby";
From this point, I need to post the new/edited XML as a string. How can I do this?
Example: I want to post a string containing
Code: Select all
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Jason</to>
<from>Toby</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Any help would be greatly appreciated. I've looked up SimpleXML but with no clear answers.
Thanks,
Jason
Re: SimpleXML object back to string
Posted: Tue Jul 21, 2009 5:37 pm
by McInfo
It looks like you will need to convert the SimpleXML object into a DOMElement object with the
dom_import_simplexml function, then import the DOMElement into a DOMDocument object and use DOMDocument's saveXML method.
Edit: This post was recovered from search engine cache.
Re: SimpleXML object back to string
Posted: Tue Jul 21, 2009 5:38 pm
by requinix
There's also the
SimpleXMLElement::asXML method...
Re: SimpleXML object back to string
Posted: Tue Jul 21, 2009 5:50 pm
by McInfo
Sure, do it the easy way...
<false>simplexml_load_file() does not return a SimpleXMLElement object, so you would need to make a new instance of SimpleXMLElement instead.
</false>
<true>simplexml_load_file() returns a SimpleXMLElement object, but you can also make a new instance of SimpleXMLElement like this:
</true>
Code: Select all
<?php
$xml = new SimpleXMLElement('./test.xml', null, true);
$xml->to = 'Jason';
$xml->from = 'Toby';
echo $xml->asXML();
?>
Edit: This post was recovered from search engine cache.
Re: SimpleXML object back to string
Posted: Tue Jul 21, 2009 6:33 pm
by requinix
McInfo wrote:simplexml_load_file() does not return a SimpleXMLElement object, so you would need to make a new instance of SimpleXMLElement instead.
You sure about that?

Re: SimpleXML object back to string
Posted: Tue Jul 21, 2009 7:33 pm
by McInfo
No. I'm not sure. In fact, I'm sure it's quite the opposite.
When I was testing, I wrote a script like this.
Code: Select all
$xml = simplexml_load_file('./example.xml');
$xml->asXML();
There was no output because I had forgotten an echo, which led me to assume (incorrectly) that simplexml_load_file() returned an object other than a SimpleXMLElement.
The correct way to write the script would have been this, which works.
Code: Select all
$xml = simplexml_load_file('./example.xml');
echo $xml->asXML();
Edit: This post was recovered from search engine cache.
Re: SimpleXML object back to string
Posted: Wed Jul 22, 2009 9:20 am
by jkwok
Hi guys,
The asXML() solution worked perfectly. Thank you both very much!
I have another question related to this. Before I can post to the server, I have to start with an empty XML document tree that I have to populate and then post with. The tree starts with just single tags like so:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<request type="entity">
<merchandiseType type="token"/>
</request>
I need to send XML looking like this:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<request type="entity">
<merchandiseType type="token" key="4">Car Rental</merchandiseType>
</request>
How do I go about doing this? I was thinking something like the following however I don't know how to set the "key" value needed in the merchandiseType tag.
Code: Select all
$xml = getTemplate(); // A function to request the blank XML doc
$xml = simplexml_load_string($xml);
$xml->merchandiseType = "Car Rental";
Again, any help would be greatly appreciated!
Re: SimpleXML object back to string
Posted: Wed Jul 22, 2009 10:04 am
by jkwok
I found the answer on my own!
$xml->merchandiseType['key'] = 4;
or
$xml->merchandiseType->addAttribute('key',4);
However I have another question!
If I wanted to add a second merchandiseType tag, how would I do that?
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<request type="entity">
<merchandiseType type="token" key="4">Car Rental</merchandiseType>
<merchandiseType type="token" key="5">Flights</merchandiseType>
</request>
Re: SimpleXML object back to string
Posted: Wed Jul 22, 2009 11:12 am
by McInfo
It's been more than an hour, so I guess it's time for a hint.
PHP Manual:
SimpleXML functions
Remember that <request> is your root node, so you would be adding a child to the root node.
Edit: This post was recovered from search engine cache.
Re: SimpleXML object back to string
Posted: Wed Jul 22, 2009 11:33 am
by jkwok
Hi McInfo,
Thanks for your playful hint there. I was using the addChild() function of the SimpleXMLElement but it wasn't working when I tried adding multiple nodes. It would only create the first node, then just add a second closing tag each time.
I've found a working solution now. Thanks for your help.
Re: SimpleXML object back to string
Posted: Wed Jul 22, 2009 12:25 pm
by McInfo
If you see this, it means that there are two merchandiseType elements. The second is just empty.
Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<request>
<merchandiseType>Car Rental</merchandiseType>
<merchandiseType />
</request>
To access children with the same tag name, treat them like array elements. Also, addChild() is optional.
Code: Select all
<?php
header('Content-Type: text/xml');
$xml = new SimpleXMLElement('<book></book>');
$xml->addChild('chapter', 'Alpha');
$xml->chapter[0]->addAttribute('num', '1');
$xml->addChild('chapter', 'Beta');
$xml->chapter[1]->addAttribute('num', '2');
$xml->appendix = 'Appendix A';
$xml->appendix[1] = 'Appendix B';
echo $xml->asXML();
?>
Code: Select all
<?xml version="1.0"?>
<book>
<chapter num="1">Alpha</chapter>
<chapter num="2">Beta</chapter>
<appendix>Appendix A</appendix>
<appendix>Appendix B</appendix>
</book>
Edit: This post was recovered from search engine cache.
Re: SimpleXML object back to string
Posted: Wed Jul 22, 2009 2:05 pm
by jkwok
Awesome post, thanks again McInfo!