PHP iterates through the XML files within the specified folder, 100 points, 100 points.
The XML file format is:
<! [cdata["Crude a" trailer]]>
130112001.jpg
《克鲁德一家》预告
130112001.3gp
An XML file is equivalent to a single piece of data.
As soon as I beginner PHP, I beg to translate the following code ...
Specify folder directory path: D:\001\1003 (there are multiple XML files under this folder)
Traversal starts (reads 4 XML files, stops traversal after satisfying)
Read the contents of an XML file
$title title within the =xml
$img image within the =xml
$path = path to this XML file
Output these 3 variables to a page
Traversal end
is equal to reading 4 data, each containing the values of the 3 variables of Title,img,path.
PHP XML
------Solution--------------------
Turn manual, Opendir traversal, DOMDocument parse XML
------Solution--------------------
$i = = 0
foreach (Glob ("D:\\001\\1003\\*.xml") as $filename) {
if ($i >3) break;
Read $filename ...
$i + +;
}
Read part of $filename reference http://bbs.csdn.net/topics/390354196
Too lazy to write
------Solution--------------------
$dir = ' D:\001\1003 ';
function ReadXml ($dir,& $arr) {
if ($handle =opendir ($dir)) {
while (false!== ($file =readdir ($handle))) {
$sub _dir= $dir. Directory_separator. $file;
if (Is_dir ($sub _dir) && $file! = '. ' && $file! = ' ... ') {
ReadXml ($sub _dir, $arr);
}elseif (Preg_match ('/\.xml$/', $file)) {
if (count ($arr) >3) {
return false;
}
$arr []= $sub _dir;
}
}
}
}
ReadXml ($dir, $arr);
foreach ($arr as $x) {
$doc = new DOMDocument ();
$doc->load ($x);
$tmp = $doc->getelementsbytagname (' template ');
foreach ($tmp as $t) {
Echo ' title: ' $t->getelementsbytagname (' title ')->item (0)->nodevalue. "
";
Echo ' Image: '. $t->getelementsbytagname (' image ')->item (0)->nodevalue. "
";
Echo ' intro: '. $t->getelementsbytagname (' intro ')->item (0)->nodevalue. "
";
Echo ' MediaPath: '. $t->getelementsbytagname (' MediaPath ')->item (0)->nodevalue. "
";
}
Echo "--------------------------------------
";
}
Give points!!!
------Solution--------------------
It's really clear, dizzy.
foreach (Glob ("D:\\001\\1003\\*.xml") as $key + = $filename)
With $key can judge read four stop traversal, do not need $i
References:
PHP code?123456$i = = 0foreach (Glob ("D:\\001\\1003\\*.xml") as $filename) {if ($i >3) break; Read $filename ... $i + + ;}
Read part of $filename reference http://bbs.csdn.net/topics/390354196
Too lazy to write
...