3

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.

2 Answers 2

8

DOMElement::getAttribute()

$intro ->getAttribute('SECTION');
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @KingCrunch... getAttribute not get_attribute... I was getting confused because of the following link... php.net/manual/en/function.domelement-get-attribute.php... thanks.
DOM and DOM XML (PHP4) are two separate extensions. See php.net/refs.xml
0

try this:

  $xml = simplexml_load_file($file);
   ....
  $attr=$intro->attributes();
  echo $attr['SECTION'];

9 Comments

Thanks for the response, but I am getting the following error: Call to undefined method DOMElement::attributes() .... Do you think there is something incorrectly set up in my PHP?
5.3... however KingCrunch has answered it... should be getAttribute not get_attribute...I was getting confused because of the following link... php.net/manual/en/function.domelement-get-attribute.php... Thanks for your help though, much appreciated... J
KingCrunch is right as you can see in your link, it says use getAttribute for php 5 otherwise I use ->attributes() recently and it works fine. it give you a array with all attributes then you only have to get what you want $attr['attribute_name']
@Xenione You are talking about SimpleXML (php.net/simplexmlelement.attributes), one more XML extension. That's slightly independent from the PHP-version ... See php.net/refs.xml too
ok, of course I forget that you have to use $xml = simplexml_load_file('myxml.xml') before; I recommmend that, the file become a php object so it's really easy to use.
|

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.