2

我正在记录来自 API 调用的错误请求/响应。为了将日志存储在数据库中,我想用 * 替换信用卡号。

请求 XML 如下所示:

<xml>
    <request servicekey="service key" branchcode="branch" language="EN" postalcode="Postal Code" country="CA" email="email@example.com">
        <paxes>
            <pax id="1" birthdate="19790215" firstname="John" lastname="Smith" />
            <pax id="2" birthdate="19800828" firstname="Jane" lastname="Smith" />
        </paxes>
        <plans>
            <plan code="Plan Code" >
                <paxes>
                    <pax id="1" sumins="1200.00" />
                    <pax id="2" sumins="1480.31" />
                </paxes>
            </plan>
        </plans>
        <payments>
            <payment amount="246.24" type="VI" ccnum="4111111111111111" ccexp="0711" ccname="John Smith" />
        </payments>
    </request>
</xml>

从这里我只需获取支付节点和 ccnum 属性来编辑它(xml 保存为 $requestXML):

$_req = new DOMDocument();
$_req->loadXML($requestXML);
$_xpathReq = new DOMXPath($_req);
$_reqDom = $_xpathReq->query("/xml/request/payments/payment");
$_reqDom->item(0)->setAttribute('ccnum','*****');
$requestXML = $_req->saveXML();    

它按预期工作,xml中的CC编号被替换为*

$responseXML 有点不同:

<string xmlns="http://tempuri.org/">
    <xml>
        <request servicekey="service key" branchcode="branch code" language="EN" postalcode="Postal Code" country="CA" email="email@example.com">
            <paxes>
                <pax id="1" birthdate="19790215" firstname="John" lastname="Smith" />
                <pax id="2" birthdate="19800828" firstname="Jane" lastname="Smith" />
            </paxes>
            <plans>
                <plan code="Plan Code">
                    <paxes>
                        <pax id="1" sumins="1200.00" />
                        <pax id="2" sumins="1480.31" />
                    </paxes>
                </plan>
            </plans>
            <payments>
                <payment amount="246.24" type="VI" ccnum="4111111111111111" ccexp="0711" ccname="John Smith" />
            </payments>
        </request>
        <response>
            <errors>
                <error>Purchase Card Processing was not approved [-1]:Cancelled: null | CARD: **** | Exp: 1407 | Amount: $246.24</error>
            </errors>
        </response>
    </xml>
</string>

和以前一样的想法,我运行了一段非常相似的 PHP 代码:

$_req = new DOMDocument();
    $_req->loadXML($responseXML);
    $_xpathReq = new DOMXPath($_req);
    $_reqDom = $_xpathReq->query("/string/xml/request/payments/payment");
    $_reqDom->item(0)->setAttribute('ccnum','*****');
    $responseXML = $_req->saveXML(); 

但是当我运行它时,我得到Fatal error: Call to a member function setAttribute() on a non-object指向包含的行$_reqDom->item(0)->setAttribute('ccnum','*****');

两个 xml 之间的唯一区别是一个包含在字符串节点中,并且有一个额外的节点表示响应。我想我可以做一个解决方法(只需拉出响应节点并将其附加到请求 xml),但我现在正处于让这种方式工作是我的使命的地步。

4

3 回答 3

2

问题是由于响应 xml 中的命名空间。您必须使用 XPath 选择器注册 rootNamespace。以下代码将起作用:

$response = new DOMDocument();
$response->loadXML($responseXml);
$selector = new DOMXPath($response);

// get the root namespace
$rootNamespace = $response->lookupNamespaceUri(
    $response->namespaceURI
);
// and register it with $selector. I'm using 'x' as the prefix
// you are free to use a different prefix
$selector->registerNamespace('x', $rootNamespace);

// You won't need to specify the whole path. You could just grab 
// all payment node regardless of their location in the xml tree
$result = $selector->query('//x:payment');
foreach($result as $node) {
    $node->setAttribute('ccnum', '*****');
}

$responseXml = $response->saveXML();
echo $responseXml;
于 2013-01-14T21:21:59.947 回答
1

<string>标签包含一个命名空间,您需要使用 DOMXPath 注册该命名空间。

$_req = new DOMDocument();
$_req->loadXML($responseXML);
$_xpathReq = new DOMXPath($_req);

$_xmlns = $_req->lookupNamespaceUri(NULL); 
$_xpathReq->registerNamespace('x', $_xmlns); 

$_reqDom = $_xpathReq->query("//x:xml/x:request/x:payments/x:payment");
$_reqDom->item(0)->setAttribute('ccnum','*****');
$responseXML = $_req->saveXML(); 
于 2013-01-14T21:21:49.600 回答
0

我有一个类似的问题,其中 xml 定义了几个命名空间,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
  <feed xmlns="http://www.w3.org/2005/Atom" 
        xmlns:dc="http://purl.org/dc/elements/1.1/" 
        xmlns:dcterms="http://purl.org/dc/terms/" 
        xmlns:media="http://search.yahoo.com/mrss/" 
        xmlns:xhtml="http://www.w3.org/1999/xhtml">

这使得无法进行DOMXPath查询。

通过以下方式导入 xml 进行修复:

libxml_use_internal_errors(true);
$doc->loadHTML($rawXml, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD | LIBXML_NSCLEAN);
libxml_use_internal_errors(false);

我希望这可以帮助别人。用这个把我的头发扯掉了...

于 2016-06-08T19:58:46.070 回答