1

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

2 Answers 2

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

$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...)

Sign up to request clarification or add additional context in comments.

22 Comments

thanks i dont understand how to get the attribute value from your code. And i have posted a wrong xml code please check the edit.
@guitarlass What is it that you don't understand? Just copy-paste the function I sent you, and use it as I've shown you (to get a specific element). if you want to get /presets/preset/name (= the full path to your element), then use it like $data = getByPattern("/presets/preset/name",$xml). Simple as that. :-)
yes i undertand that but, how can i get the attribute code 's value ?
@guitarlass if you have the element stored in $data, then to get the code attribute, just try : $data->getAttribute("code").
ok thanks but why doesn't my code working? i have coded $name = $pname->getAttribute('code');
|
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!!!

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.