【问题标题】:Can't convert string into XML object无法将字符串转换为 XML 对象
【发布时间】:2019-12-04 06:02:15
【问题描述】:

我有一个字符串格式的有效 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) {
}

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

【问题讨论】:

标签: php xml


【解决方案1】:

本来就是这样,数据还在,你只需要使用SimpleXML方法来获取你需要的东西,demo试试这个:

<?php

$string = <<<XML
YOUR XML
XML;

$xml = simplexml_load_string($string);
var_dump($xml->getName());

【讨论】:

  • 你如何提取/remote.php/dav/files/test3.txt 例如?
猜你喜欢
  • 2012-07-11
  • 2011-03-12
  • 2013-06-28
  • 1970-01-01
  • 2013-06-20
  • 2018-11-27
相关资源
最近更新 更多