Парсер xml файлов на php
Пользователи, просматривающие топик: none
|
Зашли как: Guest
|
Имя |
Сообщение |
<< Старые топики Новые топики >> |
|
|
Парсер xml файлов на php - 2009-11-17 17:58:30.023333
|
|
|
n1k.Sly
Сообщений: 2
Оценки: 0
Присоединился: 2009-11-17 17:10:53.110000
|
Пытаюсь найти примитивный XML парсер….. Желательно в один скрипт… Собственно , что мне нужно от него: Скажем в папку 2parse я закидываю xml файлы. запуская скрипт и в нем кнопка Parse. Кликаю и он автоматически из загруженых файлов выцепляет определеные поля и пишет их в базу MySQL. По сети поискал кучу парсеров, но что-то не могу понять… Нашел такую вот манку: http://www.internet-technologies.ru/articles/article_1427.html Но в ней нет ни БД ни тех возможностей, что мне нужно…
|
|
|
RE: Парсер xml файлов на php - 2009-11-17 20:40:29.840000
|
|
|
oRb
Сообщений: 4044
Оценки: 597
Присоединился: 2007-03-28 18:45:06.630000
|
http://ru2.php.net/domdocument
|
|
|
RE: Парсер xml файлов на php - 2009-11-17 23:52:19.363333
|
|
|
Agent Smith
Сообщений: 976
Оценки: 0
Присоединился: 2007-04-10 21:56:49.593333
|
<?php
CLASS Xml{
function start($data, $get_attributes = 1, $priority = 'tag')
{
$contents = "";
/*if (!function_exists('xml_parser_create'))
{
return array ();
}*/
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($data), $xml_values);
xml_parser_free($parser);
if (!$xml_values)
return; //Hmm...
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data)
{
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag')
$result = $value;
else
$result['value'] = $value;
}
if (isset ($attributes) and $get_attributes)
{
foreach ($attributes as $attr => $val)
{
if ($priority == 'tag')
$attributes_data[$attr] = $val;
else
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
if ($type == "open")
{
$parent[$level -1] = & $current;
if (!is_array($current) or (!in_array($tag, array_keys($current))))
{
$current[$tag] = $result;
if ($attributes_data)
$current[$tag . '_attr'] = $attributes_data;
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
}
else
{
if (isset ($current[$tag][0]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
}
elseif ($type == "complete")
{
if (!isset ($current[$tag]))
{
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data)
$current[$tag . '_attr'] = $attributes_data;
}
else
{
if (isset ($current[$tag][0]) and is_array($current[$tag]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes)
{
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
}
elseif ($type == 'close')
{
$current = & $parent[$level -1];
}
}
return ($xml_array);
}
}
?>
|
|
|
RE: Парсер xml файлов на php - 2009-11-20 20:45:10.783333
|
|
|
n1k.Sly
Сообщений: 2
Оценки: 0
Присоединился: 2009-11-17 17:10:53.110000
|
Спасибо ребята!
|
|
|
RE: Парсер xml файлов на php - 2009-11-30 19:02:49.726666
|
|
|
lit87
Сообщений: 13
Оценки: 0
Присоединился: 2009-11-23 20:15:36.443333
|
function xml2array ($xml){
$xmlary = array ();
if ((strlen ($xml) < 256) && is_file ($xml))
$xml = file_get_contents ($xml);
$ReElements = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*?)<\/\s*\\1\s*>)/s';
#$ReElements = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
$ReAttributes = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
preg_match_all ($ReElements, $xml, $elements);
foreach ($elements[1] as $ie => $xx) {
$xmlary[$ie]["name"] = $elements[1][$ie];
if ( $attributes = trim($elements[2][$ie])) {
preg_match_all ($ReAttributes, $attributes, $att);
foreach ($att[1] as $ia => $xx)
// all the attributes for current element are added here
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
} // if $attributes
// get text if it's combined with sub elements
$cdend = strpos($elements[3][$ie],"<");
if ($cdend > 0) {
$xmlary[$ie]["text"] = substr($elements[3][$ie],0,$cdend -1);
} // if cdend
if (preg_match ($ReElements, $elements[3][$ie]))
$xmlary[$ie]["elements"] = $this->xml2array ($elements[3][$ie]);
else if ($elements[3][$ie]){
$xmlary[$ie]["text"] = $elements[3][$ie];
}
}
return $xmlary;
}
|
|
|
RE: Парсер xml файлов на php - 2009-11-30 22:00:25.210000
|
|
|
oRb
Сообщений: 4044
Оценки: 597
Присоединился: 2007-03-28 18:45:06.630000
|
it87, зачем XML разбирать регулярками?
|
|
|
RE: Парсер xml файлов на php - 2009-12-01 17:10:58.966666
|
|
|
Parano1d
Сообщений: 423
Оценки: 0
Присоединился: 2008-05-21 13:40:17.093333
|
+1 SimpleXML можете использовать
|
|
|
|
|