douqi2571 2015-12-28 18:47 采纳率: 0%
浏览 42

用php获取xml节点

Im attempting to echo/assign a variable to the contents of the node "code" which is inside status;

I can get request-id just fine...

Any ideas people?

<?
$responseXML = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<payment xmlns="http://www.example.com" self="http://www.example.com">
  <merchant-account-id ref="http://www.example.com">0000</merchant-account-id>
  <transaction-id>0000</transaction-id>
  <request-id>0000</request-id>
  <transaction-type>auth</transaction-type>
  <transaction-state>success</transaction-state>
  <completion-time-stamp>2015-12-28T17:39:25.000Z</completion-time-stamp>
  <statuses>
    <status code="201.0000" description="3d-acquirer:The resource was successfully created." severity="information"/>
  </statuses>
  <avs-code>P</avs-code>
  <requested-amount currency="GBP">0.01</requested-amount>
  <account-holder>
    <first-name>test</first-name>
    <last-name>test</last-name>
    <email>test.test@hotmail.co.uk</email>
    <phone>00000000000</phone>
    <address>
    <street1>test</street1>
    <city>test test</city>
    <state>test</state>
    <country>GB</country>
    </address>
  </account-holder>
  <card-token>
    <token-id>000</token-id>
    <masked-account-number>000000******0000</masked-account-number>
  </card-token>
  <ip-address>192.168.0.1</ip-address>
  <descriptor></descriptor>
  <authorization-code>000000</authorization-code>
  <api-id>000-000</api-id>
</payment>';

$doc = new DOMDocument;
$doc->loadXML($responseXML);

echo $doc->getElementsByTagName('request-id')->item(0)->nodeValue;
echo $doc->getElementsByTagName('status code')->item(0)->nodeValue;



?>

I've tried simplexml looad string, but pulling hair out with this one, can anybody shed some light, speed of getting this info out in one process is quite important so not to stress the webserver out!

Many thanks.

</div>
  • 写回答

3条回答 默认 最新

  • doushenjia8514 2015-12-28 19:05
    关注

    As code is an attribute of xml tag status, doing

    getElementsByTagName('status code')
    

    is wrong.

    There's a special method for getting attribute value getAttribute:

    echo $doc->getElementsByTagName('status')->item(0)->getAttribute('code');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?