无法将字符串转换为XML对象

时间:2019-12-03 12:51:56

标签: php xml

我有一个字符串格式的有效xml代码。我想将字符串转换为XML对象。

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<d:multistatus xmlns:d="DAV:" xmlns:nc="http://example.com" xmlns:oc="http://example.com" xmlns:s="http://example.com">
   <d:response>
      <d:href>/remote.php/dav/files/test3.txt</d:href>
      <d:propstat>
         <d:prop>
            <d:getlastmodified>Tue, 03 Dec 2019 12:42:33 GMT</d:getlastmodified>
            <d:resourcetype>
               <d:collection />
            </d:resourcetype>
            <d:quota-used-bytes>1942356098</d:quota-used-bytes>
            <d:quota-available-bytes>-3</d:quota-available-bytes>
            <d:getetag>"5de6583924a4b"</d:getetag>
         </d:prop>
         <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
   </d:response>
   <d:response>
      <d:href>/remote.php/dav/files/test2.txt</d:href>
      <d:propstat>
         <d:prop>
            <d:getlastmodified>Thu, 21 Nov 2019 11:30:59 GMT</d:getlastmodified>
            <d:resourcetype>
               <d:collection />
            </d:resourcetype>
            <d:quota-used-bytes>130</d:quota-used-bytes>
            <d:quota-available-bytes>-3</d:quota-available-bytes>
            <d:getetag>"5dd675741bdcb"</d:getetag>
         </d:prop>
         <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
      <d:propstat>
         <d:prop>
            <d:getcontentlength />
            <d:getcontenttype />
         </d:prop>
         <d:status>HTTP/1.1 404 Not Found</d:status>
      </d:propstat>
   </d:response>
   <d:response>
      <d:href>/remote.php/dav/files/test.txt</d:href>
      <d:propstat>
         <d:prop>
            <d:getlastmodified>Wed, 27 Nov 2019 10:47:45 GMT</d:getlastmodified>
            <d:getcontentlength>1942355968</d:getcontentlength>
            <d:resourcetype />
            <d:getetag>"aa05cb48be85a3c306421807c2467acf"</d:getetag>
            <d:getcontenttype>application/octet-stream</d:getcontenttype>
         </d:prop>
         <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
      <d:propstat>
         <d:prop>
            <d:quota-used-bytes />
            <d:quota-available-bytes />
         </d:prop>
         <d:status>HTTP/1.1 404 Not Found</d:status>
      </d:propstat>
   </d:response>
   <d:response>
      <d:href>/remote.php/dav/files/openSUSE-Leap-42.3-DVD-x86_64.iso/</d:href>
      <d:propstat>
         <d:prop>
            <d:getlastmodified>Wed, 27 Nov 2019 10:31:51 GMT</d:getlastmodified>
            <d:resourcetype>
               <d:collection />
            </d:resourcetype>
            <d:quota-used-bytes>0</d:quota-used-bytes>
            <d:quota-available-bytes>-3</d:quota-available-bytes>
            <d:getetag>"5de6309233431"</d:getetag>
         </d:prop>
         <d:status>HTTP/1.1 200 OK</d:status>
      </d:propstat>
      <d:propstat>
         <d:prop>
            <d:getcontentlength />
            <d:getcontenttype />
         </d:prop>
         <d:status>HTTP/1.1 404 Not Found</d:status>
      </d:propstat>
   </d:response>
</d:multistatus>
XML;

$xml = simplexml_load_string($string);
var_dump($xml);

输出:

object(SimpleXMLElement)#1 (0) {
}

我不明白为什么我的结果空白。

1 个答案:

答案 0 :(得分:1)

这就是应该的样子,数据仍然存在,您只需要使用SimpleXML方法来获取所需的内容,demo可以尝试以下操作:

<?php

$string = <<<XML
YOUR XML
XML;

$xml = simplexml_load_string($string);
var_dump($xml->getName());
相关问题