简体   繁体   English

php domdocument无法正确解析

[英]php domdocument not parsing correctly

This code is producing inefficent results when I use it to parse a large XML file. 当我使用它解析大型XML文件时,此代码将产生无效的结果。

The XML that is parsed looks like this: 解析的XML如下所示:

 <product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>
<categoryPath><category><name>Buy</name></category>
<category><name>Car, Marine &amp; GPS</name></category>
<category><name>Car Audio</name></category>
<category><name>Car Stereos</name></category>
<category><name>CD Decks</name></category></categoryPath>
</product>

There is about 100 sets of product (so basically the above xml times 100) 大约有100套产品(因此上述xml乘以100)

This code works when there is only about 3-5 sets product, but not when it increases in size. 当只有大约3-5套产品时,此代码有效,但在尺寸增加时无效。 Why doesn't it work for bigger files? 为什么对更大的文件不起作用?

  <?php

set_time_limit(0);
   // load up your XML
$xml = new DOMDocument;
$xml->load('file.xml');    

// Array to store them
$append = array();
foreach ($xml->getElementsByTagName('product') as $product ) 
{
    foreach($product->getElementsByTagName('name') as $name ) {
        // Stick $name onto the array

        $append[] = $name;

}
// Now append all of them to product
        foreach ($append as $a) 
{
    $product->appendChild($a);
}
    $product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}


    // final result:
    $result = $xml->saveXML();
    echo $result;
    $file =     "new_file.xml";
    file_put_contents($file,$result);
?>

After this code is executed, the XML file is supposed to look like this for each product set 执行此代码后,每个产品集的XML文件应该看起来像这样

<?xml version='1.0'?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name></category>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>
</product>
</products>

However, when I use this PHP code to parse a rather large xml file (one with 100 sets of product), it takes the categoryPath node and its children and appends them to the bottom of the file disregarding the node it is supposed to be in (product) If I only parse a small XML file (one that has 3 sets of product), then I will get the desired result (the above XML code is was this PHP code is supposed to do, but it doesnt work when there is a large file). 但是,当我使用此PHP代码解析一个相当大的xml文件(一个包含100套产品)时,它将采用categoryPath节点及其子代,并将它们附加到文件的底部,而不考虑它应该位于的节点。 (产品)如果我仅解析一个小的XML文件(一个具有3套产品的文件),那么我将获得所需的结果(上面的XML代码是该PHP代码应做的,但是当有大文件)。

When I try to parse an XML file that has 100 product sets the result looks like this: 当我尝试解析具有100个产品集的XML文件时,结果如下所示:

<?xml version='1.0'?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
</product>
</products>
<name>Buy</name></category>
<name>Car, Marine &amp; GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas &amp; Adapters</name>

Each of the name nodes are not appended inside the product node. 每个名称节点均未附加在产品节点内。

Does this work? 这样行吗?

// load up your XML
$xml = new DOMDocument;
$xml->loadXml('

<products>
  <product>
    <ItemId>1576829</ItemId>
    <modelNumber>CX501</modelNumber>
    <categoryPath><category><name>Buy</name></category>
    <category><name>Car, Marine &amp; GPS</name></category>
    <category><name>Car Audio</name></category>
    <category><name>Car Stereos</name></category>
    <category><name>CD Decks</name></category></categoryPath>
  </product>

 <product>
    <ItemId>1576829</ItemId>
    <modelNumber>CX501</modelNumber>
    <categoryPath><category><name>Buy</name></category>
    <category><name>Car, Marine &amp; GPS</name></category>
    <category><name>Car Audio</name></category>
    <category><name>Car Stereos</name></category>
    <category><name>CD Decks</name></category></categoryPath>
  </product>
</products>

');    

// Array to store them


foreach ($xml->getElementsByTagName('product') as $product ) 
{
     $append = array();

    foreach($product->getElementsByTagName('name') as $name ) {
    // Stick $name onto the array
    $append[] = $name;
}

    foreach ($append as $a)  {
               // Now append all of them to product
           $product->appendChild($a);
    }
    $product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}

// final result:
$result = $xml->saveXML();
echo '<pre>'.print_r(htmlspecialchars($result),1).'</pre>';

Tested on c. 在c上测试。 100 <product> tags with the following result: 100个<product>标签具有以下结果:

<?xml version="1.0"?>
<products>
  <product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>

<name>Buy</name>
    <name>Car, Marine &amp; GPS</name>
<name>Car Audio</name>
<name>Car Stereos</name>
<name>CD Decks</name>
  </product>

<product>
  <ItemId>1576829</ItemId>
    <modelNumber>CX501</modelNumber>

  <name>Buy</name>
  <name>Car, Marine &amp; GPS</name>
  <name>Car Audio</name>
  <name>Car Stereos</name>
  <name>CD Decks</name>
    </product>

    // etc
</products>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM