我无法将我的大 xml 文件 (1,5g) 导入数据库。然后我用XMLReader->read()
我有错误元素有一个&符号。也许你可以帮我在哪里将无效的 XML 文件转换为有效的?
我在 Windows 7 上使用 tidy、xmlsoft、sed,但是这个命令行软件在限制内存错误时中断。
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);
}
}
如果此代码读取元素<number>111&111</number>
我有一个错误。我可以使用命令行工具删除这个 & 符号,但是我在非常大的 xml 文件上内存不足。
我的示例运行:
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
也许有其他方式使用带有跳过验证的 PHP XMLReader?