1

I am using SimpleXMLElement to parse xml in PHP and it is working fine. My xml is like,

<?xml version="1.0" encoding="ISO-8859-1"?>
<app id="123">
   <username>sample</username>
</app>

My code is,

$xml = new SimpleXMLElement(file_get_contents(file.xml));
foreach($xml->App as $product)
     {
          $user   =   $product->username;
     }

It gives correct username "sample" as output.

But now I need to get value of id inside the app tag. If I changes the xml format and gives the id inside <app> tag as <id>123</id>, I can get it simply.

But I cant change the format! Is there a way to get the id value within the same format?

1 Answer 1

3

try this:

     $xml = new SimpleXMLElement(file_get_contents(file.xml));
     foreach($xml->App as $product)
     {
          $user   =   $product->username;
          $id = $product->attributes()->id;
     }
Sign up to request clarification or add additional context in comments.

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.