Solved

XML CreateElement

Posted on 2014-11-07
2
137 Views
Last Modified: 2014-11-10
Hi,

I have an existing script that outputs an XML file, I need to alter the header and add some elements, I need some pointers please, examples below:

This is the header code:
$dom = new DOMDocument('1.0', 'utf-8');
 $element = $dom->createElement('DTD_ORDER');

This outputs:
<?xml version="1.0" encoding="utf-8"?>
<DTD_ORDER>*******************</DTD_ORDER>

What I need to do is add in some elements so result looks like:

<SyncSalesOrder xmlns:v2="http://xmlns.oracle.com/EnterpriseObjects/Core/Common/V2" xmlns:v1="http://xmlns.oracle.com/EnterpriseObjects/Core/EBO/PickList/V1" releaseID="9.0" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns="http://www.openapplications.org/oagis/9"><ApplicationArea><Sender><LogicalID>5029125000000</LogicalID><ComponentID></ComponentID><TaskID>SalesOrder</TaskID><ReferenceID>1</ReferenceID></Sender><CreationDateTime>2014-11-04T18:53:51+10:30</CreationDateTime></ApplicationArea><DataArea><Sync><ActionCriteria><ActionExpression actionCode="Sync"/></ActionCriteria></Sync><DTD_ORDER>*************</DTD_ORDER></DataArea></SyncSalesOrder>


Where
The ReferenceId should be the transmission number(date/hour as 0711201408)
The CreationDateTime should be the transmission date and time

Getting bogged down in this, any help appreciated

Thanks
0
Comment
Question by:tonypearce
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40431493
This should work for you:
$dom = new DOMDocument('1.0', 'utf-8');

    $SyncSalesOrder = $dom->createElementNS('http://www.openapplications.org/oagis/9', 'SyncSalesOrder');
        $SyncSalesOrder->setAttribute('releaseID', '9.0');
        $SyncSalesOrder->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:v2', 'http://xmlns.oracle.com/EnterpriseObjects/Core/Common/V2');
        $SyncSalesOrder->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:v1', 'http://xmlns.oracle.com/EnterpriseObjects/Core/EBO/PickList/V1' );
        $SyncSalesOrder->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:wsa', 'http://www.w3.org/2005/08/addressing');

        $ApplicationArea = $dom->createElement('ApplicationArea');
            $Sender = $dom->createElement('Sender');
                $LogicalID = $dom->createElement('LogicalID', '5029125000000');
                $Sender->appendChild($LogicalID);

                $ComponentID = $dom->createElement('ComponentID');
                $Sender->appendChild($ComponentID);

                $TaskID = $dom->createElement('TaskID', 'SalesOrder');
                $Sender->appendChild($TaskID);

                $ReferenceID = $dom->createElement('ReferenceID', date('mdYH')); // The ReferenceId should be the transmission number(date/hour as 0711201408)
                $Sender->appendChild($ReferenceID);
            $ApplicationArea->appendChild($Sender);

            $CreationDateTime = $dom->createElement('CreationDateTime', date('c')); // The CreationDateTime should be the transmission date and time
            $ApplicationArea->appendChild($CreationDateTime);
        $SyncSalesOrder->appendChild($ApplicationArea);

        $DataArea = $dom->createElement('DataArea');
            $Sync = $dom->createElement('Sync');
                $ActionCriteria = $dom->createElement('ActionCriteria');
                    $ActionExpression = $dom->createElement('ActionExpression');
                        $ActionExpression->setAttribute('actionCode', 'Sync');
                    $ActionCriteria->appendChild($ActionExpression);
                $Sync->appendChild($ActionCriteria);
            $DataArea->appendChild($Sync);

            $element = $dom->createElement('DTD_ORDER', '*************');
            $DataArea->appendChild($element);
        $SyncSalesOrder->appendChild($DataArea);
    $dom->appendChild($SyncSalesOrder);

Open in new window

manual pages I used:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/domdocument.createelementns.php
http://php.net/manual/en/domelement.setattributens.php
0
 

Author Comment

by:tonypearce
ID: 40432163
Hi,
Thanks very much, learnt a lot by seeing the code in place, php.net is good, but sometimes needs 'real world' examples, very good solution.
Tony
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XPath Syntax help in conjunction with perl XML::LibXML 18 103
Path of Workbook 3 60
Image decoding from Camera 3 72
c# code 19 69
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

19 Experts available now in Live!

Get 1:1 Help Now