Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP SimpleXML path storage in variable
    text
    copied!<p>im trying to make a function that will insert a child element when i tell it with simpleXML i have this function:</p> <pre><code>function editSitemap ( $map, $path, $node, $value = NULL, $namespace = NULL, $DOM = NULL ) { $xml = $this-&gt;Sitemaps[$map]['content']; if ( $namespace != NULL &amp;&amp; $DOM == TRUE ) { $sd = dom_import_simplexml($cur); $newNode = $sd-&gt;ownerDocument-&gt;createElement($node . ':' . $namespace, $value); $newNode = $sd-&gt;appendChild($newNode); } elseif ( $path == NULL ) { $xml-&gt;addChild( $node , $value, $namespace ); } else { $xml-&gt;$path-&gt;addChild( $node , $value, $namespace ); } $this-&gt;Sitemaps[$map]['content']-&gt;asXML($this-&gt;Sitemaps[$map]['path']); } </code></pre> <p>so in this i need $xml->$path to be like this: $xml->url[2] when simpleXML uses it when i call: editSitemap( 'compare', 'url[1]', 'loc', '<a href="http://truefoodlooks.com/usercp.php" rel="nofollow noreferrer">http://truefoodlooks.com/usercp.php</a>', NULL );</p> <p>or i need $xml->$path to be like this: $xml->url[2]->loc when simpleXML uses it when i call: editSitemap( 'compare', 'url[1]->loc', 'name', 'user cp', NULL );</p> <p>Any ideas how to do this?</p> <p>Thanks Nat</p> <hr> <p><a href="https://stackoverflow.com/questions/6824362/php-simplexml-path-storage-in-variable/6824386#6824386">@PaulPRO</a> got it:</p> <pre><code>function editSitemap ( $map, $path, $node, $value = NULL, $namespace = NULL, $DOM = NULL ) { $xml = $this-&gt;Sitemaps[$map]['content']; if ( $path != '' ){ eval('$cur = $xml-&gt;'.$path.';'); } else { $cur = $xml; } if ( $namespace != NULL &amp;&amp; $DOM == TRUE ) { $sd = dom_import_simplexml($cur); $newNode = $sd-&gt;ownerDocument-&gt;createElement($node . ':' . $namespace, $value); $newNode = $sd-&gt;appendChild($newNode); } else { $cur-&gt;addChild( $node , $value, $namespace ); } $this-&gt;Sitemaps[$map]['content']-&gt;asXML($this-&gt;Sitemaps[$map]['path']); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload