在php中解析XML Caldav请求
我正试图解析ThunderBird发送到我的CalDAV服务器的请求,并从stackoverflow的一个示例中使用如下XML进行解析:在php中解析XML Caldav请求,php,xml,caldav,Php,Xml,Caldav,我正试图解析ThunderBird发送到我的CalDAV服务器的请求,并从stackoverflow的一个示例中使用如下XML进行解析: <?xml version="1.0" encoding="ISO-8859-1"?> <products> <last_updated>2009-11-30 13:52:40</last_updated> <product> <element_1>foo&
<?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>
2009-11-30 13:52:40
福
福
福
福
使用该功能:
$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";
}
$XMLr=新的XMLReader;
$XMLr->open('test.xml');
$doc=新文档;
//移动到第一个节点
而($XMLr->read()&&$XMLr->name!=='product');
//现在我们已经到达了正确的深度,跳到下一个直到树的尽头
$node=simplexml\u import\u dom($doc->importNode($XMLr->expand(),true));
//现在,您可以使用$node而不必为解析而发狂
$children=$node->children();
foreach($childrenas$child)
{
echo$child->getName();
回音“\n”;
}
我得到的答案是“element_1 element_2 element_3 element_4”,但如果我在请求时使用相同的函数:
<?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>
替换$XMLr->name!=='产品“按$XMLr->name!=”D:我有一个白色的屏幕
我做错了什么?
如何获得“ressourcetype所有者当前用户主体等”的答案?白色屏幕通常表示错误 将错误报告放在E_ALL上
error_reporting('E_ALL');
我尝试使用
XMLReader
和simplexml\u import\u dom
但没有成功,相反,使用DomDocument您可以做到:
// 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;
}
}
}
//仅用于显示测试结果
$break_line='
';
如果(php_sapi_name()=='cli'){
$break_line=“\n”;
}
$xml='0
';
$xml_document=new DomDocument();//http://fr2.php.net/manual/fr/class.domdocument.php
$xml_document->loadXML($xml);//或者使用$xml\u document->load('test.xml)加载文件;
$elements=$xml_document->getElementsByTagName('prop');
//$elements是一个域节点列表对象:http://fr2.php.net/manual/fr/class.domnodelist.php
foreach($elements作为$element){
//$element是一个domeElement对象:http://fr2.php.net/manual/fr/class.domelement.php
$childs=$element->childNodes;
//$childs是DOMNodeList
foreach($childs作为$child){
//$element是一个domeElement对象
if($domeElement的子实例){
echo$child->nodeName.$break\u line;
}
}
}