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 simplexml_load_string()返回空对象_Php_Xml - Fatal编程技术网

Php simplexml_load_string()返回空对象

Php simplexml_load_string()返回空对象,php,xml,Php,Xml,我面临simplexml_load_string函数的问题,该函数以前可以工作,但今天它停止了工作 以下是输入xml的示例:- $response = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns

我面临simplexml_load_string函数的问题,该函数以前可以工作,但今天它停止了工作

以下是输入xml的示例:-

$response = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><ns1:ProductSearchResponse><ns1:TestMode>false</ns1:TestMode><ns1:Page>1</ns1:Page><ns1:ResultsPerPage>1</ns1:ResultsPerPage><ns1:TotalResults>1276</ns1:TotalResults><ns1:Items><ns1:Item><ns1:StockCode>L3720</ns1:StockCode><ns1:Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</ns1:Name><ns1:QuantityAvailable>1</ns1:QuantityAvailable><ns1:UnitPrice Currency="GBP"><ns1:Amount>8.72</ns1:Amount></ns1:UnitPrice><ns1:YourRating xsi:nil="true"/><ns1:YourStockCode></ns1:YourStockCode><ns1:ImageLastUpdated>2016-12-22 18:13:08</ns1:ImageLastUpdated><ns1:ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ns1:ThumbnailImageUrl><ns1:HighResImageUrl xsi:nil="true"/></ns1:Item></ns1:Items></ns1:ProductSearchResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';
$xml = simplexml_load_string(($response));

//This returning following response:-

SimpleXMLElement Object   (
)
$response='False111276L3720大卫·贝克汉姆本能礼品套装30毫升EDT+150毫升沐浴露18.722016-12-22 18:13:08https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D';
$xml=simplexml_load_字符串(($response));
//这项答复如下:-
SimpleXMLElement对象(
)

有人能检查一下,让我知道这里出了什么问题。

您得到的输出只不过是不正确而已。在处理SimpleXML和DOMDocument时,它们的结构非常复杂,并且您并不总能获得所有信息

使用SimpleXML时—查看节点内容的最佳方法是使用
asXML()
,它会重新创建节点的原始XML

$xml = simplexml_load_string($response);

echo $xml->asXML();

这将为您提供原始内容。

您获得的输出只不过是不正确的,没有多大帮助。在处理SimpleXML和DOMDocument时,它们的结构非常复杂,并且您并不总能获得所有信息

使用SimpleXML时—查看节点内容的最佳方法是使用
asXML()
,它会重新创建节点的原始XML

$xml = simplexml_load_string($response);

echo $xml->asXML();

这将为您提供原始内容。

我尝试在下面列出XML中的所有产品

 $response = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><ns1:ProductSearchResponse><ns1:TestMode>false</ns1:TestMode><ns1:Page>1</ns1:Page><ns1:ResultsPerPage>1</ns1:ResultsPerPage><ns1:TotalResults>1276</ns1:TotalResults><ns1:Items><ns1:Item><ns1:StockCode>L3720</ns1:StockCode><ns1:Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</ns1:Name><ns1:QuantityAvailable>1</ns1:QuantityAvailable><ns1:UnitPrice Currency="GBP"><ns1:Amount>8.72</ns1:Amount></ns1:UnitPrice><ns1:YourRating xsi:nil="true"/><ns1:YourStockCode></ns1:YourStockCode><ns1:ImageLastUpdated>2016-12-22 18:13:08</ns1:ImageLastUpdated><ns1:ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ns1:ThumbnailImageUrl><ns1:HighResImageUrl xsi:nil="true"/></ns1:Item></ns1:Items></ns1:ProductSearchResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';

   $xml = simplexml_load_string(($response));
   $posts = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->ProductSearchResponse;
$response='False111276L3720大卫·贝克汉姆本能礼品套装30毫升EDT+150毫升沐浴露18.722016-12-22 18:13:08https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D';
$xml=simplexml_load_字符串(($response));
$posts=$xml->children('SOAP-ENV',true)->Body->children('ns1',true)->ProductSearchResponse;

