Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

SimpleXML: Swap nodes
Welcome, Guest. Please login or register.
November 20, 2008, 10:30:27 AM
11306 Posts in 1249 Topics by 501 Members
Latest Member: rosaline
Experts Round Table Network  |  Serverside Technology  |  PHP  |  SimpleXML: Swap nodes « previous next »
Pages: [1]
Author Topic: SimpleXML: Swap nodes  (Read 252 times)
GrandSchtroumpf
Mentor

Offline Offline

Posts: 409



« on: March 13, 2008, 10:57:39 AM »

How can I swap 2 nodes using SimpleXML?

Something like this:

Code
Language: php (GeSHi-highlighted)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>SimpleXML: How to swap nodes</title>
</head>
<body>
 
<h1>SimpleXML: How to swap nodes</h1>
 
<?
 
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
 <title>PHP: Behind the Parser</title>
 <characters>
  <character>
   <name>Ms. Coder</name>
   <actor>Onlivia Actora</actor>
  </character>
  <character>
   <name>Mr. Coder</name>
   <actor>El Actor</actor>
  </character>
 </characters>
 <plot>
  So, this language. It is like, a programming language. Or is it a
  scripting language? All is revealed in this thrilling horror spoof
  of a documentary.
 </plot>
 <great-lines>
  <line>PHP solves all my web problems</line>
 </great-lines>
 <rating type="thumbs">7</rating>
 <rating type="stars">5</rating>
</movie>
</movies>
XML;
 
$xml = new SimpleXMLElement($xmlstr);
 
$character0 = $xml->movie[0]->characters[0]->character[0];
$character1 = $xml->movie[0]->characters[0]->character[1];
 
echo "<h2>Character 0</h2><pre>". htmlspecialchars($character0->asXML()) ."</pre>";
echo "<h2>Character 1</h2><pre>". htmlspecialchars($character1->asXML()) ."</pre>";
 
$xml->movie[0]->characters[0]->character[0] = $character1;
$xml->movie[0]->characters[0]->character[1] = $character0;
 
echo "<h2>XML</h2><pre>". htmlspecialchars($xml->asXML()) ."</pre>";
 
?>
 
<p>The content of the character nodes has vanished!</p>
 
</body>
</html>

I found a workaround converting to XML string and using stri_replace... ugly but it works and should be solid.

From the comments on the PHP site, il looks like there is no easy way to insert XML into a simpleXML structure:
http://us2.php.net/simplexml#78855
The technique described in this comment recursively copies the nodes one by one from one structure to the other...  Even uglier than my str_replace method IMHO.  Looks like this particular code does not copy the XML attributes, but that could be fixed.

Have I missed something?  Is there some magic notation that I should use?

Except for that small detail, SimpleXML is great !

Cheers and thanks in advance for your constructive feedback ;)
Logged
CrYpTiC_MauleR
Site Builder

Offline Offline

Posts: 489



WWW
« Reply #1 on: March 22, 2008, 09:52:05 PM »

I tried to see how to do this but also failed. My method was to do array_reverse() or arsort() on the character array, but PHP errors saying the argument is not an array but an object. So I am guessing all the nodes are treated as properties instead. Sorry for the lack of help, it has me stumped too.
Logged

[x] Fight | www.crypticmauler.com
"You must be
Pages: [1]
« previous next »
    Jump to: