如何使用PHP中的SimpleXMLElement生成名称空间前缀xml元素?

时间:2022-11-23 08:26:45

I'm trying to generate a RSS feed using PHP SimpleXMLElement, the problem is that i need to prefix elements and can't find a way to do this using the SimpleXMLElement class.

我尝试使用PHP SimpleXMLElement生成RSS提要,问题是我需要对元素进行前缀,并且无法找到使用SimpleXMLElement类来实现这一点的方法。

I've tried using $item->addChild('prefix:element', 'value') but in the result xml it strips the prefix, any idea why this happens ?.

我尝试过使用$item->addChild('prefix:element', 'value'),但是在结果xml中它去掉了前缀,知道为什么会这样吗?

I wonder if there is a way to solve this using the SimpleXMLElement or any other cleaner way than just echoing the XML.

我想知道是否有一种方法可以使用SimpleXMLElement或其他更简洁的方法来解决这个问题,而不仅仅是响应XML。

For clarification, this is my PHP code:

澄清一下,这是我的PHP代码:

    $xml = new SimpleXMLElement('<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"/>');
    $channel = $xml->addChild('channel');
    $channel->addChild('title', 'Text');
    $channel->addChild('link', 'http://example.com');
    $channel->addChild('description', 'An example item from the feed.');

    foreach($this->products as $product) {
        $item = $channel->addChild('item');

        foreach($product as $key => $value)
            $item->addChild($key, $value);
    }

    return $xml->asXML();

And this is the example XML i'm trying to generate:

这就是我要生成的XML示例:

<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
    <title>Test Store</title>
    <link>http://www.example.com</link>
    <description>An example item from the feed</description>

    <item>
        <g:id>DB_1</g:id>
        <g:title>Dog Bowl In Blue</g:title>
        <g:description>Solid plastic Dog Bowl in marine blue color</g:description>
        ...
    </item>
...

Thanks in advance

谢谢提前

1 个解决方案

#1


2  

You need to pass the namespace uri of the prefix to add child element with prefix :

您需要传递前缀的名称空间uri,以添加带有前缀的子元素:

$item->addChild($key, $value, 'http://base.google.com/ns/1.0');

eval.in demo :

eval。在演示:

$xml = new SimpleXMLElement('<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"/>');
$channel = $xml->addChild('channel');
$channel->addChild('title', 'Text');
$channel->addChild('link', 'http://example.com');
$channel->addChild('description', 'An example item from the feed.');

$item = $channel->addChild('item');
$item->addChild('g:foo', 'bar', 'http://base.google.com/ns/1.0');

print $xml->asXML();

#1


2  

You need to pass the namespace uri of the prefix to add child element with prefix :

您需要传递前缀的名称空间uri,以添加带有前缀的子元素:

$item->addChild($key, $value, 'http://base.google.com/ns/1.0');

eval.in demo :

eval。在演示:

$xml = new SimpleXMLElement('<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0"/>');
$channel = $xml->addChild('channel');
$channel->addChild('title', 'Text');
$channel->addChild('link', 'http://example.com');
$channel->addChild('description', 'An example item from the feed.');

$item = $channel->addChild('item');
$item->addChild('g:foo', 'bar', 'http://base.google.com/ns/1.0');

print $xml->asXML();