开发者

PHP loads not wanted elements from a XML file

开发者 https://www.devze.com 2023-03-23 08:19 出处:网络
I wan\'t to load some data from my XML file using this function: public function getElements() { $elements = array();

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)
       开发者_Go百科     {
                $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.


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 .= ", ";
    }
}


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'];
0

精彩评论

暂无评论...
验证码 换一张
取 消