2010-07-08 3 views
1

안녕하세요 저는 XML을 구문 분석하는 클래스를 사용하고 있습니다.XML 파서 재설정 PHP

요청 XML과 응답 XML을 구문 분석해야하므로 동일한 구문 분석기로 처리하고 싶습니다. 응답 XML을 구문 분석 할 때 계속 junk after document element 오류가 발생합니다.

XML을 <?xml version="1.0" encoding="UTF-8" ?><check></check>으로 줄였으며 여전히 오류가 발생했습니다. 따라서 생각할 수있는 유일한 점은 두 번째 <?xml ..?> 헤더가 '정크'로 구문 분석된다는 것입니다.

기본적으로 XML 파서를 새 문서로 시작할 수 있도록 재설정하지만 새 파서 개체를 만들지는 않습니다. 이것이 가능한가?

편집 : 내 XmlParser가 객체에 다음 코드를 사용하고

. 이 새 문서로 시작할 수 있지만, 나는 새로운 파서 객체를 생성하지 않도록 기본적으로 내가하고 싶은 무엇

<?php 
    class XmlComponent extends Object { 
     private $parser, $c, $current_tag; 
     public $contents; 

     function initialize(&$controller, $settings = array()) 
     { 
      $this->controller =& $controller; 
      $this->parser = xml_parser_create(); 
      xml_set_object($this->parser, $this); 
      xml_set_element_handler($this->parser, "tag_open", "tag_close"); 
      xml_set_character_data_handler($this->parser, "cdata"); 
      xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); 

      if (isset($settings['xml'])) { 
       $contents = $settings['xml']; 
      } 
      if (isset($settings['file'])) { 
       $f = fopen($settings['file'], 'r'); 
       $contents = fread($f, filesize($settings['file'])); 
       fclose($f); 
      } 

      debug($this->parse($contents)); 

     } 

     public function parse($xml) 
     { 

      $xml = trim(preg_replace("/>\s+</", '><', $xml)); 
      $this->contents = array(); 
      $this->c = &$this->contents; 
      xml_parse($this->parser, $xml); 
      return xml_error_string(xml_get_error_code($this->parser))."\r\n".$xml; 
     } 

     //*/ 
     public function xml($root) 
     { 
      $xml = ''; 
      foreach ($root as $tag=>$elem) { 
       if (substr($tag, 0,1) != '@') { 
        foreach ($elem as $val) { 
         $xml .= '<'.$tag; 
         if (is_array($val) && isset($val['@attributes'])) { 
          foreach($val['@attributes'] as $a_key => $a_val) { 
           $xml .= ' '.$a_key.'="'.$a_val.'"'; 
          } 
         } 
         $xml .= '>'; 
         if (is_array($val) && isset($val['@data'])) { 
          $xml .= $val['@data']; 
         } 
         $xml .= $this->xml($val); 
         $xml .= '</'.$tag.'>'; 
        } 
       } 
      } 

      return $xml; 
     } 
     //*/ 

     private function tag_open($parser, $tag, $attributes) 
     { 
      if (!empty($attributes)) { $this->c[$tag][] = array('@attributes' => $attributes, '@parent' => &$this->c); } 
      else { $this->c[$tag][] = array('@parent' => &$this->c); } 

      $this->c =& $this->c[$tag][count($this->c[$tag]) - 1]; 

     } 

     private function tag_close($parser, $tag) 
     { 
      $parent = &$this->c['@parent']; 
      unset($this->c['@parent']); 
      $this->c =& $parent; 
     } 

     private function cdata($parser, $data) 
     { 
      if (!empty($data)) {$this->c['@data'] = $data;} 
     } 
     //*/ 

    } 
?> 
+0

새 개체를 만드는 것이 잘못된 이유는 무엇입니까? – deceze

+2

사용하는 파서에 대한 자세한 정보를 제공 할 수 있습니다. 그렇지 않으면, 우리가 그것이 당신이 요구하는 것에 능력이 있는지를 어떻게 알 수 있습니까? – Gordon

+0

일부 코드가 도움이 될 것입니다. –

답변

2

은 XML 파서를 재설정합니다. 이것이 가능한가?

아니요, 새 파서를 만들어야합니다. 물론 파서를 "재설정"하는 메서드를 클래스에 제공 할 수 있습니다. 즉 현재의 것을 삭제하고 새로운 것을 생성하므로 클래스 소비자로부터 그것을 숨 깁니다.

+0

고마워, 내가 그랬어. – Rob