Solved

Remove element tagging - $dom->createElement

Posted on 2014-10-23
6
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 110

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 110

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 110

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 dynamically set the form action using jQuery.

688 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