?
Solved

Remove element tagging - $dom->createElement

Posted on 2014-10-23
6
Medium Priority
?
98 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

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 111

Expert Comment

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

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 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