我尝试下面列出XML中的所有产品

 $response = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Body><ns1:ProductSearchResponse><ns1:TestMode>false</ns1:TestMode><ns1:Page>1</ns1:Page><ns1:ResultsPerPage>1</ns1:ResultsPerPage><ns1:TotalResults>1276</ns1:TotalResults><ns1:Items><ns1:Item><ns1:StockCode>L3720</ns1:StockCode><ns1:Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</ns1:Name><ns1:QuantityAvailable>1</ns1:QuantityAvailable><ns1:UnitPrice Currency="GBP"><ns1:Amount>8.72</ns1:Amount></ns1:UnitPrice><ns1:YourRating xsi:nil="true"/><ns1:YourStockCode></ns1:YourStockCode><ns1:ImageLastUpdated>2016-12-22 18:13:08</ns1:ImageLastUpdated><ns1:ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ns1:ThumbnailImageUrl><ns1:HighResImageUrl xsi:nil="true"/></ns1:Item></ns1:Items></ns1:ProductSearchResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>';

   $xml = simplexml_load_string(($response));
   $posts = $xml->children('SOAP-ENV', true)->Body->children('ns1', true)->ProductSearchResponse;
$response='False111276L3720大卫·贝克汉姆本能礼品套装30毫升EDT+150毫升沐浴露18.722016-12-22 18:13:08https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D';
$xml=simplexml_load_字符串(($response));
$posts=$xml->children('SOAP-ENV',true)->Body->children('ns1',true)->ProductSearchResponse;

要回答您的问题,让我们先检查一下这个问题

问题 假设您通过以下方式显示
$xml
变量的内容:

php > print_r($xml);
在这里,您列出了当前节点(根节点)的子节点,其默认名称空间(不带前缀)未在该节点中声明:

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
php > print_r($xml->getNamespaces());
Array
(
    [SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
)
只有一个节点可见,因为它是该(根)节点中实际使用的唯一节点,不包括其子节点

要列出根目录及其所有子目录的所有已用名称空间,请添加“true”作为第一个参数:

php > print_r($xml->getNamespaces(true));
Array
(
    [SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
    [ns1] => http://www.beautyfort.com/api/
    [xsi] => http://www.w3.org/2001/XMLSchema-instance
)
要列出根目录及其所有子目录的所有已声明名称空间,请执行以下操作:

php > print_r($xml->getDocNamespaces(true));
Array
(
    [SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
    [ns1] => http://www.beautyfort.com/api/
    [xsi] => http://www.w3.org/2001/XMLSchema-instance
)
解决方案#1
simplexml\u load\u string
允许将默认名称空间设置为参数,例如
SOAP-ENV
(带有额外参数以指示它是前缀):

如果您现在打印变量,您将更接近您所需要的:

php > print_r($xml);
SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
        )

)
但这只适用于根节点

在列出子项时,如果子项使用的名称空间不是默认(空)名称空间,则仍需要指定名称空间,并指示其是前缀还是名称空间:

php > print_r($xml->children("SOAP-ENV", true));
SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
        )

)
要查看第一个根子级的子级,请执行以下操作:

php > print_r($xml->children("SOAP-ENV", true)->children("ns1", true));
SimpleXMLElement Object
(
    [ProductSearchResponse] => SimpleXMLElement Object
        (
            ...
        )

)
但这可能不是您想要的,因为它不是通用的解决方案

解决方案#2 对所有名称空间使用字符串替换并重新加载XML文档(存储在
$content
中):

您的输入XML现在如下所示:

php > echo $content2;
<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Body><ProductSearchResponse><TestMode>false</TestMode><Page>1</Page><ResultsPerPage>1</ResultsPerPage><TotalResults>1276</TotalResults><Items><Item><StockCode>L3720</StockCode><Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</Name><QuantityAvailable>1</QuantityAvailable><UnitPrice Currency="GBP"><Amount>8.72</Amount></UnitPrice><YourRating nil="true"/><YourStockCode></YourStockCode><ImageLastUpdated>2016-12-22 18:13:08</ImageLastUpdated><ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ThumbnailImageUrl><HighResImageUrl nil="true"/></Item></Items></ProductSearchResponse></Body></Envelope>

