Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 解析器错误:仅允许在文档开头使用XML声明_Php_Xml - Fatal编程技术网

Php 解析器错误:仅允许在文档开头使用XML声明

Php 解析器错误:仅允许在文档开头使用XML声明,php,xml,Php,Xml,我有一个包含多个声明的xml文件,如下所示 <?xml version="1.0" encoding="UTF-8"?> <root> <node> <element1>Stefan</element1> <element2>42</element2> <element3>Shirt</element3> <element4>3000</element4

我有一个包含多个声明的xml文件,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <node>
  <element1>Stefan</element1>
  <element2>42</element2>
  <element3>Shirt</element3>
  <element4>3000</element4>  
</node>
</root>

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <node>
  <element1>Damon</element1>
  <element2>32</element2>
  <element3>Jeans</element3>
  <element4>4000</element4>  
</node>
</root>
然后它给出了以下错误

Warning: simplexml_load_file(): testdoc.xml:11: parser error : XML declaration allowed only at the start of the document in C:\xampp\htdocs\crea\services\testxml.php on line 3

Warning: simplexml_load_file(): <?xml version="1.0" encoding="UTF-8"?> in C:\xampp\htdocs\crea\services\testxml.php on line 3

Warning: simplexml_load_file(): ^ in C:\xampp\htdocs\crea\services\testxml.php on line 3

Warning: simplexml_load_file(): testdoc.xml:12: parser error : Extra content at the end of the document in C:\xampp\htdocs\crea\services\testxml.php on line 3

Warning: simplexml_load_file(): <root> in C:\xampp\htdocs\crea\services\testxml.php on line 3

Warning: simplexml_load_file(): ^ in C:\xampp\htdocs\crea\services\testxml.php on line 3
Error: Cannot create object
警告:simplexml\u load\u file():testdoc.xml:11:解析器错误:仅允许在第3行的C:\xampp\htdocs\crea\services\testxml.php中的文档开头声明xml
警告:simplexml\u load\u file():在第3行的C:\xampp\htdocs\crea\services\testxml.php中
警告:第3行C:\xampp\htdocs\crea\services\testxml.php中的simplexml\u load\u file():^
警告:simplexml\u load\u file():testdoc.xml:12:解析器错误:第3行C:\xampp\htdocs\crea\services\testxml.php中文档末尾有额外内容
警告:simplexml\u load\u file():在第3行的C:\xampp\htdocs\crea\services\testxml.php中
警告:第3行C:\xampp\htdocs\crea\services\testxml.php中的simplexml\u load\u file():^
错误:无法创建对象
请让我知道如何解析此xml或如何将其拆分为任何xml文件,以便我可以阅读。文件大小约为1 gb。

第二行

<?xml version="1.0" encoding="UTF-8"?>
in.xml
表示原始文件,
out.xml
表示清除的结果

printf
打印单个xml声明和开始/结束标记。
sed
是一种逐行编辑文件的工具,根据正则表达式模式匹配执行操作。要匹配的模式是xml声明的开头(
这是无效的xml。您需要使用字符串函数将其拆分,或者更准确地说,是逐部分读取

$xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>';

$file = new SplFileObject($filename, 'r');
$file->setFlags(SplFileObject::SKIP_EMPTY);
$buffer = '';
foreach ($file as $line) {
  if (FALSE === strpos($line, $xmlDeclaration)) {
    $buffer .= $line; 
  } else {
    outputBuffer($buffer);
    $buffer = $line;
  }
}
outputBuffer($buffer);

function outputBuffer($buffer) {
  if (!empty($buffer)) {
    $dom = new DOMDocument();
    $dom->loadXml($buffer);
    $xpath = new DOMXPath($dom);
    echo $xpath->evaluate('string(//element1)'), "\n";
  }
}

先生,该文件约为1GB,包含多个声明,以上只是其结构的示例。您是否可以控制该文件的生成?显然,它由连接良好的xml文件组成-它们可以通过剥离第一行进行预处理。我理解先生,请告诉我如何在t包含1 gb数据的xml文件,该文件来自数据源服务器,因此我无法控制生成xmluse
printf
sed
,以对文件进行后处理(删除有问题的xml声明并添加
metaroot
元素。添加cli示例以回答问题。
<?xml version="1.0" encoding="UTF-8"?>
<metaroot><!-- synthetic unique root, no semantics attached -->
    <root>
        <!-- ... -->
    </root>
    <root>
        <!-- ... -->
    </root>

    <!-- ... -->
</metaroot>
  printf "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<metaroot>\n" >out.xml
  sed '/<\?xml /d' in.xml >>out.xml
  printf "\n</metaroot>\n" >>out.xml
csplit -z -f 'temp' -b 'out%03d.xml' in.xml '/<\?xml /' {*}
$xmlDeclaration = '<?xml version="1.0" encoding="UTF-8"?>';

$file = new SplFileObject($filename, 'r');
$file->setFlags(SplFileObject::SKIP_EMPTY);
$buffer = '';
foreach ($file as $line) {
  if (FALSE === strpos($line, $xmlDeclaration)) {
    $buffer .= $line; 
  } else {
    outputBuffer($buffer);
    $buffer = $line;
  }
}
outputBuffer($buffer);

function outputBuffer($buffer) {
  if (!empty($buffer)) {
    $dom = new DOMDocument();
    $dom->loadXml($buffer);
    $xpath = new DOMXPath($dom);
    echo $xpath->evaluate('string(//element1)'), "\n";
  }
}
Stefan
Damon