PHP 从 XML 文件加载不需要的元素

发布于 2024-11-26 15:48:00 字数 1844 浏览 0 评论 0原文

我不想使用此函数从 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 技术交流群。

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

发布评论

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

评论(2

青衫负雪 2024-12-03 15:48:00

XML 解析器并不能神奇地知道您想要加载哪些元素以及不加载哪些元素 - 您必须自己过滤这些元素。然后,您必须决定在您发布的 getElements 函数中或您所称的“查询循环”中的何处过滤所需的元素。

getElements 是否应该是某种必须返回所有元素的通用函数?然后你应该将该检查 if($v->nodeValue > 0) 更改为类似 if(!empty($v->nodeValue)) 否则你不会得到“holyPercent”值,因为它是负数(并且旧表达式变为假)。

然后在“查询循环”中,只需选择所需的元素:

foreach($elements as $element => $value) {
    if(in_array($element, array("physicalPercent", "icePercent", "holyPercent"))) {
        $elN++;
        $elements_string .= $element . ":".$value;
        if($elC != $elN)
            $elements_string .= ", ";
    }
}

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 check if($v->nodeValue > 0) to something like if(!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:

foreach($elements as $element => $value) {
    if(in_array($element, array("physicalPercent", "icePercent", "holyPercent"))) {
        $elN++;
        $elements_string .= $element . ":".$value;
        if($elC != $elN)
            $elements_string .= ", ";
    }
}
月寒剑心 2024-12-03 15:48:00

只是:

$xml = new SimpleXMLElement($xmlfile);

然后:

for($i=1;$i<Count($xml->elements);$i++)
    echo $xml->elements[$i][0];

没有尝试它是否适用于[0],通常我使用:

    echo $xml->elements[$i]['attributename'];

Just:

$xml = new SimpleXMLElement($xmlfile);

And then:

for($i=1;$i<Count($xml->elements);$i++)
    echo $xml->elements[$i][0];

Didn't try if it works with [0], usually i use :

    echo $xml->elements[$i]['attributename'];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文