I
I
Ivan2019-01-02 22:52:21
PHP
Ivan, 2019-01-02 22:52:21

How to move to the next object in xml after reading the first one?

I get data from an XML file and save it to the database.

$read = simplexml_load_file($file); // получаем объект класса
$xml = $read->Category->Site; // $xml - объект-массив, вложенные теги - его свойства
$count = count($xml); // кол-во элементов массива

for($i = 0; $i < $count; $i++){
... код ...

$values .= "('$id_xml', '$date_xml','$title_xml','$link_xml'),";
/* заносим данные в БД если накопилось 1000 записей при подготовке запроса*/
if($i % 1000 == 0)
{
$values[strlen($values)-1]=' ';
$res = mysql_query("$Query $values $ODKU");
$values = "";
}

}
/* СОХРАНЯЮ В БАЗУ ПОЛУЧЕННЫЙ РЕЗУЛЬТАТ */
if(strlen($values)>0)
{
$values[strlen($values)-1]=' ';
/* заносим данные в БД */
$res = mysql_query("$Query $values $ODKU");    
}

echo 'Done';

The XML file looks like this
<?xml version="1.0" encoding="UTF-8"?>
<Catalog>
<Category>
<Name>Категория 1</Name>
<Site>...</Site>
<Site>...</Site>
<Site>...</Site>
</Category>
<Category>
<Name>Категория 2</Name>
<Site>...</Site>
<Site>...</Site>
<Site>...</Site>
</Category>
<Category>
<Name>Категория 3</Name>
<Site>...</Site>
<Site>...</Site>
<Site>...</Site>
</Category>
</Catalog>

Everything works for me until the first category ends, the script does not go to the second category $xml = $read->Category->Site;
.
Help me please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-01-02
@9StarRu

iterate over it
foreach($read->Category as $cat) {
$site = $cat->Site[0]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question