PHP 从 XML 文件加载不需要的元素
我不想使用此函数从 XML 文件加载一些数据:
public function getElements()
{
$elements = array();
$element = $this->documentElement->getElementsByTagName('elements')->item(0);
// checks if it has any immunities
if( isset($element) )
{
// read all immunities
foreach( $element->getElementsByTagName('element') as $v)
{
$v = $v->attributes->item(0);
// checks if immunity is set
if($v->nodeValue > 0)
{
$elements[$v->nodeName] = $v->nodeValue;
}
}
}
return $elements;
}
我不想从 XML 文件加载该元素:
<elements>
<element physicalPercent="10"/>
<element icePercent="10"/>
<element holyPercent="-10"/>
</elements>
我不想仅加载元素节点名称和节点值。
在我的查询循环中得到了这个代码:
$elements = $monster->getElements();
$elN = 0;
$elC = count($elements);
if(!empty($elements)) {
foreach($elements as $element => $value) {
$elN++;
$elements_string .= $element . ":".$value;
if($elC != $elN)
$elements_string .= ", ";
}
}
最后 - $elements_string 变量的输出是错误的:
earthPercent:50, holyPercent:50, firePercent:15, energyPercent:5, physicalPercent:25, icePercent:30, deathPercent:30firePercent:20, earthPercent:75firePercent:20, earthPercent:75firePercent:20, earthPercent:75physicalPercent:70, holyPercent:20, deathPerce
它应该而是返回:
physicalPercent:10,icePercent:10,holyPercent:-10
您能再帮我一次吗?:)
先谢谢您了。
I wan't to load some data from my XML file using this function:
public function getElements()
{
$elements = array();
$element = $this->documentElement->getElementsByTagName('elements')->item(0);
// checks if it has any immunities
if( isset($element) )
{
// read all immunities
foreach( $element->getElementsByTagName('element') as $v)
{
$v = $v->attributes->item(0);
// checks if immunity is set
if($v->nodeValue > 0)
{
$elements[$v->nodeName] = $v->nodeValue;
}
}
}
return $elements;
}
I wan't to load that elements from my XML file:
<elements>
<element physicalPercent="10"/>
<element icePercent="10"/>
<element holyPercent="-10"/>
</elements>
I wan't to load only element node name and node value.
Got this code in my query loop:
$elements = $monster->getElements();
$elN = 0;
$elC = count($elements);
if(!empty($elements)) {
foreach($elements as $element => $value) {
$elN++;
$elements_string .= $element . ":".$value;
if($elC != $elN)
$elements_string .= ", ";
}
}
And finally - the output of $elements_string variable is wrong:
earthPercent:50, holyPercent:50, firePercent:15, energyPercent:5, physicalPercent:25, icePercent:30, deathPercent:30firePercent:20, earthPercent:75firePercent:20, earthPercent:75firePercent:20, earthPercent:75physicalPercent:70, holyPercent:20, deathPerce
It should rather return:
physicalPercent:10, icePercent:10, holyPercent:-10
Could you help me one more time?:)
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 解析器并不能神奇地知道您想要加载哪些元素以及不加载哪些元素 - 您必须自己过滤这些元素。然后,您必须决定在您发布的 getElements 函数中或您所称的“查询循环”中的何处过滤所需的元素。
getElements
是否应该是某种必须返回所有元素的通用函数?然后你应该将该检查if($v->nodeValue > 0)
更改为类似if(!empty($v->nodeValue))
否则你不会得到“holyPercent”值,因为它是负数(并且旧表达式变为假)。然后在“查询循环”中,只需选择所需的元素:
Well the XML-Parser doesn't magically know which elements you want to load and which you won't - you have to filter this by yourself. Then you have to decide where you want to filter your desired elements in the
getElements
-function you posted or in your "query loop" as you call it.Should the
getElements
be some kind of general function which must return all elements? Then you should change that checkif($v->nodeValue > 0)
to something likeif(!empty($v->nodeValue))
otherwise you wont get the "holyPercent" value since this is negative (and the old expression becomes false).Then in your "query loop", just select your desired elements:
只是:
然后:
没有尝试它是否适用于[0],通常我使用:
Just:
And then:
Didn't try if it works with [0], usually i use :