我尝试从php5.6迁移到php7.x但是我有读取xml的问题。
如果我在php5.6上运行此代码没有问题,在php7.x上有些奇怪的错误
echo 'LIBXML_VERSION ' . LIBXML_VERSION . "\n";
echo 'LIBXML_DOTTED_VERSION ' . LIBXML_DOTTED_VERSION . "\n";
libxml_use_internal_errors(true);
$entryContent ='<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0"
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"
xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0"
xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0"
xmlns:presentation="urn:oasis:names:tc:opendocument:xmlns:presentation:1.0"
xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0"
xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0"
xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0"
xmlns:math="http://www.w3.org/1998/Math/MathML"
xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0"
xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0"
xmlns:ooo="http://openoffice.org/2004/office"
xmlns:ooow="http://openoffice.org/2004/writer"
xmlns:oooc="http://openoffice.org/2004/calc"
xmlns:dom="http://www.w3.org/2001/xml-events"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:field="urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:field:1.0"
office:version="1.1">
<office:scripts/>
<office:body>
<office:spreadsheet>
<table:table table:name="Data">
<table:table-column table:style-name="co0"/>
<table:table-column table:style-name="co0"/>
<table:table-column table:style-name="co0"/>
<table:table-row>
<table:table-cell office:value-type="string"
office:value="Mar 16, 2017">
<text:p>Mar 16, 2017</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="2.0E">
<text:p>2.0E</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell office:value-type="string"
office:value="id"
table:style-name="ceHeader">
<text:p>id</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="tag1"
table:style-name="ceHeader">
<text:p>tag1</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="tag2"
table:style-name="ceHeader">
<text:p>tag2</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell office:value-type="string"
office:value="8d427c81bae5a27d07fa2d68eb348fbdd8e83571">
<text:p>8d427c81bae5a27d07fa2d68eb348fbdd8e83571</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="1">
<text:p>1</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="0">
<text:p>0</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell office:value-type="string"
office:value="e04913657b418d19838b920bc379fea9c4b7c76a">
<text:p>e04913657b418d19838b920bc379fea9c4b7c76a</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="0">
<text:p>0</text:p>
</table:table-cell>
<table:table-cell office:value-type="string"
office:value="1">
<text:p>1</text:p>
</table:table-cell>
</table:table-row>
</table:table>
</office:spreadsheet>
</office:body>
</office:document-content>
';
$xml = simplexml_load_string($entryContent, 'SimpleXMLElement');
if ($xml === false) {
echo "Failed loading XML\n";
foreach(libxml_get_errors() as $error) {
echo "\t", $error->message;
}
}
$ns = $xml->getNamespaces(true);
$spreadsheetXml = $xml->children($ns['office'])->{'body'}->{'spreadsheet'};
$headerRowXml = $spreadsheetXml->children($ns['table'])->{'table'}->{'table-row'}->{1};
print_r($headerRowXml);
print_r($headerRowXml->children($ns['table'])->{'table-cell'}->{1}->children($ns['text'])->{0});
print_r(libxml_get_errors());
我收到了这个输出和错误
LIBXML_VERSION 20904
LIBXML_DOTTED_VERSION 2.9.4
SimpleXMLElement对象 ( )
警告:main():第122行的test4.php中不再存在节点
致命错误:未捕获错误:在test4.php中调用null上的成员函数children():122
在php5.6上我得到了
LIBXML_VERSION 20902
LIBXML_DOTTED_VERSION 2.9.2
SimpleXMLElement Object
(
[table-cell] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[style-name] => ceHeader
)
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[style-name] => ceHeader
)
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[style-name] => ceHeader
)
)
)
)
SimpleXMLElement Object
(
[0] => tag1
)
Array
(
)
这个修复如何在php7.x中运行?