PHP iterates through the XML data within the specified folder, 100 points, 100 points.

Source: Internet
Author: User
Tags cdata glob
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
...
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.