使用php从具有多个命名元素的文件中删除xml元素

时间:2011-03-19 09:30:39

标签: php xml parsing

喜 我正在使用xml文件,该文件的结构就像

<ListRecords>
    <record>
</record>

<totalupnow> </totalupnow>

<record>
</record>
<record>
</record>
<record>
</record>
<record>
</record>
<totalupnow> </totalupnow>

</listrecord>

现在我需要一个php程序,只是从这个文件中删除<totalupnow> </totalupnow> ..文件的大小几乎是4 gbs。

请帮帮我......

或者无论如何,我只能从<record> </record>开始阅读<totalupnow> </totalupnow>

1 个答案:

答案 0 :(得分:0)

您可以使用event-based streaming XML parser (SAX)来解析此类文件。它的工作方式与DOM解析器略有不同,但作为交换,它可以处理任何大小的文件。

  

[...]或者无论如何我只能阅读<record> </record>

为了简单起见,我假设你的<record>元素只包含文本而“read”意味着“将其内容写入屏幕”。

<?php 
$file       = "your_big_file.xml"; 
$xml_parser = xml_parser_create(); 

// set up some basic parser properties
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0); 
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1); 

// set up your event handlers    
xml_set_element_handler($xml_parser, "startElement", "endElement"); 
xml_set_character_data_handler($xml_parser, "contents"); 

// read the file in 4kb chunks and parse these as they are read
while ($data = fread($fp, 4096)) { 
  if (!xml_parse($xml_parser, $data, feof($fp))) { 
    die( sprintf("XML error: %s at line %d", 
         xml_error_string(xml_get_error_code($xml_parser)), 
         xml_get_current_line_number($xml_parser))); 
  } 
} 

// clean up
xml_parser_free($xml_parser); 

// EVENT HANDLERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$in_record = false;
$counter   = 0;
$depth     = 0;

// this function is called whenever a start element (<foo>) is encountered
function startElement($parser, $name, $attrs) { 
  global $in_record; 
  global $depth; 
  global $counter;

  $depth++; 
  $in_record = ($name == "record");

  if ($in_record) {
    $counter++;
    echo "Record #$counter:\n"; 
  }
} 

// this function is called whenever a start element (</foo>) is encountered
function endElement($parser, $name) { 
  global $in_record; 
  global $depth; 

  $depth--; 
  $in_record = ($name != "record");
} 

// this function is called whenever text data is encountered
function contents($parser, $data) { 
  global $in_record; 

  if ($in_record) {
    echo "\t".$data."\n"; 
  }
} 

?>