XML Get 用于从 xml 文件中获取节点值。以下示例显示如何从 xml 中获取数据。
Note.xml
Note.xml 是 xml 文件, 它可以通过php文件访问。
<SUBJECT> <COURSE>Android</COURSE> <COUNTRY>India</COUNTRY> <COMPANY>TutorialsPoint</COMPANY> <PRICE>$10</PRICE> </SUBJECT>
Index.htm
Index page 索引页面有权通过使用 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"?> <tutorialspoint> <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>
它将产生以下结果 –
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!