如何将 50MB 的 zip 文件和 600MB xml 文件放入 mysql 数据表中?
How do I get a 50MB zip file with a 600MB xml file into a mysql datatable?
如何将 50MB 的 zip 文件和 600MB xml 文件(超过 300,000 个“<"abc:ABCRecord">”)放入 mysql 数据table? xml 文件本身具有以下结构:
<?xml version='1.0' encoding='UTF-8'?>
<abc:ABCData xmlns:abc="http://www.abc-example.com" xmlns:xyz="http:/www.xyz-example.com">
<abc:ABCHeader>
<abc:ContentDate>2015-08-15T09:03:29.379055+00:00</abc:ContentDate>
<abc:FileContent>PUBLISHED</abc:FileContent>
<abc:RecordCount>310598</abc:RecordCount>
<abc:Extension>
<xyz:Sources>
<xyz:Source>
<xyz:ABC>5967007LIEEXZX4LPK21</xyz:ABC>
<xyz:Name>Bornheim Register Centre</xyz:Name>
<xyz:ROCSponsorCountry>NO</xyz:ROCSponsorCountry>
<xyz:RecordCount>398</xyz:RecordCount>
<xyz:ContentDate>2015-08-15T05:00:02.952+02:00</xyz:ContentDate>
<xyz:LastAttemptedDownloadDate>2015-08-15T09:00:01.885686+00:00</xyz:LastAttemptedDownloadDate>
<xyz:LastSuccessfulDownloadDate>2015-08-15T09:00:02.555222+00:00</xyz:LastSuccessfulDownloadDate>
<xyz:LastValidDownloadDate>2015-08-15T09:00:02.555222+00:00</xyz:LastValidDownloadDate>
</xyz:Source>
</xyz:Sources>
</abc:Extension>
</abc:ABCHeader>
<abc:ABCRecords>
<abc:ABCRecord>
<abc:ABC>5967007LIEEXZX4LPK21</abc:ABC>
<abc:Entity>
<abc:LegalName>REGISTERENHETEN I Bornheim</abc:LegalName>
<abc:LegalAddress>
<abc:Line1>Havnegata 48</abc:Line1>
<abc:City>Bornheim</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>8900</abc:PostalCode>
</abc:LegalAddress>
<abc:HeadquartersAddress>
<abc:Line1>Havnegata 48</abc:Line1>
<abc:City>Bornheim</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>8900</abc:PostalCode>
</abc:HeadquartersAddress>
<abc:BusinessRegisterEntityID register="Enhetsregisteret">974757873</abc:BusinessRegisterEntityID>
<abc:LegalForm>Organisasjonsledd</abc:LegalForm>
<abc:EntityStatus>Active</abc:EntityStatus>
</abc:Entity>
<abc:Registration>
<abc:InitialRegistrationDate>2014-06-15T12:03:33.000+02:00</abc:InitialRegistrationDate>
<abc:LastUpdateDate>2015-06-15T20:45:32.000+02:00</abc:LastUpdateDate>
<abc:RegistrationStatus>ISSUED</abc:RegistrationStatus>
<abc:NextRenewalDate>2016-06-15T12:03:33.000+02:00</abc:NextRenewalDate>
<abc:ManagingLOU>59670054IEEXZX44PK21</abc:ManagingLOU>
</abc:Registration>
</abc:ABCRecord>
<abc:ABCRecord>
<abc:ABC>5967007LIE45ZX4MHC90</abc:ABC>
<abc:Entity>
<abc:LegalName>SUNNDAL HOSTBANK</abc:LegalName>
<abc:LegalAddress>
<abc:Line1>Sunfsalsvegen 15</abc:Line1>
<abc:City>SUNNDALSPRA</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>6600</abc:PostalCode>
</abc:LegalAddress>
<abc:HeadquartersAddress>
<abc:Line1>Sunndalsvegen 15</abc:Line1>
<abc:City>SUNNDALSPRA</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>6600</abc:PostalCode>
</abc:HeadquartersAddress>
<abc:BusinessRegisterEntityID register="Foretaksregisteret">9373245963</abc:BusinessRegisterEntityID>
<abc:LegalForm>Hostbank</abc:LegalForm>
<abc:EntityStatus>Active</abc:EntityStatus>
</abc:Entity>
<abc:Registration>
<abc:InitialRegistrationDate>2014-06-26T15:01:02.000+02:00</abc:InitialRegistrationDate>
<abc:LastUpdateDate>2015-06-27T15:02:39.000+02:00</abc:LastUpdateDate>
<abc:RegistrationStatus>ISSUED</abc:RegistrationStatus>
<abc:NextRenewalDate>2016-06-26T15:01:02.000+02:00</abc:NextRenewalDate>
<abc:ManagingLOU>5967007LIEEXZX4LPK21</abc:ManagingLOU>
</abc:Registration>
</abc:ABCRecord>
</abc:ABCRecords>
</abc:ABCData>
mysql table 应该是什么样子,我该如何实现?目标是让所有abc标记的内容都在table中。此外,每天都会通过下载 link 提供一个新的 zip 文件,它应该每天更新 table。 zip 文件以以下结构命名:“20150815-XYZ-concatenated-file.zip”。一步一步的提示会很棒吗?我试过这个: 截至目前,但它还没有完成工作!
根据下面的 ThW 解释,我现在完成了以下操作:
<?php
// open input
$reader = new XMLReader();
$reader->open('./xmlreader.xml');
// open output
$output = fopen('./xmlreader.csv', 'w');
fputcsv($output, ['id', 'name']);
$xmlns = [
'a' => 'http://www.abc-example.com'
];
// prepare DOM
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
// look for the first record element
while (
$reader->read() &&
(
$reader->localName !== 'ABCRecord' ||
$reader->namespaceURI !== $xmlns['a']
)
) {
continue;
}
// while you have an record element
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
// expand record element node
$node = $reader->expand($dom);
// fetch data and write it to output
fputcsv(
$output,
[
$xpath->evaluate('string(a:ABC)', $node),
$xpath->evaluate('string(a:Entity/a:LegalName)', $node)
]
);
}
// move to the next record sibling
$reader->next('ABCRecord');
}
这是正确的吗?!我在哪里可以找到输出?!以及如何获得 mysql 中的输出。抱歉我的菜鸟问题,这是我第一次这样做......
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "password";
$dbName = "new_xml_extract";
$dbConn = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
$delete = $dbConn->query("TRUNCATE TABLE `test_xml`");
....
$sql = "INSERT INTO `test_xml` (`.....`, `.....`)" . "VALUES ('". $dbConn->real_escape_string($.....) ."', '".$dbConn->real_escape_string($.....)."')";
$result = $dbConn->query($sql);
}
MySQL 不知道您的 XML 结构。虽然它可以直接导入简单、格式良好的 XML 结构,但您需要自己转换更复杂的结构。您可以生成 CSV、SQL 或(支持的)XML.
对于这样的大文件 XMLReader 是最好的 API。首先创建一个实例并打开文件:
$reader = new XMLReader();
$reader->open('php://stdin');
您正在使用命名空间,所以我建议为它们定义一个映射数组:
$xmlns = [
'a' => 'http://www.abc-example.com'
];
可以使用与 XML 文件中相同的 prefixes/aliases,但您也可以使用自己的文件。
接下来遍历XML个节点,直到找到第一个记录元素节点:
while (
$reader->read() &&
($reader->localName !== 'ABCRecord' || $reader->namespaceURI !== $xmlns['a'])
) {
continue;
}
您需要比较本地名称(没有命名空间前缀的标签名称)和命名空间URI。这样你的程序不依赖于 XML 文件中的实际前缀。
找到第一个节点后,可以遍历到下一个具有相同本地名称的兄弟节点。
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
// read data for the record ...
}
// move to the next record sibling
$reader->next('ABCRecord');
}
您可以使用 XMLReader 读取记录数据,但使用 DOM 和 XPath 表达式更容易。 XML读者可以将当前节点扩展为DOM个节点。因此,准备一个 DOM 文档,为其创建一个 XPath 对象并注册名称空间。扩展节点会将节点和所有后代加载到内存中,但不会加载父节点或兄弟节点。
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
$node = $reader->expand($dom);
var_dump(
$xpath->evaluate('string(a:ABC)', $node),
$xpath->evaluate('string(a:Entity/a:LegalName)', $node)
);
}
$reader->next('ABCRecord');
}
DOMXPath::evaluate()
允许您使用 Xpath 表达式从 DOM 中获取标量值或节点列表。
fputcsv()
将数据写入 CSV 文件真的很容易吗?
合计:
// open input
$reader = new XMLReader();
$reader->open('php://stdin');
// open output
$output = fopen('php://stdout', 'w');
fputcsv($output, ['id', 'name']);
$xmlns = [
'a' => 'http://www.abc-example.com'
];
// prepare DOM
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
// look for the first record element
while (
$reader->read() &&
(
$reader->localName !== 'ABCRecord' ||
$reader->namespaceURI !== $xmlns['a']
)
) {
continue;
}
// while you have an record element
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
// expand record element node
$node = $reader->expand($dom);
// fetch data and write it to output
fputcsv(
$output,
[
$xpath->evaluate('string(a:ABC)', $node),
$xpath->evaluate('string(a:Entity/a:LegalName)', $node)
]
);
}
// move to the next record sibling
$reader->next('ABCRecord');
}
输出:
id,name
5967007LIEEXZX4LPK21,"REGISTERENHETEN I Bornheim"
5967007LIE45ZX4MHC90,"SUNNDAL HOSTBANK"
如何将 50MB 的 zip 文件和 600MB xml 文件(超过 300,000 个“<"abc:ABCRecord">”)放入 mysql 数据table? xml 文件本身具有以下结构:
<?xml version='1.0' encoding='UTF-8'?>
<abc:ABCData xmlns:abc="http://www.abc-example.com" xmlns:xyz="http:/www.xyz-example.com">
<abc:ABCHeader>
<abc:ContentDate>2015-08-15T09:03:29.379055+00:00</abc:ContentDate>
<abc:FileContent>PUBLISHED</abc:FileContent>
<abc:RecordCount>310598</abc:RecordCount>
<abc:Extension>
<xyz:Sources>
<xyz:Source>
<xyz:ABC>5967007LIEEXZX4LPK21</xyz:ABC>
<xyz:Name>Bornheim Register Centre</xyz:Name>
<xyz:ROCSponsorCountry>NO</xyz:ROCSponsorCountry>
<xyz:RecordCount>398</xyz:RecordCount>
<xyz:ContentDate>2015-08-15T05:00:02.952+02:00</xyz:ContentDate>
<xyz:LastAttemptedDownloadDate>2015-08-15T09:00:01.885686+00:00</xyz:LastAttemptedDownloadDate>
<xyz:LastSuccessfulDownloadDate>2015-08-15T09:00:02.555222+00:00</xyz:LastSuccessfulDownloadDate>
<xyz:LastValidDownloadDate>2015-08-15T09:00:02.555222+00:00</xyz:LastValidDownloadDate>
</xyz:Source>
</xyz:Sources>
</abc:Extension>
</abc:ABCHeader>
<abc:ABCRecords>
<abc:ABCRecord>
<abc:ABC>5967007LIEEXZX4LPK21</abc:ABC>
<abc:Entity>
<abc:LegalName>REGISTERENHETEN I Bornheim</abc:LegalName>
<abc:LegalAddress>
<abc:Line1>Havnegata 48</abc:Line1>
<abc:City>Bornheim</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>8900</abc:PostalCode>
</abc:LegalAddress>
<abc:HeadquartersAddress>
<abc:Line1>Havnegata 48</abc:Line1>
<abc:City>Bornheim</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>8900</abc:PostalCode>
</abc:HeadquartersAddress>
<abc:BusinessRegisterEntityID register="Enhetsregisteret">974757873</abc:BusinessRegisterEntityID>
<abc:LegalForm>Organisasjonsledd</abc:LegalForm>
<abc:EntityStatus>Active</abc:EntityStatus>
</abc:Entity>
<abc:Registration>
<abc:InitialRegistrationDate>2014-06-15T12:03:33.000+02:00</abc:InitialRegistrationDate>
<abc:LastUpdateDate>2015-06-15T20:45:32.000+02:00</abc:LastUpdateDate>
<abc:RegistrationStatus>ISSUED</abc:RegistrationStatus>
<abc:NextRenewalDate>2016-06-15T12:03:33.000+02:00</abc:NextRenewalDate>
<abc:ManagingLOU>59670054IEEXZX44PK21</abc:ManagingLOU>
</abc:Registration>
</abc:ABCRecord>
<abc:ABCRecord>
<abc:ABC>5967007LIE45ZX4MHC90</abc:ABC>
<abc:Entity>
<abc:LegalName>SUNNDAL HOSTBANK</abc:LegalName>
<abc:LegalAddress>
<abc:Line1>Sunfsalsvegen 15</abc:Line1>
<abc:City>SUNNDALSPRA</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>6600</abc:PostalCode>
</abc:LegalAddress>
<abc:HeadquartersAddress>
<abc:Line1>Sunndalsvegen 15</abc:Line1>
<abc:City>SUNNDALSPRA</abc:City>
<abc:Country>NO</abc:Country>
<abc:PostalCode>6600</abc:PostalCode>
</abc:HeadquartersAddress>
<abc:BusinessRegisterEntityID register="Foretaksregisteret">9373245963</abc:BusinessRegisterEntityID>
<abc:LegalForm>Hostbank</abc:LegalForm>
<abc:EntityStatus>Active</abc:EntityStatus>
</abc:Entity>
<abc:Registration>
<abc:InitialRegistrationDate>2014-06-26T15:01:02.000+02:00</abc:InitialRegistrationDate>
<abc:LastUpdateDate>2015-06-27T15:02:39.000+02:00</abc:LastUpdateDate>
<abc:RegistrationStatus>ISSUED</abc:RegistrationStatus>
<abc:NextRenewalDate>2016-06-26T15:01:02.000+02:00</abc:NextRenewalDate>
<abc:ManagingLOU>5967007LIEEXZX4LPK21</abc:ManagingLOU>
</abc:Registration>
</abc:ABCRecord>
</abc:ABCRecords>
</abc:ABCData>
mysql table 应该是什么样子,我该如何实现?目标是让所有abc标记的内容都在table中。此外,每天都会通过下载 link 提供一个新的 zip 文件,它应该每天更新 table。 zip 文件以以下结构命名:“20150815-XYZ-concatenated-file.zip”。一步一步的提示会很棒吗?我试过这个:
根据下面的 ThW 解释,我现在完成了以下操作:
<?php
// open input
$reader = new XMLReader();
$reader->open('./xmlreader.xml');
// open output
$output = fopen('./xmlreader.csv', 'w');
fputcsv($output, ['id', 'name']);
$xmlns = [
'a' => 'http://www.abc-example.com'
];
// prepare DOM
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
// look for the first record element
while (
$reader->read() &&
(
$reader->localName !== 'ABCRecord' ||
$reader->namespaceURI !== $xmlns['a']
)
) {
continue;
}
// while you have an record element
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
// expand record element node
$node = $reader->expand($dom);
// fetch data and write it to output
fputcsv(
$output,
[
$xpath->evaluate('string(a:ABC)', $node),
$xpath->evaluate('string(a:Entity/a:LegalName)', $node)
]
);
}
// move to the next record sibling
$reader->next('ABCRecord');
}
这是正确的吗?!我在哪里可以找到输出?!以及如何获得 mysql 中的输出。抱歉我的菜鸟问题,这是我第一次这样做......
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "password";
$dbName = "new_xml_extract";
$dbConn = mysqli_connect($dbHost, $dbUser, $dbPass, $dbName);
$delete = $dbConn->query("TRUNCATE TABLE `test_xml`");
....
$sql = "INSERT INTO `test_xml` (`.....`, `.....`)" . "VALUES ('". $dbConn->real_escape_string($.....) ."', '".$dbConn->real_escape_string($.....)."')";
$result = $dbConn->query($sql);
}
MySQL 不知道您的 XML 结构。虽然它可以直接导入简单、格式良好的 XML 结构,但您需要自己转换更复杂的结构。您可以生成 CSV、SQL 或(支持的)XML.
对于这样的大文件 XMLReader 是最好的 API。首先创建一个实例并打开文件:
$reader = new XMLReader();
$reader->open('php://stdin');
您正在使用命名空间,所以我建议为它们定义一个映射数组:
$xmlns = [
'a' => 'http://www.abc-example.com'
];
可以使用与 XML 文件中相同的 prefixes/aliases,但您也可以使用自己的文件。
接下来遍历XML个节点,直到找到第一个记录元素节点:
while (
$reader->read() &&
($reader->localName !== 'ABCRecord' || $reader->namespaceURI !== $xmlns['a'])
) {
continue;
}
您需要比较本地名称(没有命名空间前缀的标签名称)和命名空间URI。这样你的程序不依赖于 XML 文件中的实际前缀。
找到第一个节点后,可以遍历到下一个具有相同本地名称的兄弟节点。
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
// read data for the record ...
}
// move to the next record sibling
$reader->next('ABCRecord');
}
您可以使用 XMLReader 读取记录数据,但使用 DOM 和 XPath 表达式更容易。 XML读者可以将当前节点扩展为DOM个节点。因此,准备一个 DOM 文档,为其创建一个 XPath 对象并注册名称空间。扩展节点会将节点和所有后代加载到内存中,但不会加载父节点或兄弟节点。
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
$node = $reader->expand($dom);
var_dump(
$xpath->evaluate('string(a:ABC)', $node),
$xpath->evaluate('string(a:Entity/a:LegalName)', $node)
);
}
$reader->next('ABCRecord');
}
DOMXPath::evaluate()
允许您使用 Xpath 表达式从 DOM 中获取标量值或节点列表。
fputcsv()
将数据写入 CSV 文件真的很容易吗?
合计:
// open input
$reader = new XMLReader();
$reader->open('php://stdin');
// open output
$output = fopen('php://stdout', 'w');
fputcsv($output, ['id', 'name']);
$xmlns = [
'a' => 'http://www.abc-example.com'
];
// prepare DOM
$dom = new DOMDocument;
$xpath = new DOMXpath($dom);
foreach ($xmlns as $prefix => $namespaceURI) {
$xpath->registerNamespace($prefix, $namespaceURI);
}
// look for the first record element
while (
$reader->read() &&
(
$reader->localName !== 'ABCRecord' ||
$reader->namespaceURI !== $xmlns['a']
)
) {
continue;
}
// while you have an record element
while ($reader->localName === 'ABCRecord') {
if ($reader->namespaceURI === 'http://www.abc-example.com') {
// expand record element node
$node = $reader->expand($dom);
// fetch data and write it to output
fputcsv(
$output,
[
$xpath->evaluate('string(a:ABC)', $node),
$xpath->evaluate('string(a:Entity/a:LegalName)', $node)
]
);
}
// move to the next record sibling
$reader->next('ABCRecord');
}
输出:
id,name
5967007LIEEXZX4LPK21,"REGISTERENHETEN I Bornheim"
5967007LIE45ZX4MHC90,"SUNNDAL HOSTBANK"