Solved

Remove element tagging - $dom->createElement

Posted on 2014-10-23
6
90 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 108

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 108

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 108

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now