How can i get the value of attribute in of a xml node in php?

℡╲_俬逩灬. 提交于 2019-12-11 12:18:13

问题


I'm using simplexml to read a xml file. So far i'm unable to get the attribute value i'm looking for. this is my code.

          if(file_exists($xmlfile)){
              $doc = new DOMDocument();
              $doc->load($xmlfile);
              $usergroup = $doc->getElementsByTagName( "preset" );
              foreach($usergroup as $group){         
                 $pname = $group->getElementsByTagName( "name" );
                 $att = 'code';
                 $name = $pname->attributes()->$att; //not working

                 $name = $pname->getAttribute('code'); //not working
                 if($name==$preset_name){
                     echo($name);
                      $group->parentNode->removeChild($group);
                 }
              }
          }

and my xml file looks like

<presets>
<preset>
 <name code="default">Default</name>
  <createdBy>named</createdBy>
  <icons>somethignhere</icons>
 </preset>
</presets>

回答1:


Try this :

function getByPattern($pattern, $source)
{
    $dom = new DOMDocument();
    @$dom->loadHTML($source);

    $xpath = new DOMXPath($dom);
    $result = $xpath->evaluate($pattern);

    return $result;
}

And you may use it like (using XPath) :

$data = getByPattern("/regions/testclass1/presets/preset",$xml);

UPDATE


Code :

<?php
    $xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><presets><preset><name code=\"default\">Default</name><createdBy>named</createdBy><icons>somethignhere</icons></preset></presets>";

    $xml = new SimpleXMLElement($xmlstr);

    $result = $xml->xpath("/presets/preset/name");

    foreach($result[0]->attributes() as $a => $b) {
        echo $a,'="',$b,"\"\n";
    }

?>

Output :

code="default"

P.S. And also try accepting answers as @TJHeuvel mentioned; it's an indication that you respect the community (and the community will be more than happy to help you more, next time...)




回答2:


Actually question in my head includes deleting a node as well , mistakenly i could not add it. So in my point of view this is the complete answer, i a case if someone else find this useful. This answer doesn't include SimpleXMLElement class because how hard i tried it didn't delete the node with unset(); . So back to where i was , i finally found an answer. This is my code. and its Simple!!!

if(file_exists($xmlfile)){
              $doc = new DOMDocument();
              $doc->load($xmlfile);
              $presetgroup = $doc->getElementsByTagName( "preset" );
              foreach($presetgroup as $group){       
                 $pname = $group->getElementsByTagName( "name" );
                  $pcode = $pname->item(0)->getAttribute('code');
                 if($pcode==$preset_name){
                      echo($preset_name);
                      $group->parentNode->removeChild($group);
                 }
              }
          }
        $doc->save($xmlfile);


来源:https://stackoverflow.com/questions/10347715/how-can-i-get-the-value-of-attribute-in-of-a-xml-node-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!