XML parsing conundrum

后端 未结 7 1398
甜味超标
甜味超标 2021-01-15 22:25

UPDATE: I\'ve reworked the question, to show progress I\'ve made, and maybe make it easier to answer.

UPDATE 2: I\'ve added another value to the XML. Extension avail

7条回答
  •  醉梦人生
    2021-01-15 22:53

    How's something like this? Code is a bit sloppy, and tweaks should probably be made to improve the validation.

    class XMLFileImporter {
      public $file; //Absolute path to import file
      public $import = array();
      public $xml;
      public $error = false;
    
      public function __construct($file) {
        $this->file = $file;
        $this->load();
      }
    
      public function load() {
        if(!is_readable($this->file)) {
          $this->error("File is not readable");
          return false;
        }
    
        $xml = simplexml_load_file($this->file);
        if(!$xml) {
          $this->error("XML could not be parsed");
          return false;
        }
        $this->xml = json_decode(json_encode($xml));
    
        return true;
      }
    
      public function import() {
        $count = $this->parseItems();
        echo "Imported $count rows";
    
      }
    
      public function parseItems() {
        if($this->error()){
          return false;
        }
    
        if(!self::validateXML($this->xml)) {
          $this->error("Invalid SimpleXML object");
          return false;
        }
    
        if(!self::validateArray($this->xml->Item)) {
          $this->error("Invalid Array 'Item' on SimpleXML object");
          return false;
        }
        $count = 0;
        foreach($this->xml->Item as $item) {
          if($this->parseItem($item)){
            $count++;
          }
        }
        return $count;
    
      }
      public function parseItem($item) {
        if($this->error()){
          return false;
        }
    
        if(!self::validateItem($item)) {
          $this->error("Invalid file item");
          return false;
        }
    
        $item = self::normalizeItem($item);
    
        $this->handlePlatform((string)$item->Platform);
        $this->handleGroup($item);
        $this->handleSubGroup($item);
        $this->handleFile($item);
        return true;
      }
    
      public function handlePlatform($platform) {
        if(!isset($this->import[$platform])) {
          $this->import[$platform] = array();
        }
    
        return true;
      }
    
      public function handleGroup($item) {
        if(!isset($this->import[$item->Platform][$item->Name])) {
          $this->import[$item->Platform][$item->Name] = array();
        }
        return true;
      }
    
      public function handleSubGroup($item) {
        if(!isset($this->import[$item->Platform][$item->Name][$item->Title])) {
          $this->import[$item->Platform][$item->Name][$item->Title] = array();
        }
        return true;
      }
    
      public function handleFile($item) {
        array_push($this->import[$item->Platform][$item->Name][$item->Title],$item->DownloadPath);
      }
    
      public function error($set=false) {
        if($set){
          $this->error = $set;
          return true;
        }
        return $this->error;
      }
    
      public static function validateXML($xml) {
        return is_object($xml);
      }
      public static function validateArray($arr,$min=1){
        return (isset($arr) && !empty($arr) && count($arr) > $min);
    
      }
    
      public static function validateItem($item){
        return (isset($item->Title)
               && isset($item->Name)
               && isset($item->DownloadPath)
               && isset($item->Platform));
    
      }
    
      public static function normalizeItem($item){
        $item->Name = stripslashes(trim((string)$item->Name));
        $item->Title = stripslashes(trim((string)$item->Title));
        $item->Platform = (string)$item->Platform;
        $item->DownloadPath = (string)$item->DownloadPath;
    
        return $item;
      }
    
      public function output() {
        print_r($this->import);
        return true;
      }
    
    }
    
    $importer = new XMLFileImporter(dirname(__FILE__)."/files.xml");
    $importer->load();
    $importer->import();
    $importer->output();
    var_dump($importer->error());
    

提交回复
热议问题