Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: PHP error parsing xml ONLY using namespace - EDITED [on hold]


TOTALLY EDITED because of wrong Error focusing (sorry...)

In the following code (which I need for repositioning some nodes in an Xml) I get error if the Xml has a namespace on $target = current($MyXml->xpath($FullPath.'[last()]'));

//Get Grandparents of reference nodes
$GrandParents = $MyXml->xpath('//_0/../..');
foreach ($GrandParents as $GrandParent){
$Pcount = $GrandParent->count();
//set root string using GrandParent's Tag name
$TName = (string) $GrandParent->getName();
$root = " ".$TName.">";
//count GrandChildren (They are the same number for each parent)
$Parents = $GrandParent->children();
$Ccount = $Parents[0]->count();
//Loop for "each parent's children" times
for ($CNum = 0; $CNum //Set new element using GrandParent's Tag name
$GPElem = new SimpleXMLElement($root);
//Loop for "Parents->count" times
for ($PNum = 0; $PNum //Get parent name
$PName = (string) $Parents[$PNum]->getname();
//Get child value (it'll be the NewElement->child value)
$PValue = (string) $GrandParent->xpath('./'.$PName.'/*')[$CNum];
//Add child with value to $GPElem
$PElem = $GPElem->addChild($PName, $PValue);
//Find the full path of current GrandParent
$ancestors = $GrandParent->xpath("ancestor-or-self::*");
$FullPath = "";
foreach ( $ancestors as $p ) {
$FullPath .= "/".$p->getName();
//Get where to add New element **HERE IS THE ERROR**
$target = current($MyXml->xpath($FullPath.'[last()]'));
// Insert the new element after the last $target node
simplexml_insert_after($GPElem, $target);
//Remove original GrandParent node and his descendants

I get the Xml I'm trying to parse from the following code.
I need to point out that I'm forced to write an Xml having a prefix only in the root node (for compatibility with receiver)

function array_to_xml(array $arr, SimpleXMLElement $xml, $NumK = false) {
foreach ($arr as $k => $v) {
if (is_array($v)){
//Check if is associative array looking at the keys
preg_match('/^0|[1-9]\d*$/', implode(array_keys($v)))
//The empty parameters are included to remove prefix in children nodes
? array_to_xml($v, $xml->addChild($k, '', ''), true)
: array_to_xml($v, $xml->addChild($k, '', ''));
}else{ //add node - 3rd parameter is for namespace (I need to remove it in children nodes)
$NumK //if isn't associative
? $xml->addChild('_'.$k, $v, '') //add '_' before key
: $xml->addChild($k, $v, ''); //else add node with key and value - without prefix
return $xml;

So, the real issue is:
if I call the function like so:

$MyXml = array_to_xml($TestArr, new SimpleXMLElement(''));

all works correctly.
BUT, if I call the function adding a namespace, like below, I get the error:

$MyXml = array_to_xml($_POST_Rev, new SimpleXMLElement(''));

I've already pointed out that I need a namespace prefix only in the root node.
I'll also accept hints on different logics to reach my goal

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: PHP error parsing xml ONLY using namespace - EDITED [on hold]


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription