Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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_Simplexml - Fatal编程技术网

Php 如何修复尝试读取XML文件时的错误

Php 如何修复尝试读取XML文件时的错误,php,xml,simplexml,Php,Xml,Simplexml,我试图使用库simplexmlement读取PHP中的XML文件,但在获取其值时发现以下错误: Fatal error: Uncaught Error: Call to a member function attributes() on null 我尝试读取的XML文件如下所示: <?xml version="1.0" encoding="UTF-8"?> <cfdi:Comprobante Version="3.3"

我试图使用库
simplexmlement
读取PHP中的XML文件,但在获取其值时发现以下错误:

Fatal error: Uncaught Error: Call to a member function attributes() on null
我尝试读取的XML文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante Version="3.3" Total="45264.13">
    <cfdi:Emisor Rfc="ABC123456AB1" Nombre="JOHN DOE"/>
     <cfdi:Conceptos>
         <cfdi:Concepto ValorUnitario="93.80">
        </cfdi:Concepto>
    </cfdi:Conceptos>
     <cfdi:Complemento>
        <tfd:Digital Version="1.1" Sello="BXTJPwDh+" NoCertificado="0000100"
            FechaTimbrado="2019-07-18" UID="C58C"
            xmlns:tfd="http://www.stackoverflow.com"/>
    </cfdi:Complemento>
</cfdi:Comprobante>
我重复使用
rfc
total
我没有任何问题,我遵循相同的想法,但是如果它产生开头提到的错误,我将读取
UID

您知道我读取此值的方式是否不同,或者我犯了错误。

数字元素位于“Complemento”元素内,但您的
$xml
变量表示顶级的“Comprobante”元素

因此,您需要浏览:

$uid=(字符串)$xml
->children('cfdi',true)->Complemento[0]
->子项('tfd',true)->数字[0]
->属性()['UID'];
请注意,您可以省去
[0]
,这样在只有一个元素具有该名称时更易于阅读:

$uid=(字符串)$xml
->儿童('cfdi',真)->补语
->儿童('tfd',正确)->数字
->属性()['UID'];
另外,我强烈建议您使用完整的名称空间标识符(URI),而不是短前缀,除非这是您将要丢弃的代码。前缀可以随时更改(例如,因为生成XML的软件发生更改),但完整标识符不会更改。您可以将它们放入常量中,以避免重新键入/粘贴它们,如下所示:

const XMLNS\u CFDI=”http://example.com/you-didnt-include-this-in-your-example';
常数XMLNS\u TFD=http://www.stackoverflow.com';
$uid=(字符串)$xml
->儿童(XMLNS_CFDI)->补语
->儿童(XMLNS_TFD)->数字
->属性()['UID'];

与前面发布的答案不同,您可以通过将XML数据处理到关联数组来方便XML数据的解析和引用。剥离名称空间,并使用
json_decode(json_encode($xml),true)将xml转换为关联数组

您可以使用
var\u dump()
print\u r()
来获得数组结构的视觉效果,以选定元素为目标

<?php
$xml = file_get_contents('data.xml');
$cleanXml = str_ireplace(['cfdi:', 'tfd:'], '', $xml); // strip namespace prefixes from xml
$xml = simplexml_load_string($cleanXml);
$arr = json_decode(json_encode($xml), true);

// reference the elements you need in the associative array
$total = (float)$arr['@attributes']['Total'];
$rfc = (string)$arr['Emisor']['@attributes']['Rfc'];
$uid = (string)$arr['Complemento']['Digital']['@attributes']['UID'];

// output
echo $total;
echo $rfc;
echo $uid;

<?php
$xml = file_get_contents('data.xml');
$cleanXml = str_ireplace(['cfdi:', 'tfd:'], '', $xml); // strip namespace prefixes from xml
$xml = simplexml_load_string($cleanXml);
$arr = json_decode(json_encode($xml), true);

// reference the elements you need in the associative array
$total = (float)$arr['@attributes']['Total'];
$rfc = (string)$arr['Emisor']['@attributes']['Rfc'];
$uid = (string)$arr['Complemento']['Digital']['@attributes']['UID'];

// output
echo $total;
echo $rfc;
echo $uid;