如何获得php中的xml节点的属性值?

时间:2022-08-25 21:53:24

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.

我使用simplexml读取xml文件。到目前为止,我无法获得我要找的属性值。这是我的代码。

          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

我的xml文件是这样的。

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

2 个解决方案

#1


3  

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) :

您可以使用它(使用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...)

P.S.并尝试接受@TJHeuvel所述的答案;这表明你尊重社区(下次,社区会非常乐意帮助你)。

#2


1  

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!!!

实际上,我脑子里的问题包括删除一个节点,错误地我不能添加它。所以在我看来,这是一个完整的答案,如果有人觉得这个有用的话。这个答案不包括SimpleXMLElement类,因为我尝试了它没有删除节点();。回到我原来的地方,我终于找到了答案。这是我的代码。和它的简单! ! !

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);

#1


3  

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) :

您可以使用它(使用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...)

P.S.并尝试接受@TJHeuvel所述的答案;这表明你尊重社区(下次,社区会非常乐意帮助你)。

#2


1  

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!!!

实际上,我脑子里的问题包括删除一个节点,错误地我不能添加它。所以在我看来,这是一个完整的答案,如果有人觉得这个有用的话。这个答案不包括SimpleXMLElement类,因为我尝试了它没有删除节点();。回到我原来的地方,我终于找到了答案。这是我的代码。和它的简单! ! !

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);