1

我有一个来自外部服务器的 xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Response ResponseReference="200002">
    <ResponseDetails Language="en">
        <SearchCurrencyResponse>
            <CurrencyDetails>
                <Currency Code="ARS"><![CDATA[Argentine Peso]]></Currency>
                <Currency Code="AUD"><![CDATA[Australian Dollar]]></Currency>
        </CurrencyDetails>
        </SearchCurrencyResponse>
    </ResponseDetails>
</Response>

获取所有货币(里面的代码和文本)的正确方法是什么?

我已经尝试过这种方式并且可以工作,但是有没有办法让它更快?因为我可以拥有数千个元素:

$searchCurrencyReponseElements = $xpath->query( 'ResponseDetails/SearchCurrencyResponse', $responseElement );
foreach( $searchCurrencyReponseElements as $searchCurrencyReponseElement ) {
     $CurrencyElements = $xpath->query( 'CurrencyDetails/Currency', $searchCurrencyReponseElement );
     foreach( $CurrencyElements as $CurrencyElement ) {
          echo '<p>Currency: '.$CurrencyElement->textContent.'</p>';
     }
}

以及如何取代码属性?谢谢

4

1 回答 1

0

如果您需要按特定节点名称选择所有节点,请使用DOMDocument::getElementsByTagName(). 尝试这个:

$doc = new DOMDocument();
$doc->load($path);
// get all Currency nodes
$currencyNodes = $doc->getElementsByTagName('Currency');
// iterate over them
foreach($currencyNodes as $node) {
    echo $node->getAttribute('Code') . ' ' . $node->nodeValue . PHP_EOL;
}
于 2013-09-14T09:24:25.073 回答