Reputation: 1005
I've got webdav PROPFIND xml response that I'm getting from OwnCloud. I need to convert to a PHP object (and ultimately json). The problem is that the response is heavy with namespaces, which php's simplexml apparently can't handle very well.
This is what the xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<d:multistatus xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns" xmlns:oc="http://owncloud.org/ns">
<d:response>
<d:href>/owncloud/remote.php/webdav/ownCloudUserManual.pdf</d:href>
<d:propstat>
<d:prop>
<d:getlastmodified>Sun, 04 Oct 2015 18:01:18 GMT</d:getlastmodified>
<d:getcontentlength>2241884</d:getcontentlength>
<d:resourcetype/>
<d:getetag>"b00009ac4b1b17c45667abd2a6d2f7c7"</d:getetag>
<d:getcontenttype>application/pdf</d:getcontenttype>
</d:prop>
<d:status>HTTP/1.1 200 OK</d:status>
</d:propstat>
</d:response>
</d:multistatus>
When I put it through simplexml, I do not get anything back:
$xml = simplexml_load_string($xml);
print_r( $xml );
The print_r gives:
SimpleXMLElement Object
(
)
{}
There doesn't appear to be any simplexml errors (as returned from libxml_get_errors()) - there's just nothing returned but an empty class.
I could use some advice on how to parse the XML into something useful. Thanks for any help.
Upvotes: 2
Views: 1601
Reputation: 878
I just ran into the same problem and found out how to solve this. The comments above point in the right direction, but I wanted to show an actual solution here:
you need to get the namespaces that are inferred in the second line of the XML code with "xmlns:..":
$ns = $xml->getNamespaces(true);
Then you can pick all children of the desired namespace ('d') with the following command:
$child = $xml->children($ns['d']);
after that, you can iterate the 'response' elements and extract information from their children:
foreach ($child->response as $files) {
echo $files->href . "<br>";
}
that will output for example all the HREF elements, in your case the "/owncloud/remote.php/webdav/ownCloudUserManual.pdf" and any other directory contents.
Upvotes: 0