0

I have a valid xml code in a string format. I would like to convert the string into an XML object.

$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);

Output:

object(SimpleXMLElement)#1 (0) {
}

I don't understand why I have a blank result.

jkdev
  • 11,360
  • 15
  • 54
  • 77
executable
  • 3,365
  • 6
  • 24
  • 52
  • This behaviour is expected - it's more a limitation of `var_dump()` than SimpleXML - https://stackoverflow.com/questions/16119597/dont-understand-the-output-in-xml-entities-and-php-simplexmlelement – Nigel Ren Dec 03 '19 at 13:01

1 Answers1

1

That is just how it is supposed to be, the data is still there, you just need to use the SimpleXML methods to get what you need, for demo try this:

<?php

$string = <<<XML
YOUR XML
XML;

$xml = simplexml_load_string($string);
var_dump($xml->getName());
jkdev
  • 11,360
  • 15
  • 54
  • 77
mega6382
  • 9,211
  • 17
  • 48
  • 69