PHP 更改 XML 节点值 [英] PHP Change XML node values

查看:28
本文介绍了PHP 更改 XML 节点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 PHP 更改 XML 节点值时遇到了一些困难.

我的 XML 如下

<肥皂:身体><处理事务xmlns="http://example.com"><交易请求xmlns="http://example.com"><标题><RequestType>SALE</RequestType><RequestMethod>SYNCHRONOUS</RequestMethod><商家信息><PosName>kwstasna</PosName><PosID>1234</PosID></MerchantInfo></标题></交易请求></ProcessTransaction></soap:Body></soap:Envelope>

我想更改PosNamePosID.XML 是从 POST 请求接收的.如果我 print_r($REQUEST['xml']我得到文本中的值.

我试过的是以下

$posid = '321';$posname = 'nakwsta';$result = $xml->xpath("/soap:Envelope/soap:Body/ProcessTransaction/TransactionRequest/Header/MerchantInfo");$result[0]->PosID = $posid;$result[0]->PosName = $posname;回声 $result;

但我得到一个空数组 Array[]

例如,我认为我的错误在于 <soap:Envelope 的值.有没有遇到同样问题并找到解决方法的人?

非常感谢您抽出宝贵时间.

解决方案

ProcessTransaction 元素(及其所有子节点)位于 "http://example.com" 命名空间.如果你想使用 xpath() 访问它们,你需要注册一个命名空间前缀:

$xml->registerXPathNamespace('ex', 'http://example.com');

然后您可以在查询的所有相关部分使用 ex 前缀

$result = $xml->xpath("/soap:Envelope/soap:Body/ex:ProcessTransaction/ex:TransactionRequest/ex:Header/ex:MerchantInfo");

您的其余代码应该可以正常运行,请参阅 https://eval.in/916856

I'm having some difficulties in changing XML Node values with PHP.

My XML is the following

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
            <ProcessTransaction
                  xmlns="http://example.com">
                  <TransactionRequest
                        xmlns="http://example.com">
                        <Header>
                              <RequestType>SALE</RequestType>
                              <RequestMethod>SYNCHRONOUS</RequestMethod>
                              <MerchantInfo>
                                    <PosName>kwstasna</PosName>
                                    <PosID>1234</PosID>
                              </MerchantInfo>
                        </Header>
                  </TransactionRequest>
            </ProcessTransaction>
      </soap:Body>
</soap:Envelope> 

And i want to change PosName and PosID. The XML is received from a POST Request. If i print_r($REQUEST['xml'] I get the values in text.

And what i've tried is the following

$posid = '321';
$posname = 'nakwsta';

$result = $xml->xpath("/soap:Envelope/soap:Body/ProcessTransaction/TransactionRequest/Header/MerchantInfo");

$result[0]->PosID = $posid;
$result[0]->PosName = $posname;

echo $result;

But i get an empty array Array[]

I think my mistake is in the values of <soap:Envelope for example. Anyone that had the same issue and find out the way to solve it?

Thanks a lot for your time.

解决方案

The ProcessTransaction element (and all of its child nodes) are in the "http://example.com" namespace. If you want to access them using xpath(), you'll need to register a namespace prefix:

$xml->registerXPathNamespace('ex', 'http://example.com');

You can then use the ex prefix on all relevant parts of your query

$result = $xml->xpath("/soap:Envelope/soap:Body/ex:ProcessTransaction/ex:TransactionRequest/ex:Header/ex:MerchantInfo");

The rest of your code should function correctly, see https://eval.in/916856

这篇关于PHP 更改 XML 节点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