<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
...
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
Парсинг XML
Код:
Если пытаюсь вытащить значения и раскидать данные по массиву следующим образом
Код:
$simple = implode(file($xmlfile));
$xml = xml_parser_create();
xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml,XML_OPTION_SKIP_WHITE,1);
$indexes = array();
$values = array();
xml_parse_into_struct($xml,$simple, $values, $indexes);
xml_parser_free($xml);
//print '<pre>';
//print_r( $values );
$xml = xml_parser_create();
xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml,XML_OPTION_SKIP_WHITE,1);
$indexes = array();
$values = array();
xml_parse_into_struct($xml,$simple, $values, $indexes);
xml_parser_free($xml);
//print '<pre>';
//print_r( $values );
то скрипт вытаскивает только первую ветку, дальше не идет, хелпните как сделать, второй день сижу мозг мучаю.
http://ru2.php.net/manual/en/ref.simplexml.php
ЗЫ Кстати если вы привели ПОЛНЫЙ код xml-файла, то он неверный и в этом вероятно источник проблемы
В том то и деле что код полный, где троеточчие еще блоки идут, если бы был общий блок, к котором болтаются остальные, то не парился бы, разобрал. А из-за этой хрени не могу понять в чом дело.
А если изменить файл к такому виду заработает?
Код:
<?xml version="1.0" encoding="UTF-8" ?>
<Info>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
</Info>
<Info>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
<S time="1122456484" shop="New Moscow Shop" xy="0/-1" city="New Moscow" >
<R name="Metals" cost="0.5" need="1646" />
<R name="Gold" cost="1" need="20641" />
<R name="Polymers" cost="4" need="14362" />
<R name="Organic" cost="1.2" need="17988" />
<R name="Silicon" cost="0.1" need="81165" />
<R name="Radioactive" cost="1.5" need="17572" />
<R name="Gems" cost="0.4" need="22663" />
<R name="Venom" cost="1" need="10294" />
</S>
</Info>
То есть предварительно записаь в файл типа кэша? Хмм, счаз попробую.
Нет. Задать заголовок
Код:
<?xml version="1.0" encoding="UTF-8" ?>
и корневой тег (в нашем случае <Info></Info>)...
Иначе это НЕ XML-файл
идентификатор ХМл добавил, корневой тег тоже, повесил это всё в кэш, дабы постоянно не тянуть инфу с сайта (заготовк кэширования уже был готов), код сейчас представляет из себя следующее
Код:
$xmlfile="http://www.timezero.ru/res.xml";
//Считываем файл из кэша
$cache=@file("price.cache");
$fs=trim($cache[0]);
// если файл не обновлялся 10 минут 600сек
// обновляем его
if (intval($cache[0])<time()-600 || empty($fs)) {
$simple = implode(file($xmlfile));
$xml = xml_parser_create();
xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml,XML_OPTION_SKIP_WHITE,1);
$indexes = array();
$values = array();
xml_parse_into_struct($xml,$simple, $values, $indexes);
xml_parser_free($xml);
//print '<pre>';
//print_r( $values );
$mark="<".$values[0]['tag']." time=\"".$values[0]['attributes']['time']."\" shop=\"".$values[0]['attributes']['shop']."\" xy=\"".$values[0]['attributes']['xy']."\" city=\"".$values[0]['attributes']['city']."\">";
$cache=@file($xmlfile);
$fs=trim($cache[1]);
// сохраняем версию в кэш
if (!empty($fs)) {
$fw=fopen("price.cache","wt");
flock($fw, LOCK_EX);
// в первую строку пишем время
fputs($fw,time()."\n<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<Info>\n".$mark."\n");
// сохраняем остальные строки
for ($i=1;$i<count($cache);$i++) fputs($fw,trim($cache[$i])."\n");
fputs($fw,"\n</Info>");
fclose($fw);
}
// Если файл не был считан
// пользуемся кешем
else $cache=@file("price.cache");
}
//Считываем файл из кэша
$cache=@file("price.cache");
$fs=trim($cache[0]);
// если файл не обновлялся 10 минут 600сек
// обновляем его
if (intval($cache[0])<time()-600 || empty($fs)) {
$simple = implode(file($xmlfile));
$xml = xml_parser_create();
xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml,XML_OPTION_SKIP_WHITE,1);
$indexes = array();
$values = array();
xml_parse_into_struct($xml,$simple, $values, $indexes);
xml_parser_free($xml);
//print '<pre>';
//print_r( $values );
$mark="<".$values[0]['tag']." time=\"".$values[0]['attributes']['time']."\" shop=\"".$values[0]['attributes']['shop']."\" xy=\"".$values[0]['attributes']['xy']."\" city=\"".$values[0]['attributes']['city']."\">";
$cache=@file($xmlfile);
$fs=trim($cache[1]);
// сохраняем версию в кэш
if (!empty($fs)) {
$fw=fopen("price.cache","wt");
flock($fw, LOCK_EX);
// в первую строку пишем время
fputs($fw,time()."\n<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<Info>\n".$mark."\n");
// сохраняем остальные строки
for ($i=1;$i<count($cache);$i++) fputs($fw,trim($cache[$i])."\n");
fputs($fw,"\n</Info>");
fclose($fw);
}
// Если файл не был считан
// пользуемся кешем
else $cache=@file("price.cache");
}
файл кэша:
Код:
1161333453
<?xml version="1.0" encoding="UTF-8" ?>
<Info>
<S time="1161333002" shop="New Moscow Shop" xy="0/-1" city="New Moscow">
<R name="Metals" cost="0.35" need="73344" />
<R name="Gold" cost="1.2" need="84420" />
<R name="Polymers" cost="1.1" need="97149" />
<R name="Organic" cost="1.9" need="94231" />
<R name="Silicon" cost="0.6" need="79586" />
<R name="Radioactive" cost="1.2" need="80905" />
<R name="Gems" cost="0.3" need="95646" />
<R name="Venom" cost="0.6" need="99157" />
</S>
<S time="1161333002" shop="Berezka" xy="-1/-1" city="New Moscow" >
<R name="Metals" cost="0.32" need="86947" />
<R name="Gold" cost="1" need="1952" />
<R name="Polymers" cost="1.2" need="2147" />
<R name="Organic" cost="1.9" need="2121" />
<R name="Silicon" cost="0.52" need="53618" />
<R name="Radioactive" cost="1" need="1372" />
<R name="Gems" cost="0.3" need="8310" />
<R name="Venom" cost="1" need="3057" />
</S>
</Info>
<?xml version="1.0" encoding="UTF-8" ?>
<Info>
<S time="1161333002" shop="New Moscow Shop" xy="0/-1" city="New Moscow">
<R name="Metals" cost="0.35" need="73344" />
<R name="Gold" cost="1.2" need="84420" />
<R name="Polymers" cost="1.1" need="97149" />
<R name="Organic" cost="1.9" need="94231" />
<R name="Silicon" cost="0.6" need="79586" />
<R name="Radioactive" cost="1.2" need="80905" />
<R name="Gems" cost="0.3" need="95646" />
<R name="Venom" cost="0.6" need="99157" />
</S>
<S time="1161333002" shop="Berezka" xy="-1/-1" city="New Moscow" >
<R name="Metals" cost="0.32" need="86947" />
<R name="Gold" cost="1" need="1952" />
<R name="Polymers" cost="1.2" need="2147" />
<R name="Organic" cost="1.9" need="2121" />
<R name="Silicon" cost="0.52" need="53618" />
<R name="Radioactive" cost="1" need="1372" />
<R name="Gems" cost="0.3" need="8310" />
<R name="Venom" cost="1" need="3057" />
</S>
</Info>
Код:
прохожусь по кешу:
$simple = implode(file('price.cache'));
$xml = xml_parser_create();
xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml,XML_OPTION_SKIP_WHITE,1);
$indexes = array();
$values = array();
xml_parse_into_struct($xml,$simple, $values, $indexes);
xml_parser_free($xml);
print '<pre>';
print_r( $values );
$simple = implode(file('price.cache'));
$xml = xml_parser_create();
xml_parser_set_option($xml,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($xml,XML_OPTION_SKIP_WHITE,1);
$indexes = array();
$values = array();
xml_parse_into_struct($xml,$simple, $values, $indexes);
xml_parser_free($xml);
print '<pre>';
print_r( $values );
выдает пустой массив
Цитата:
Array
(
)
(
)
Что за число в начале XML-файла? Сделай ты НОРМАЛЬНЫЙ ПРАВИЛЬНЫЙ XML....
Ты уверен что у функции implode всего один аргумент? По моему два...
[QUOTE=shine]Ты уверен что у функции implode всего один аргумент? По моему два...[/QUOTE]
$vals = array();
$index = array();
$xml_parser = xml_parser_create();
//read file
if (!($fp = fopen($filename, "r"))) {
die("could not open XML input");
}
$data = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
Вот и все... используем значения массива $vals как угодно и где угодно.