/ / SimpleXMLフェッチタグを含む。すべての子要素-php、xml、rss、tags、simplexml

SimpleXMLフェッチタグを含むすべての子要素 ​​- php、xml、rss、tags、simplexml

simplexmlでrssファイルを解析したいと思います。 特定のタグを印刷する場合、直接コンテンツのみが選択されます-子タグと「サブ」タグは含まれません。タグを含むタグにアクセスするにはどうすればよいですか。子タグ名とそのコンテンツ?

//load rss into $xml
foreach ($this->xml->channel->item as $item) {
echo "<h3>{$this->out($item->title)}</h3>",
$this->out($item->description);
}

回答:

回答№1は0

$ this-> xml-> children()を使用して子を取得できますノード、そしてそれを再帰的に使用します。最近、あるXMLブロックを別のXMLブロックに再帰的にコピーするメソッドを作成しました。これは、使用できるテクニックを示しているはずです。

protected function copyXml(SimpleXMLElement $from, SimpleXMLElement $to)
{
// Create a new branch in the target, as per the source
$fromValue = (string) $from;
if ($fromValue)
{
$toChild = $to->addChild($from->getName(), (string) $from);
}
else
{
$toChild = $to->addChild($from->getName());
}

// Copy attributes across
foreach ($from->attributes() as $name => $value)
{
$toChild->addAttribute($name, $value);
}

// Copy any children across, recursively
foreach ($from->children() as $fromChild)
{
$this->copyXml($fromChild, $toChild);
}
}

HTH。