开发者

PHP SimpleXML path storage in variable

im trying to make a function that will insert a child element when i tell it with simpleXML i have this function:

function editSitemap ( $map, $path, $node, $value = NULL, $namespace = NULL, $DOM = NULL ) {
    $xml = $this->Sitemaps[$map]['content']; 
    if ( $namespace != NULL && $DOM == TRUE ) {
        $sd = dom_import_simplexml($cur);
        $newNode = $sd->ownerDocument->createElement($node . ':' . $namespace, $value);
        $newNode = $sd->appendChild($newNode);
    } elseif ( $path == NULL ) {
        $xml->addChild( $node , $value, $namespace );
    } else {
        $xml->$path->addChild( $node , $value, $namespace );
    }
    $this->Sitemaps[$map]['content']->asXML($this->Sitemaps[$map]['path']);
}

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', 'http://truefoodlooks.com/usercp.php', NULL );

or i need $xml->$path to be like this: $xml->url[2]->loc when simpleXML uses it when i call: editSitemap( 'compar开发者_JAVA百科e', 'url[1]->loc', 'name', 'user cp', NULL );

Any ideas how to do this?

Thanks Nat


@PaulPRO got it:

function editSitemap ( $map, $path, $node, $value = NULL, $namespace = NULL, $DOM = NULL ) {
    $xml = $this->Sitemaps[$map]['content'];
    if ( $path != '' ){
        eval('$cur = $xml->'.$path.';');
    } else {
        $cur = $xml;
    }
    if ( $namespace != NULL && $DOM == TRUE ) {
        $sd = dom_import_simplexml($cur);
        $newNode = $sd->ownerDocument->createElement($node . ':' . $namespace, $value);
        $newNode = $sd->appendChild($newNode);
    } else {
        $cur->addChild( $node , $value, $namespace );
    }
    $this->Sitemaps[$map]['content']->asXML($this->Sitemaps[$map]['path']);
}


Edit

My previous solution with {$path} doesn't work. In fact I think this is impossible without using eval... :( Here is a solution with eval:

function editSitemap ( $map, $path, $node, $value = NULL, $namespace = NULL, $DOM = NULL ) {
    $xml = $this->Sitemaps[$map]['content']; 
    if ( $namespace != NULL && $DOM == TRUE ) {
        $sd = dom_import_simplexml($cur);
        $newNode = $sd->ownerDocument->createElement($node . ':' . $namespace, $value);
        $newNode = $sd->appendChild($newNode);
    } elseif ( $path == NULL ) {
        $xml->addChild( $node , $value, $namespace );
    } else {
        eval('$xml->'.$path.'->addChild( $node , $value, $namespace);');
    }
    $this->Sitemaps[$map]['content']->asXML($this->Sitemaps[$map]['path']);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