SimpleXML object back to string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jkwok
Forum Newbie
Posts: 13
Joined: Thu May 14, 2009 11:54 pm

SimpleXML object back to string

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: SimpleXML object back to string

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:50 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SimpleXML object back to string

Post by requinix »

There's also the SimpleXMLElement::asXML method...
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: SimpleXML object back to string

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:51 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: SimpleXML object back to string

Post 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? ;)
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: SimpleXML object back to string

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:52 pm, edited 1 time in total.
jkwok
Forum Newbie
Posts: 13
Joined: Thu May 14, 2009 11:54 pm

Re: SimpleXML object back to string

Post 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!
jkwok
Forum Newbie
Posts: 13
Joined: Thu May 14, 2009 11:54 pm

Re: SimpleXML object back to string

Post 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>
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: SimpleXML object back to string

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:52 pm, edited 1 time in total.
jkwok
Forum Newbie
Posts: 13
Joined: Thu May 14, 2009 11:54 pm

Re: SimpleXML object back to string

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: SimpleXML object back to string

Post 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.
Last edited by McInfo on Wed Jun 16, 2010 3:54 pm, edited 1 time in total.
jkwok
Forum Newbie
Posts: 13
Joined: Thu May 14, 2009 11:54 pm

Re: SimpleXML object back to string

Post by jkwok »

Awesome post, thanks again McInfo!
Post Reply