Solved

Remove element tagging - $dom->createElement

Posted on 2014-10-23
6
92 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

943 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

9 Experts available now in Live!

Get 1:1 Help Now