English 中文(简体)
如何使用 PHP 用多 Xml 声明来分析 xml 文件? (多个 XML 文件的组合)
原标题:How to parse an xml file with multiple xml declaration using PHP? (A concatenation of several XML files)
  • 时间:2012-05-28 06:57:48
  •  标签:
  • php
  • xml

xml 格式 :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE >
<root>
 <node>
  <element1></element1>
  <element2></element2>
  <element3></element2>
  <element4></element3>  
</node>
</root>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE >
<root>
 <node>
  <element1></element1>
  <element2></element2>
  <element3></element2>
  <element4></element3>  
</node>
</root>

and several more xml declarations after. BTW, the file size 500MB. I would like to ask for help how to parse this file without breaking it up into different files using PHP.

Any help would be appreciated. Thank you..

问题回答

如果您不想拆分文件, 您必须在内存中工作 。 鉴于您的 500MB 文件大小, 这可能会产生问题 。 总之, 一个选项是从所有文档中删除 XML Prolog 和 DocType, 然后将整个文件装入 :

$dom = new DOMDocument;
$dom->loadXML(
    sprintf(
         <?xml version="1.0" encoding="UTF-8"?>%s  .
         <!DOCTYPE >%s  . 
         <roots>%s</roots> ,
        PHP_EOL, 
        PHP_EOL, 
        str_replace(
            array(
                 <?xml version="1.0" encoding="UTF-8"?> , 
                 <!DOCTYPE > 
            ),
              ,
            file_get_contents( /path/to/your/file.xml )
        )
    )
);

这将使它变成一个巨大的 XML 文件, 只有一个 XML Prolog 和一个 DocType (注I 假设文档中所有文档的 DocType 相同) 。 然后您可以通过对单个根元素进行循环处理来处理文件 。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签