Solved

Remove element tagging - $dom->createElement

Posted on 2014-10-23
6
94 Views
Last Modified: 2014-10-23
Hi,
I have a chunk from some code that creates an XML file for an purchase order, I need to alter this as now need it in TRADACOM format so all elements are just a text file. I need some pointers on how to unravel the createelement parts:

So, instead of this result:

<StartOfTransmission>
    <SenderId>1234567890:A Company</SenderId>
    <ReceiverId>5013546066966:B Company</ReceiverId>
    <TRANSDATE>23-10-2014</TRANSDATE>
    <TRANSTIME>15:51:05</TRANSTIME>
    <SenderReference>000000000000000</SenderReference>
    <ApplicationReference>ORDHDR</ApplicationReference>
  </StartOfTransmission>

I end up with:

1234567890:A company:5013546667966:B Company:23-10-2014:15:51:05:000000000000000:ORDHDR'

        //start transmission
        $sot = $dom->createElement('StartOfTransmission');
        $arr_foo = array(
              'SenderId'=>$this->_sender_id,
              'ReceiverId'=>$this->_receiver_id,
              'TRANSDATE'=>date('d-m-Y'),
              'TRANSTIME'=>date('H:i:s'),
              'SenderReference'=>'000000000000000',
              //'ReceiverReference'=>'',
              'ApplicationReference'=>'ORDHDR',
              );
        foreach ($arr_foo as $key => $value) {
            $item = $dom->createElement($key, $value);
            $sot->appendChild($item);  
        }
        
        $element->appendChild($sot);
        unset($sot);
        //end trammission

Open in new window


Hope someone can help?
0
Comment
Question by:tonypearce
  • 3
  • 3
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40399954
Please see http://iconoun.com/demo/temp_tonypearce.php

<?php // demo/temp_tonypearce.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28543344.html

$raw = <<<EOD
<StartOfTransmission>
    <SenderId>1234567890:A Company</SenderId>
    <ReceiverId>5013546066966:B Company</ReceiverId>
    <TRANSDATE>23-10-2014</TRANSDATE>
    <TRANSTIME>15:51:05</TRANSTIME>
    <SenderReference>000000000000000</SenderReference>
    <ApplicationReference>ORDHDR</ApplicationReference>
  </StartOfTransmission>
EOD;

// INTERPRET THE XML-LIKE STRING INTO AN ARRAY
$obj = (array)SimpleXML_Load_String($raw);

// ACTIVATE THIS TO SEE THE ARRAY
// print_r($obj);

// MAKE A COLON-SEPARATED STRING
$str = implode(':', $obj);
echo PHP_EOL . htmlentities($str);

Open in new window

0
 

Author Comment

by:tonypearce
ID: 40400009
Cool, thanks, have tried to 'wrap' this around the code below but cannot get it correct, is there a way to forget creating the xml and go straight for the text string?:

       //start transmission
        $sot = $dom->createElement('StartOfTransmission');
        $arr_foo = array(
              'SenderId'=>$this->_sender_id,
              'ReceiverId'=>$this->_receiver_id,
              'TRANSDATE'=>date('d-m-Y'),
              'TRANSTIME'=>date('H:i:s'),
              'SenderReference'=>'000000000000000',
              //'ReceiverReference'=>'',
              'ApplicationReference'=>'ORDHDR',
              );
        foreach ($arr_foo as $key => $value) {
            $item = $dom->createElement($key, $value);
            $sot->appendChild($item);  
        }
       
        $element->appendChild($sot);
        unset($sot);
        //end trammission
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40400192
This should work, however it cannot be tested without being in the object context.

<?php // demo/temp_tonypearce.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28543344.html

$arr_foo = array(
              'SenderId'=>$this->_sender_id,
              'ReceiverId'=>$this->_receiver_id,
              'TRANSDATE'=>date('d-m-Y'),
              'TRANSTIME'=>date('H:i:s'),
              'SenderReference'=>'000000000000000',
              //'ReceiverReference'=>'',
              'ApplicationReference'=>'ORDHDR',
              );

// MAKE A COLON-SEPARATED STRING
$str = implode(':', $arr_foo);
echo PHP_EOL . htmlentities($str);

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:tonypearce
ID: 40400298
That's spot on, learnt a lot from this, thanks very much.
0
 

Author Closing Comment

by:tonypearce
ID: 40400303
Very accurate answer and great insight, thanks.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40400618
Thanks for the points; it's a great question, ~Ray
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question