getbookname.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. //get book name
  3. $filelist=array();
  4. $fileNums=0;
  5. $log="";
  6. $outputFileName="bookname.csv";
  7. $fp=fopen($outputFileName, "w");
  8. function getChildNodeValue($array,$attName){
  9. if($array){
  10. foreach($array as $x=>$x_value) {
  11. if($x==$attName){
  12. return $x_value;
  13. }
  14. }
  15. }
  16. return false;
  17. }
  18. if(($handle=fopen("filelist.csv",'r'))!==FALSE){
  19. while(($htmlFileName=fgetcsv($handle,0,','))!==FALSE){
  20. $FileName=$htmlFileName[1].".htm";
  21. $fileId=$htmlFileName[0];
  22. $dirLog=_DIR_LOG_."/";
  23. $dirHtml="pali/";
  24. $inputFileName=$dirHtml.$FileName;
  25. if(file_exists($inputFileName)==false){
  26. die('file ".."not exists...');
  27. }
  28. $xmlfile = $inputFileName;
  29. $xmlparser = xml_parser_create();
  30. echo "doing:".$xmlfile."<br>";
  31. // 打开文件并读取数据
  32. $fp = fopen($xmlfile, 'r');
  33. $xmldata = fread($fp,filesize($xmlfile));
  34. xml_parse_into_struct($xmlparser,$xmldata,$values);
  35. xml_parser_free($xmlparser);
  36. $begin = false;
  37. $suttaCount=0;
  38. $output="";
  39. $suttaName="";
  40. $log=$log."file:".$xmlfile."\r\n";
  41. $currNikaya="";
  42. $currBook="";
  43. foreach ($values as $child)
  44. {
  45. $attributes=getChildNodeValue($child,"attributes");
  46. switch ($child["tag"])
  47. {
  48. case "BODY":
  49. break;
  50. case "P":
  51. $class=getChildNodeValue($attributes,"CLASS");
  52. if($class=="nikaya"){
  53. $currNikaya=$child["value"];
  54. }
  55. if($class=="book"){
  56. $currBook=$child["value"];
  57. }
  58. $csvWord[0]=$htmlFileName[0];
  59. $csvWord[1]=$htmlFileName[1];
  60. $csvWord[2]=$currNikaya;
  61. $csvWord[3]=$currBook;
  62. fputcsv($fp,$csvWord);
  63. echo $currBook."<br>";
  64. break;
  65. }
  66. }
  67. $fileNums++;
  68. }
  69. }
  70. fclose($fp);
  71. ?>