XML Get已经用于从xml文件中获取节点值。以下示例显示如何从xml获取数据。
Note.xml是xml文件,可以通过php文件访问。
<SUBJECT> <COURSE>Android</COURSE> <COUNTRY>India</COUNTRY> <COMPANY>TutorialsPoint</COMPANY> <PRICE>$10</PRICE> </SUBJECT>
索引页有权通过使用implexml_load_file()来访问xml数据。
<?php $xml = simplexml_load_file("note.xml") or die("Error: Object Creation failure"); ?> <html> <head> <body> <?php echo $xml->COURSE . "<br>"; echo $xml->COUNTRY . "<br>"; echo $xml->COMPANY . "<br>"; echo $xml->PRICE; ?> </body> </head> </html>
输出结果如下 -
以下代码具有如何从xml文件获取节点值的信息,XML应如下所示:
<?xml version = "1.0" encoding = "utf-8"?> <vue5> <course category = "JAVA"> <title lang = "en">Java</title> <tutor>Gopal</tutor> <duration></duration> <price>$30</price> </course> <course category = "HADOOP"> <title lang = "en">Hadoop</title>. <tutor>Satish</tutor> <duration>3>/duration> <price>$50</price> </course> <course category = "HTML"> <title lang = "en">html</title> <tutor>raju</tutor> <duration>5</duration> <price>$50</price> </course> <course category = "WEB"> <title lang = "en">Web Technologies</title> <tutor>Javed</tutor> <duration>10</duration> <price>$60</price> </course> </tutorialspoint>
PHP代码应该如下
<html> <body> <?php $xml = simplexml_load_file("books.xml") or die("Error: Cannot create object"); foreach($xml->children() as $books) { echo $books->title . "<br> "; echo $books->tutor . "<br> "; echo $books->duration . "<br> "; echo $books->price . "<hr>"; } ?> </body> </html>
输出结果如下 -