为了回答你的问题,让我们先检查一下这个问题

问题 假设您通过以下方式显示
$xml
变量的内容:

php > print_r($xml);
在这里,您列出了当前节点(根节点)的子节点,其默认名称空间(不带前缀)未在该节点中声明:

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
php > print_r($xml->getNamespaces());
Array
(
    [SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
)
只有一个节点可见,因为它是该(根)节点中实际使用的唯一节点,不包括其子节点

要列出根目录及其所有子目录的所有已用名称空间,请添加“true”作为第一个参数:

php > print_r($xml->getNamespaces(true));
Array
(
    [SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
    [ns1] => http://www.beautyfort.com/api/
    [xsi] => http://www.w3.org/2001/XMLSchema-instance
)
要列出根目录及其所有子目录的所有已声明名称空间,请执行以下操作:

php > print_r($xml->getDocNamespaces(true));
Array
(
    [SOAP-ENV] => http://schemas.xmlsoap.org/soap/envelope/
    [ns1] => http://www.beautyfort.com/api/
    [xsi] => http://www.w3.org/2001/XMLSchema-instance
)
解决方案#1
simplexml\u load\u string
允许将默认名称空间设置为参数,例如
SOAP-ENV
(带有额外参数以指示它是前缀):

如果您现在打印变量,您将更接近您所需要的:

php > print_r($xml);
SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
        )

)
但这只适用于根节点

在列出子项时,如果子项使用的名称空间不是默认(空)名称空间,则仍需要指定名称空间,并指示其是前缀还是名称空间:

php > print_r($xml->children("SOAP-ENV", true));
SimpleXMLElement Object
(
    [Body] => SimpleXMLElement Object
        (
        )

)
要查看第一个根子级的子级,请执行以下操作:

php > print_r($xml->children("SOAP-ENV", true)->children("ns1", true));
SimpleXMLElement Object
(
    [ProductSearchResponse] => SimpleXMLElement Object
        (
            ...
        )

)
但这可能不是您想要的,因为它不是通用的解决方案

解决方案#2 对所有名称空间使用字符串替换并重新加载XML文档(存储在
$content
中):

您的输入XML现在如下所示:

php > echo $content2;
<?xml version="1.0" encoding="UTF-8"?><Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.beautyfort.com/api/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Body><ProductSearchResponse><TestMode>false</TestMode><Page>1</Page><ResultsPerPage>1</ResultsPerPage><TotalResults>1276</TotalResults><Items><Item><StockCode>L3720</StockCode><Name>David Beckham Instinct Gift Set 30ml EDT + 150ml Shower Gel</Name><QuantityAvailable>1</QuantityAvailable><UnitPrice Currency="GBP"><Amount>8.72</Amount></UnitPrice><YourRating nil="true"/><YourStockCode></YourStockCode><ImageLastUpdated>2016-12-22 18:13:08</ImageLastUpdated><ThumbnailImageUrl>https://www.beautyfort.com/pic/Y0NqeTBJbmdvaUx6ZUFOa0MyTlNObmhGckltYnVQQmg%3D</ThumbnailImageUrl><HighResImageUrl nil="true"/></Item></Items></ProductSearchResponse></Body></Envelope>

我还尝试使用$xml=simplexml\u load\u字符串(trim($response)),但输出相同。它不是空的,只是
simplexmlement
不能很好地处理
print\u r()
var\u dump()
。例如,
var_dump($xml->getnamespace())
正确返回名称空间。我也尝试使用$xml=simplexml\u load\u string(trim($response)),但输出相同。它不是空的,只是
simplexmlement
不能很好地处理
print\u r()
var\u dump()
。例如,
var_dump($xml->getnamespace())正确返回名称空间。