Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, XML How to get attributes?

Tags:

dom

php

xml

I am trying to return the values of the SECTION attribute of the INTRO tags from the following xml:

Sample of myxml.xml:

<INTRO SECTION="ONE">
  <TEXT>Hello</TEXT>
</INTRO>
<INTRO SECTION="TWO">
  <TEXT>Goodbye</TEXT>
</INTRO>

My PHP:

$doc = new DOMDocument();
$doc->load('myxml.xml');
$intros = $doc->getElementsByTagName("INTRO");

foreach( $intros as $intro ) {
echo $intro ->get_attribute('SECTION');
}

I am getting the following error:

Call to undefined method DOMElement::get_attribute()

Does anyone know what I am doing wrong? Kind regards to any responders.

like image 832
jon Avatar asked Feb 02 '26 07:02

jon


1 Answers

DOMElement::getAttribute()

$intro ->getAttribute('SECTION');
like image 100
KingCrunch Avatar answered Feb 03 '26 21:02

KingCrunch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!