Created
November 4, 2019 04:47
-
-
Save Guley/c8de9639bf4140cea96da54c33ec42cb to your computer and use it in GitHub Desktop.
Codeigniter Genrate XML Envelope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') OR exit('No direct script access allowed'); | |
class XML extends CI_Controller { | |
protected $xml; | |
public function __construct() { | |
parent::__construct(); | |
$this->xml = new DOMDocument('1.0', 'utf8'); | |
$this->xml->formatOutput = true; | |
$this->xml->preserveWhiteSpace = false; | |
} | |
public function index(){ | |
$startTag1 = ['field1'=>'field1','field2'=>'field2','field3'=>'field3']; | |
$startTag2 = [ | |
['field1'=>'field1','field2'=>'field2','field3'=>'field3'], | |
['field1'=>'field1','field2'=>'field2','field3'=>'field3'], | |
['field1'=>'field1','field2'=>'field2','field3'=>'field3'], | |
]; | |
$this->genreateXml($startTag1,$startTag2); | |
} | |
public function genreateXml($startTag1,$startTag2){ | |
$startTag1 = $this->xml->createElement('startTag1'); | |
$this->add_dom_child($startTag1, 'Field 1', $startTag1['field1']); | |
$this->add_dom_child($startTag1, 'Field 2', $startTag1['field2']); | |
$this->add_dom_child($startTag1, 'Field 3', '$startTag1['field3']'); | |
$this->xml->appendChild($startTag1); | |
$startTag2 = $this->xml->createElement('startTag2'); | |
if(!empty($startTag2)){ | |
foreach ($startTag2 as $key => $startTag) { | |
$startTag2NodeName = $this->xml->createElement('startTag2NodeName'); | |
$this->add_dom_child($startTag2NodeName, 'Field1',$startTag['field1']); | |
$this->add_dom_child($startTag2NodeName, 'Field2', $startTag['field2']); | |
$startTag2->appendChild($startTag2NodeName); | |
} | |
$this->xml->appendChild($startTag2); | |
} | |
$xmlString = $this->xml->saveXML(); | |
$xmlString = substr($xmlString, strpos($xmlString, 'startTag1')); | |
/*Obejct to send with request*/ | |
$data_obj = new SoapVar($xmlString, XSD_ANYXML); | |
} | |
private function add_dom_child (&$parent, $tag, $value, $type = 'text') { | |
$node = $this->xml->createElement($tag); | |
$node = $parent->appendChild($node); | |
$value = trim(preg_replace('/\s+/', '',$value)); | |
switch($type) { | |
case 'cdata': | |
$contxt = $this->xml->createCDATASection($value); | |
break; | |
case 'bool': | |
$value = ($value === true) ? 'TRUE' : 'FALSE'; | |
$contxt = $this->xml->createTextNode($value); | |
break; | |
default: | |
$contxt = $this->xml->createTextNode($value); | |
break; | |
} | |
$contxt = $node->appendChild($contxt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment