Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用PHP解析eXist XQuery响应?_Php_Parsing_Simplexml_Exist Db - Fatal编程技术网

如何用PHP解析eXist XQuery响应?

如何用PHP解析eXist XQuery响应?,php,parsing,simplexml,exist-db,Php,Parsing,Simplexml,Exist Db,我试图找到一种简单的方法,在不安装第三方类的情况下,用php解析存在响应 这是我要分析的响应类型: <exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist"> <exist:collection name="xData" path="/db/SBXXX/00000000-00000000/sapData" created="2010-07-15T16:12:09.135-05:00">

我试图找到一种简单的方法,在不安装第三方类的情况下,用php解析存在响应

这是我要分析的响应类型:

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">
  <exist:collection name="xData" path="/db/SBXXX/00000000-00000000/sapData" created="2010-07-15T16:12:09.135-05:00">
    <exist:resource name="xDataData1.xml" size="1565" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-20T23:42:09.584-05:00"/>
    <exist:resource name="xDataData10.xml" size="805" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:27:09.614-05:00"/>
    <exist:resource name="xDataData11.xml" size="806" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:27:22.005-05:00"/>
    <exist:resource name="xDataData12.xml" size="1565" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:42:15.768-05:00"/>
    <exist:resource name="xDataData13.xml" size="805" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:57:09.749-05:00"/>
  </exist:collection>
</exist:result>

我正在尝试使用simplexml\u load\u stringphp函数,但是我得到了一个空的simplexmlement对象,没有任何错误

我想从这个响应中得到XML文件列表


谢谢

设置名称空间,并使用xpath:

    $xml = '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">
  <exist:collection name="xData" path="/db/SBXXX/00000000-00000000/sapData" created="2010-07-15T16:12:09.135-05:00">
    <exist:xmlObject name="xDataData1.xml" size="1565" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-20T23:42:09.584-05:00"/>
    <exist:resource name="xDataData10.xml" size="805" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:27:09.614-05:00"/>
    <exist:resource name="xDataData11.xml" size="806" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:27:22.005-05:00"/>
    <exist:resource name="xDataData12.xml" size="1565" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:42:15.768-05:00"/>
    <exist:resource name="xDataData13.xml" size="805" created="1969-12-31T18:00:00.000-06:00" last-modified="2010-09-21T01:57:09.749-05:00"/>
  </exist:collection>
</exist:result>';
$xmlObject = simplexml_load_string($xml);
$xmlObject->registerXPathNamespace('e','http://exist.sourceforge.net/NS/exist');
var_dump($xmlObject->xpath('//e:resource'));
$xml='1!'
';
$xmlObject=simplexml\u load\u字符串($xml);
$xmlObject->registerXPathNamespace('e','http://exist.sourceforge.net/NS/exist');
var_dump($xmlObject->xpath('//e:resource'));

使用DOMDocument和DOMXPath是访问这些属性的最“正确”的方法:
$xml = '<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist">
  <exist:collection name="xData">
    <exist:resource name="xDataData1.xml"/>
    <exist:resource name="xDataData10.xml"/>
  </exist:collection>
</exist:result>
';

$dom = DOMDocument::loadXML($xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('exist', 'http://exist.sourceforge.net/NS/exist');

$nodes = $xpath->query('exist:collection/exist:resource');
if (!$nodes->length)
  exit('No nodes found');

$items = array();
foreach ($nodes as $node)
  $items[] = $node->getAttribute('name');

print_r($items);
Array
(
    [0] => xDataData1.xml
    [1] => xDataData10.xml
)