基于属性打印 XML 文档的一部分?

发布于 2024-12-25 12:23:00 字数 1472 浏览 0 评论 0原文

可能的重复:
SimpleXML根据属性值获取元素内容

我看过通过几个相关主题但找不到答案。

我如何打印,例如美元货币的 curr 值?

            <?xml version="1.0"?>
            <currencies>
                <currency id="AUD">
                        <curr>Australian dollar</curr>
                        <loc>Australia, Australian Antarctic Territory, Christmas Island, Cocos (Keeling) Islands, Heard and McDonald Islands, Kiribati, Nauru, Norfolk Island, Tuvalu</loc>
                </currency>
                <currency id="USD">
                        <curr>United States Dollar</curr>
                        <loc>American Samoa, British Indian Ocean Territory, Caribbean Netherlands, Ecuador, El Salvador, Guam, Haiti, Marshall Islands, Micronesia, Northern Mariana Islands, Palau, Panama, Puerto Rico, Timor-Leste, Turks and Caicos Islands, United States, U.S. Virgin Islands, Barbados (as well as Barbados Dollar), Bermuda (as well as Bermudian Dollar), Zimbabwe</loc>
                </currency>
            </currencies>

我可以通过这样做来打印我想要的内容:

    foreach($xml as $currency) {
        echo $currency[0]->curr;
    }   

但这并不完全是我想要的。另外,我的 XML 标记是否适合我想要实现的目标,或者是否可以通过更简单的方法来实现?

Possible Duplicate:
SimpleXML get element content based on attribute value

I've looked through several related topics but can't find an answer.

How can I print, for example the value of curr for the USD currency?

            <?xml version="1.0"?>
            <currencies>
                <currency id="AUD">
                        <curr>Australian dollar</curr>
                        <loc>Australia, Australian Antarctic Territory, Christmas Island, Cocos (Keeling) Islands, Heard and McDonald Islands, Kiribati, Nauru, Norfolk Island, Tuvalu</loc>
                </currency>
                <currency id="USD">
                        <curr>United States Dollar</curr>
                        <loc>American Samoa, British Indian Ocean Territory, Caribbean Netherlands, Ecuador, El Salvador, Guam, Haiti, Marshall Islands, Micronesia, Northern Mariana Islands, Palau, Panama, Puerto Rico, Timor-Leste, Turks and Caicos Islands, United States, U.S. Virgin Islands, Barbados (as well as Barbados Dollar), Bermuda (as well as Bermudian Dollar), Zimbabwe</loc>
                </currency>
            </currencies>

I can print what I want specifically by doing this:

    foreach($xml as $currency) {
        echo $currency[0]->curr;
    }   

But it's not quite what I'm after. Also - is my XML markup okay for what I'm trying to achieve or could this be achieved an easier way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦归所梦 2025-01-01 12:23:00

使用 XPath 语法:http://php.net/manual/simplexmlelement.xpath.php

$string = '<?xml version="1.0"?>
            <currencies>
                <currency id="AUD">
                        <curr>Australian dollar</curr>
                        <loc>Australia, Australian Antarctic Territory, Christmas Island, Cocos (Keeling) Islands, Heard and McDonald Islands, Kiribati, Nauru, Norfolk Island, Tuvalu</loc>
                </currency>
                <currency id="USD">
                        <curr>United States Dollar</curr>
                        <loc>American Samoa, British Indian Ocean Territory, Caribbean Netherlands, Ecuador, El Salvador, Guam, Haiti, Marshall Islands, Micronesia, Northern Mariana Islands, Palau, Panama, Puerto Rico, Timor-Leste, Turks and Caicos Islands, United States, U.S. Virgin Islands, Barbados (as well as Barbados Dollar), Bermuda (as well as Bermudian Dollar), Zimbabwe</loc>
                </currency>
            </currencies>';

$xml = new SimpleXMLElement($string);
var_dump($xml->xpath('//currency[@id="USD"]');

Do it with the XPath syntax: http://php.net/manual/simplexmlelement.xpath.php

$string = '<?xml version="1.0"?>
            <currencies>
                <currency id="AUD">
                        <curr>Australian dollar</curr>
                        <loc>Australia, Australian Antarctic Territory, Christmas Island, Cocos (Keeling) Islands, Heard and McDonald Islands, Kiribati, Nauru, Norfolk Island, Tuvalu</loc>
                </currency>
                <currency id="USD">
                        <curr>United States Dollar</curr>
                        <loc>American Samoa, British Indian Ocean Territory, Caribbean Netherlands, Ecuador, El Salvador, Guam, Haiti, Marshall Islands, Micronesia, Northern Mariana Islands, Palau, Panama, Puerto Rico, Timor-Leste, Turks and Caicos Islands, United States, U.S. Virgin Islands, Barbados (as well as Barbados Dollar), Bermuda (as well as Bermudian Dollar), Zimbabwe</loc>
                </currency>
            </currencies>';

$xml = new SimpleXMLElement($string);
var_dump($xml->xpath('//currency[@id="USD"]');
姐不稀罕 2025-01-01 12:23:00

尝试

$data = simplexml_load_string($xml);
foreach($data->currencies as $currency) {
    // $currency->curr.. etc
]

Try

$data = simplexml_load_string($xml);
foreach($data->currencies as $currency) {
    // $currency->curr.. etc
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文