XML : PHP importnode() from SimpleXML to DOM

I have two tables $attributes and $frqTable. For ShipAddress node I have an array. As there is an array, I want to get two ShipAddress- one for index 0 and another for index 1. for example-

  <ShipAddress>   <Addr1>Kristy Abercrombie</Addr1>   <Addr2>5647 Cypress Bill Rd</Addr2>   <City>Bayshore</City>   <State>CA</State>   <PostCode>94326</PostCode>   </ShipAddress>      <ShipAddress>    <Addr1>RHP</Addr1>    <Addr2>170, North</Addr2>    <City>Dhaka</City>    <PostCode>1219</PostCode>  </ShipAddress>    

But unfortunately I get this-

   <ShipAddress>     <CustomerAdd>       <Addr1>Kristy Abercrombie</Addr1>       <Addr2>5647 Cypress Bill Rd</Addr2>       <City>Bayshore</City>       <State>CA</State>       <PostCode>94326</PostCode>     </CustomerAdd>    <1>     <Addr1>RHP</Addr1>     <Addr2>170, North</Addr2>     <City>Dhaka</City>     <PostCode>1219</PostCode>   </1>    

This ShipAddress node is to be selected from $frqTable as it gives a possible indication of an array existence.

  <?php  $attributes=array(      'Customer'=>array(          'Name'=>'Abercrombie, Kristie',          'Salutation'=>'Mrs',          'FirstName'=>'Kristy',          'LastName'=>'Abercrombie',          'BillAddress'=>array(              'Addr1'=>'Kristy Abercrombie',              'Addr2'=>'5647 Cypress Bill Rd',              'City'=>'Bayshore',              'State'=>'CA',              'PostCode'=>'94326'          ),          'ShipAddress'=>array(              0=>array(                  'Addr1'=>'Kristy Abercrombie',                  'Addr2'=>'5647 Cypress Bill Rd',                  'City'=>'Bayshore',                  'State'=>'CA',                  'PostCode'=>'94326'              ),              1=>array(                  'Addr1'=>'RHP',                  'Addr2'=>'170, North',                  'City'=>'Dhaka',                  'PostCode'=>'1219'              )          ),          'Email'=>'kristy@samplename.com',          'Contact'=>'Kristy Abercrombie',          'AltContact'=>'Steve Darcangelo',          'AccountNumber'=>'91-431',          'JobStartDate'=>'2013-09-23',          'JobProjectedEndDate'=>'2013-09-23',          'JobEndDate'=>'2013-09-23',          'Notes'=>'Sample',          'PaymentDeliveryMethod'=>'Email'      )  );    $frqTable=array(      'Customer'=>array(          'ShipAddress'=>null,          'AdditionalContactRef'=>null,          'Contacts'=>array(              'AdditionalContactRef'=>null          )      )  );  $dom = new DOMDocument('1.0', 'utf-8');  $dom->formatOutput=true;    $XMLSimpleObject=array2xml($attributes);  var_dump($XMLSimpleObject);  $XMLDOMElement=dom_import_simplexml($XMLSimpleObject);  $XMLDOMDocument=$XMLDOMElement->ownerDocument;    $importingNode=$XMLDOMDocument->getElementsByTagName('CustomerAdd')->item(0);  $importedNode=$dom->importNode($importingNode, true);    $dom->appendChild($importedNode);    print($dom->saveXML());    function array2xml($array, $xml = false){      if($xml === false){          $xml = new SimpleXMLElement('<root/>');      }      foreach($array as $key => $value){          if($key=='Customer'){              $key='CustomerAdd';          }          if(is_array($value)){              array2xml($value, $xml->addChild($key));              }else{              $xml->addChild($key, $value);          }      }             return $xml;      //return $xml->asXML();  }    

No comments:

Post a Comment