/ / Tag de busca SimpleXML incl. todos os elementos filho - php, xml, rss, tags, simplexml

Tag de busca SimpleXML incl. todos os elementos filho - php, xml, rss, tags, simplexml

Eu gostaria de analisar um arquivo rss com simplexml. Se eu imprimir uma tag específica, o conteúdo direto será selecionado apenas - nenhum filho e "sub" -tags serão incluídos. Como posso acessar uma tag incl. nomes de tags de crianças e seu conteúdo?

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

Respostas:

0 para resposta № 1

Você pode usar $ this-> xml-> children () para obter filhonós e use isso recursivamente. Eu recentemente escrevi um método para copiar um bloco XML recursivamente em outro - o que deveria mostrar as técnicas que você pode usar.

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.