Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > PHP: X технологии > Ошибка при парсинге xml на PHP4


Автор: Elessar 3.4.2007, 20:23
есть документ:

<?xml version="1.0" encoding="windows-1251"?>
<pult date="2007-03-29 17:13">
<product id="108">
<group>Акустическая система</group><manufacture>Boston Acoustics</manufacture><name>PV1000-BV</name><description>Этот сабвуфер является убедительным доказательством того, что невероятно мощное устройство может быть заключено в конструкцию небольших размеров. Речь идет о сабвуфере, который обсепечивает сногшибательное воспроизведение баса, занимая при этом менее 1 квадратного метра. 
</description><price>1268.00</price><url>http://www.pult.ru/product/108.htm?plg=Aistandil</url><img file="http://www.pult.ru/upload/images/6340.jpg" type="1" width="157" height="200" /><img file="http://www.pult.ru/upload/images/6341.jpg" type="2" width="78" height="100" /></product>
<product id="109">
<group>Акустическая система</group><manufacture>Boston Acoustics</manufacture><name>PV1000-WG</name><description>Этот сабвуфер является убедительным доказательством того, что невероятно мощное устройство может быть заключено в конструкцию небольших размеров. Речь идет о сабвуфере, который обсепечивает сногшибательное воспроизведение баса, занимая при этом менее 1 квадратного метра. 
</description><price>1268.00</price><url>http://www.pult.ru/product/109.htm?plg=Aistandil</url><img file="http://www.pult.ru/upload/images/6328.jpg" type="2" width="85" height="100" /><img file="http://www.pult.ru/upload/images/6327.jpg" type="1" width="170" height="200" /></product>



пытаюсь отпарсить таким скриптом:


Код

$news = array();

$xml = join('',file('a.xml'));
$xml = xmldoc($xml);

$xml->xpath_init();
$ctx = xpath_new_context($xml);
$nodes = xpath_eval($ctx,'//product');

foreach($nodes->nodeset as $node)
{
    $currentNews = array();
    $currentNews['id'] = $node->get_attribute('id');
    $content = $node->children();
    foreach($content as $contentNode)
    {
        if (($contentNode->type==XML_ELEMENT_NODE) &&
            (in_array($contentNode->name,array('group','manufacture'))))
            $currentNews[$contentNode->name] = $contentNode->content;
    };
    $news[] = $currentNews;
};


print_r ($news);



в итоге парсятся тока id..что не так?

 ! 
PARROT
Модератор: Название темы должно отражать ее суть!

Автор: Golda 5.4.2007, 18:56
Попробуйте так,

Код

<?php
$news = array();
$xml = join('',file('/var/www/test/test.xml'));
$xml = xmldoc($xml);
$xml->xpath_init();
$ctx = xpath_new_context($xml);
$nodes = xpath_eval($ctx,'//product');
foreach($nodes->nodeset as $node)
{
    $currentNews = array();
    $currentNews['id'] = $node->get_attribute('id');
    $content = $node->child_nodes();
    foreach($content as $contentNode)
    {
        if (($contentNode->type==XML_ELEMENT_NODE) &&
            (in_array($contentNode->name(),array('group','manufacture'))))
            $currentNews[$contentNode->name()] = $contentNode->get_content();
    };
    $news[] = $currentNews;
};
print_r ($news);
?>


На случай, если помощь еще понадобится, отредактируйте пример xml. вы забыли закрыть тег <pult>

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)