2015-12-04 9 views
6

Próbuję wygenerować kanał RSS przy użyciu PHP SimpleXMLElement, problem polega na tym, że muszę przedrostek elementów i nie mogę znaleźć sposobu, aby to zrobić przy użyciu klasy SimpleXMLElement .Jak wygenerować prefiksowane elementy przestrzeni nazw za pomocą SimpleXMLElement w PHP

Próbowałem już używać $item->addChild('prefix:element', 'value'), ale w wyniku xml usuwa przedrostek, , jakikolwiek pomysł, dlaczego tak się dzieje?.

Zastanawiam się, czy istnieje sposób, aby rozwiązać ten problem za pomocą innego czystsze sposób niż tylko echem XML SimpleXMLElement lub dowolny.

Dla wyjaśnienia, to jest mój kod 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(); 

I to jest przykład XML Próbuję wygenerować:

<?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> 
... 

góry dzięki

Odpowiedz

2

Musisz przekazać przedrostek przestrzeni nazw przedrostka, aby dodać element potomny z prefiksem:

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

eval.in demo:

$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(); 
Powiązane problemy