0

I have a file like this :

<Student>
<Karishma id="2" roll="3" />
<Sakshi id="3" rol="4" />
</Student>

This is just an example file. I have a file similar to this, and I want to read the tag names " Karishma and Sakshi as I don't know them beforehand. Student tag is known to me. How do I do this? Please help!!

2 Answers2

2

This will output the name of your tag if it's what you need (Karishma and Sakshi):

<?php

$xml = '<Student>
<Karishma id="2" roll="3" />
<Sakshi id="3" rol="4" />
</Student>';

$simpleXmlElement = new SimpleXMLElement($xml);

foreach ($simpleXmlElement->children() as $tagName => $element) {
    echo $tagName;
}
Matthieu
  • 948
  • 8
  • 8
1

You can do it like this

$xml = simplexml_load_file('file.xml');

foreach($xml->children() AS $tagname => $value) {
  echo $tagname;
}
vhen
  • 105
  • 12