INDEX

PHP - xml file - How to get the value of the attribute

PHP - xml file - How to get the value of the attribute

(Source : test1.xml)
< ?xml version="1.0" encoding="utf-8"?>
< books>
  < book>
    < title author="dojeun">This is sample source.< /title>
    < contents>This book is good.< /contents>
  < /book>
< /books>
-----------------------------------------
(PHP code)
< ?
$xml = "test1.xml";
$xmlData = simplexml_load_file($xml);

foreach ($xmlData->book as $book) {
    echo $book->title, '< br />';
    //-------------------------------------------------
    echo $book->title->attributes()->author, '< br />';
    //-------------------------------------------------
    echo $book->contents, '< br />';
}
?>
-----------------------------------------
(Result)
This is sample source.
dojeun
This book is good.



Copyright(c) 2007-2023 coding.dojeun.com All Rights Reserved.