XML Get已用于从xml文件获取节点值。以下示例显示了如何从xml获取数据。
Note.xml 是xml文件,可以通过php文件访问。
<SUBJECT> <COURSE>Android</COURSE> <COUNTRY>India</COUNTRY> <COMPANY>LearnFk</COMPANY> <PRICE>$10</PRICE> </SUBJECT>
index.html 索引页有权使用simplexml_load_file()访问xml数据。
<?php $xml=simplexml_load_file("note.xml") or die("Error:ObjectCreation 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"?> <learnfk> <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> </learnfk>
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>
它将产生以下输出-
参考链接
https://www.learnfk.com/php/php-simple-xml-get.html
标签:XML,xml,php,无涯,echo,books,PHP,html From: https://blog.51cto.com/u_14033984/7178189