基于属性打印 XML 文档的一部分?
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 XPath 语法:http://php.net/manual/simplexmlelement.xpath.php
Do it with the XPath syntax: http://php.net/manual/simplexmlelement.xpath.php
尝试
Try