PHP Simple XML GET
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页面有权使用implexml_load_file()来访问xml数据。
<?php
xml = simplexml_load_file("note.xml") or die("Error: Object Creation failure");
?>
<html>
<head>
<body>
<?php
echoxml->COURSE . "<br>";
echo xml->COUNTRY . "<br>";
echoxml->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) { echobooks->title . "<br> ";
echo books->tutor . "<br> "; echobooks->duration . "<br> ";
echo $books->price . "<hr>";
}
?>
</body>
</html>
它将产生以下结果−