user2021091
user2021091

Reputation: 571

Parsing an XML Caldav request in php

I'm trying to parse a request sent by ThunderBird to my CalDAV Server and from an example taken from stackoverflow with an XML like :

<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
    <last_updated>2009-11-30 13:52:40</last_updated>
    <product>
        <element_1>foo</element_1>
        <element_2>foo</element_2>
        <element_3>foo</element_3>
        <element_4>foo</element_4>
    </product>
</products> 

Using the function :

$XMLr = new XMLReader;
$XMLr->open('test.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($XMLr->read() && $XMLr->name !== 'product');


// now that we're at the right depth, hop to the next <product/> until the end of the tree
    $node = simplexml_import_dom($doc->importNode($XMLr->expand(), true));

    // now you can use $node without going insane about parsing
    $children = $node->children();
    foreach($children as $child)
    {
        echo $child->getName();
        echo "\n";
    }

I get the answer "element_1 element_2 element_3 element_4 ", but if I use the same function on my request :

<?xml version="1.0" encoding="UTF-8"?>
<D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/" xmlns:C="urn:ietf:params:xml:ns:caldav">
    <D:prop>
        <D:resourcetype/>
        <D:owner/>
        <D:current-user-principal/>
        <D:supported-report-set/>
        <C:supported-calendar-component-set/>
        <CS:getctag/>
    </D:prop>
</D:propfind>

Replacing $XMLr->name !== 'product' by $XMLr->name !== 'D:prop' I get a white screen...

What do I do wrong ? How can I get the answer "ressourcetype owner current-user-principal etc ..." ?

Upvotes: 2

Views: 828

Answers (2)

bux
bux

Reputation: 7739

I try with XMLReader and simplexml_import_dom without success but in opposite, with DomDocument you can do it:

// Just for display test results
$break_line = '<br>';
if (php_sapi_name() === 'cli') {
  $break_line = "\n";
}

$xml = '<?xml version="1.0" encoding="UTF-8"?>
<D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/" xmlns:C="urn:ietf:params:xml:ns:caldav">
    <D:prop>
        <D:resourcetype/>
        <D:owner/>
        <D:current-user-principal/>
        <D:supported-report-set/>
        <C:supported-calendar-component-set/>
        <CS:getctag/>
    </D:prop>
</D:propfind>';

$xml_document = new DomDocument(); // http://fr2.php.net/manual/fr/class.domdocument.php
$xml_document->loadXML($xml); // Or load file with $xml_document->load('test.xml);
$elements = $xml_document->getElementsByTagName('prop'); 
// $elements is a DOMNodeList object: http://fr2.php.net/manual/fr/class.domnodelist.php
foreach($elements as $element) {
  // $element is a DOMElement object: http://fr2.php.net/manual/fr/class.domelement.php
  $childs = $element->childNodes;
  // $childs is DOMNodeList
  foreach ($childs as $child) {
    // $element is a DOMElement object
    if ($child instanceof DOMElement) {
      echo $child->nodeName . $break_line;
    }
  }

}

Upvotes: 2

Youri
Youri

Reputation: 505

White screen normally means error.

Put error_reporting on E_ALL

error_reporting('E_ALL');

Upvotes: 0

Related Questions