douwei1930 2010-09-22 10:26
浏览 34
已采纳

如何用PHP解析eXist XQuery响应?

I am trying to find an easy way to parse an eXist response with php without installing third party classes.

This is the type of response that I would like to parse :

<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>

I am trying with the simplexml_load_string php function but I get back an empty SimpleXMLElement object with no errors.

I would like to get out of this response the list of XML files.

Thanks

  • 写回答

2条回答 默认 最新

  • dongpalou5352 2010-09-22 11:01
    关注

    Using DOMDocument and DOMXPath is the most "correct" way to access those attributes:

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

    Which produces this:

    Array
    (
        [0] => xDataData1.xml
        [1] => xDataData10.xml
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了