首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果标签中有特殊字符,simplexml_load_string总是失败的。

如果标签中有特殊字符,simplexml_load_string总是失败的。
EN

Stack Overflow用户
提问于 2022-07-21 09:24:39
回答 1查看 67关注 0票数 1

我正在从外部web服务接收这样的XML (注意标签内的冒号)

XML

代码语言:javascript
复制
<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns0="http://webservice/CrsWSApi.wsdl/types/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
        <ns0:callwsapiResponseElement>
            <ns0:result>
                <ns0:object>BTS</ns0:object>
                <ns0:receiver>BTSV45_ANSF_PROD-AKITT</ns0:receiver>
                <ns0:sender>ANSFP</ns0:sender>
                <ns0:data>
                {"name":"Paolo", "cognome": Rossi, "data":"18/11/1983"}
                </ns0:data>
                <ns0:errormess xsi:nil="true"/>
                <ns0:errorcode xsi:nil="true"/>
                <ns0:status>1</ns0:status>
                <ns0:sessionid>akit1</ns0:sessionid>
            </ns0:result>
        </ns0:callwsapiResponseElement>
    </env:Body>
</env:Envelope>

如果我使用这个函数来解析它

代码语言:javascript
复制
$xml = simplexml_load_string($xml) or die("Error: Cannot create object");

我总是收到错误。如果我移除结肠,它就会像预期的那样工作。在xml解析之前,是否有一种无需操作冒号的方法来使其工作呢?

我想避免这种操作PHP

代码语言:javascript
复制
$xml = preg_replace(['/env:/','/:env(?!=)/','/ns0:/','/:ns0(?!=)/'], '', $myXMLData);

更新

我已经接受了一个很好的答案,就是报告关于名称xpath在xml环境中是如何工作的有用链接,但是我想要共享另一种方法来获得相同的结果,而无需解析xpath表达式。我们可以使用simple_xml_loadstring的子方法,将第二个参数指定为命名空间。

对于上述代码,您可以使用此代码获得ns0:data内容(在您必须将if语句更改为接受的答案之前)

代码语言:javascript
复制
    $xml = simplexml_load_string($response);
    
    echo $xml
         ->children("env", true)
         ->Body
         ->children("ns0", true)
         ->callwsapiResponseElement
         ->result
         ->data;

// This will print
{"name":"Paolo", "cognome": Rossi, "data":"18/11/1983"}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-22 07:46:42

对我来说,simplexml_load_string (https://www.php.net/manual/en/function.simplexml-load-string.php)的返回非常奇怪:

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

实际上,如果您替换了or die

代码语言:javascript
复制
<?php
$response = <<<XML
<?xml version='1.0' encoding='UTF-8'?>
<env:Envelope
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns0="http://webservice/CrsWSApi.wsdl/types/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <env:Body>
        <ns0:callwsapiResponseElement>
            <ns0:result>
                <ns0:object>BTS</ns0:object>
                <ns0:receiver>BTSV45_ANSF_PROD-AKITT</ns0:receiver>
                <ns0:sender>ANSFP</ns0:sender>
                <ns0:data>
                {"name":"Paolo", "cognome": Rossi, "data":"18/11/1983"}
                </ns0:data>
                <ns0:errormess xsi:nil="true"/>
                <ns0:errorcode xsi:nil="true"/>
                <ns0:status>1</ns0:status>
                <ns0:sessionid>akit1</ns0:sessionid>
            </ns0:result>
        </ns0:callwsapiResponseElement>
    </env:Body>
</env:Envelope>
XML;
$xml = simplexml_load_string($response);
if($xml===false){
    die();
}
echo $xml->xpath("//*[local-name() = 'receiver']")[0];

结果:

代码语言:javascript
复制
BTSV45_ANSF_PROD-AKITT

然后,您可以使用名称空间来查找数据。这是一个不错的帖子Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73063707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档