我正在使用xmlReader来解析一个大的xml文件(~100mb)。像这样的东西
<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
<product>
<element_1>foo</element_1>
<element_2>foo</element_2>
<element_3>foo</element_3>
<element_4>foo</element_4>
</product>
<product>
<element_1>bar</element_1>
<element_2>bar</element_2>
<element_3>bar</element_3>
<element_4>bar</element_4>
</product>
<product>
<element_1>bar</element_1>
<element_2>bar</element_2>
<element_3>bar</element_3>
<element_4>bar</element_4>
</product>
<product>
<element_1>bar</element_1>
<element_2>bar</element_2>
<element_3>bar</element_3>
<element_4>bar</element_4>
</product>
<product>
<element_1>bar</element_1>
<element_2>bar</element_2>
<element_3>bar</element_3>
<element_4>bar</element_4>
</product>
</products>
我们如何跳过前2棵树并转移到第3棵树?
答案 0 :(得分:0)
XMLReader逐行遍历您的文件。由于xml文件中没有任何标识符(属性),因此只能通过使用“next()”跳过产品1和2来使用修复解决方案:
$xmlReader = new XMLReader;
// try to open
if (!$xmlReader->open($file)) {
die("Failed to open file");
}
// read the file
while($xmlReader->read()) {
// skip 1st product
if($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name == "product") {$xmlReader->next("product");}
// skip 2nd product
if($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name == "product") {$xmlReader->next("product");}
// start reading
if($xmlReader->nodeType == XMLReader::ELEMENT && $xmlReader->name == "product") {
// use your code here
// personally I like using simplexml from here since its easier to work with and the nodes are usually small enough
$node = simplexml_load_string($xmlReader->readOuterXML());
// use it
$myNewElement1 = $node->element_1;
$myNewElement2 = $node->element_2;
$myNewElement3 = $node->element_3;
$myNewElement4 = $node->element_4;
}
}