Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

parent html content included in xml output

php xml

This topic has been archived. This means that you cannot reply to this topic.
No replies to this topic

#1 yonghan79

yonghan79

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 30 May 2015 - 09:18 PM

Hi all, currently I'm trying to export array to xml file. My problem is when I export it, the current page html content was written in the xml file along with the array. This is the output file http://pastebin.com/eEFbkNUt. Sorry if I put it on pastebin as I can't upload it here and these are the code:

class XmlExport extends ReportFormatBase {
	public static function display($report, $requests=array()) {
		$file_name = preg_replace(array('/[\s]+/','/[^0-9a-zA-Z\-_\.]/'),array('_',''),$report['name']);
		
		$now = gmdate("D, d M Y H:i:s");
	    header("Pragma: no-cache");
		header("Expires: 0");
	    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
	    header("Last-Modified: {$now} GMT");

	    // force download  
	    header("Content-Type: application/force-download");
	    header("Content-Type: application/octet-stream");
	    header("Content-Type: application/download");
	    header("Content-type: application/xml");

	    // disposition / encoding on response body
	    header("Content-Disposition: attachment;filename={$file_name}.xml");
	    header("Content-Transfer-Encoding: binary");
		$data = "";
		if(isset($report['data']) && $report['data']) {
			$data = self::array2xml( $report['data'] );
		}

		if(trim($data)) echo $data;
	}

	public static function array2xml($array, $node_name="root") {
        if (count($array) == 0) {
            return null;
        }
	    $dom = new DOMDocument('1.0', 'UTF-8');
	    $dom->formatOutput = true;
	    $root = $dom->createElement($node_name);
	    $dom->appendChild($root);

	    $array2xml = function ($node, $array) use ($dom, &$array2xml) {
	        foreach($array as $key => $value){
	            if ( is_array($value) ) {
	            	if(strpos($key, "item_") === 0){
	            		$key = "item";
	            	}
	            	if(strpos($key, "month_") === 0){
	            		$key = "month";
	            	}
	                $n = $dom->createElement($key);
	                $node->appendChild($n);
	                $array2xml($n, $value);
	            }else{
	                $attr = $dom->createAttribute($key);
	                $attr->value = $value;
	                $node->appendChild($attr);
	            }
	        }
	    };

	    $array2xml($root, $array);
	    return $dom->saveXML();

	}
}
$reports = array();
        $reports['name'] = 'sale_order';
        $reports['data'] = array();
        $reports['data'] = $this->getReportData();

        if (isset($this->request->get['report_period'])) {
            $report_period = $this->request->get['report_period'];
        } else {
            $report_period = "month_";
        }

        if($reports['data']) {
            $tmp = array();
            $i = 1;
            foreach($reports['data'] as $key=>$val) {
                $tmp[$report_period.$i] = $val;
                $i++;
            }
            $reports['data'] = array();
            if($export_type == "xml") {
                $reports['data']['items'] = $tmp;
            } else {
                $reports['data'] = $tmp;
            }
        }
        $this->exportReport( $reports, array(), $export_type );
    public function exportReport( $report = array(), $requests = array(), $type = "csv") {
        $method = "export".ucfirst($type);
        if( method_exists( $this, $method ) ){
            return $this->{$method}( $report, $requests );
        }
    }

   
    public function exportXml( $report = array(), $requests = array() ) {
        require_once(DIR_EXT."advanced_reports".DIR_EXT_CORE."lib/exports/xml_export.php");
        $requests['content_html'] = $this->_export_content_html;
        XmlExport::display( $report, $requests );
 

Please point me how to remove the html content during outputting the xml content. Thanks a lot.






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download