I can't import my big xml file (1,5g) into database. Then I use XMLReader->read()
i have error where element have a ampersand. maybe you can help me where I convert invalid XML file to valid?
I use tidy, xmlsoft, sed on Windows 7 but this command line software breaks on limit memory error.
PHP:
$reader = new XMLReader();
$reader->open('sm.xml');
while ($reader->read())
{
// check to ensure nodeType is an Element not attribute or #Text
if ($reader->nodeType == XMLReader::ELEMENT)
{
if ($reader->localName == 'brand')
{
$reader->read();
$data['brand'] = $reader->value;
}
if ($reader->localName == 'number')
{
$reader->read();
$data['number'] = $reader->value;
}
if ($reader->localName == 'descr')
{
$reader->read();
$data['descr'] = $reader->value;
}
if ($reader->localName == 'price')
{
$reader->read();
$data['price'] = $reader->value;
}
if ($reader->localName == 'deadline')
{
$reader->read();
$data['deadline'] = $reader->value;
}
if ($reader->localName == 'rest')
{
$reader->read();
$data['rest'] = $reader->value;
}
} //Checking if the </person>tag is reached.
elseif($reader->nodeType == XMLReader::END_ELEMENT AND $reader->name == 'article')
{
$sql = 'INSERT INTO tec (brand_name,brand_art,name_tov,cena,srok,kolvo)
VALUES ("'.$data['brand'].'","'.$data['number'].'","'.$data['descr'].'","'.$data['price'].'","'.$data['deadline'].'","'.$data['rest'].'");';
$mysqli->query($sql);
// Insert the content of array $data to database or some other action.
//print_r($data);
}
}
If this code read element <number>111&111</number>
I have an error. I can remove this ampersand using a command line tool, but I have out of memory on very big xml file.
My example run:
xmllint.exe --recover --maxmem 10000000000 --noout --encode utf8 sm.xml -o smtt.xml
tidy.exe -m -utf8 -xml sm.xml
sed.exe 's/&/\&/g; s/&amp;/\&/g; s/&quot;/\"/g;' sm.xml > smtt.xml <-- can't run
Maybe have other way use PHP XMLReader with skip validation?