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 the folder directory path: D:\001\1003 (there are multiple XML files under this folder) traversal starts (reads 4 XML files, satisfies stop traversal) reads an XML file content within $title=xml Title$img=xml image$path= The path to this XML file outputs these 3 variables to the end of the page traversal
is equal to reading 4 data, each containing the values of the 3 variables of Title,img,path.
Reply to discussion (solution)
I saw the head of the head suddenly clear zero ... The micro-minister to retire ...
I saw the head of the head suddenly clear zero ... The micro-minister to retire ...
Help me to solve the next, and immediately the Avatar.
$doc = new DOMDocument (), $doc->load ("A.xml"), $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. "
";}
Turn manual, Opendir traversal, DOMDocument parse XML
$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
Well, actually wrote the Double equals sign ...
$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!!!
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
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 ...
PHP code?1234567891011121314151617181920212223242526272829$dir= ' D:\001\1003 '; function ReadXml ($dir,& $arr) {if ($handle =opendir ($dir)) {while (false!== ($file =readdir ($handle))) ...
Thank you brother, have time to talk about this code to add a few comments, I am good at learning, hehe.
There is also a function for the PHP traversal folder
You can refer to: http://www.bacysoft.cn/thread-77-1-1.html