PHP SimpleXML 路径存储在变量中
我试图创建一个函数,当我用 simpleXML 告诉它时,它会插入一个子元素,我有这个函数:
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']);
}
所以在这个函数中我需要 $xml->$path 像这样: $xml->url[2] 当simpleXML 在我调用时使用它: editSitemap( 'compare', 'url[1]', 'loc', 'http://truefoodlooks.com/usercp.php', NULL );
或者我需要 $xml->$path 像这样: $xml->url[2]->loc 当 simpleXML 在我调用时使用它: editSitemap( 'compare', 'url[1]->loc ) loc', '名称', '用户 cp', NULL );
有什么想法如何做到这一点?
谢谢 Nat
@PaulPRO 明白了:
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']);
}
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( 'compare', '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']);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑
我之前使用
{$path}
的解决方案不起作用。事实上,我认为如果不使用 eval 这是不可能的...:( 这是一个使用 eval 的解决方案: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: