Search code examples
phpxmlsimplexml

how i can to build xml with this format?


I´m trying to create xml from associative array with php. To build my xml, i´m using SimpleXMLElement

I need that contain this structure:

<pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0">
    <pt:request_meta>
        <request_meta:service>PT0005_ACCEPTANCEXPRESS</request_meta:service>
        <request_meta:time2close unit="d" value="28"/>
        <request_meta:time2save unit="d" value="1825"/>
        <request_meta:lopd>0</request_meta:lopd>
        <request_meta:retryprotocol>3</request_meta:retryprotocol>
        <request_meta:synchronous>false</request_meta:synchronous>
        <request_meta:tsa>0</request_meta:tsa>
        <request_meta:userdefined name="LGT_SDKJava">4.13.2</request_meta:userdefined>
    </pt:request_meta>
</pt:request>

In my SimpleXMLElement instance, i´m doing:

new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0"><pt:request_meta></pt:request_meta></pt:request>');

my xml its created ok, but when i show it in notepad++ with complment for XML i can see that my tag it´s bad formed:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0">
    <pt:request_meta/>
    <pt:service>instalacion_id_1</pt:service>
    <pt:0>request_meta:time2close unit="d" value="28"</pt:0>
    <pt:1>request_meta:time2save unit="d" value="1825"</pt:1>
    <pt:lopd>0</pt:lopd>
    <pt:retryprotocol>3</pt:retryprotocol>
    <pt:synchronous/>
    <pt:tsa>0</pt:tsa>
    <pt:userdefined name="php">8.1.6</pt:userdefined>
</pt:request>

Anybody can help my to create my XMl with correct structure¿? i don´t know how i can to do my tag with this information, for example:

<request_meta:time2close unit="d" value="28"/>

My array data it´s:

$logalty_data = [
          'request_meta:service' => 'instalacion_id_'.$id,
          'request_meta:time2close unit="d" value="28"',
          'request_meta:time2save unit="d" value="1825"',
          'request_meta:lopd' => 0,
          'request_meta:retryprotocol' => 3,
          'request_meta:synchronous' => false,
          'request_meta:tsa' => 0,
          'request_meta:userdefined name="php"' => '8.1.6'
 ];

UPDATE:

I have got solve my problem with tags: <request_meta:time2close unit="d" value="28"/>

i add in my array in this index: => "" with this way, create a empty tag and close it:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0">
    <pt:request_meta/>
    <pt:service>instalacion_id_1</pt:service>
    <pt:time2close unit="d" value="28"/>
    <pt:time2save unit="d" value="1825"/>
    <pt:lopd>0</pt:lopd>
    <pt:retryprotocol>3</pt:retryprotocol>
    <pt:synchronous/>
    <pt:tsa>0</pt:tsa>
    <pt:userdefined name="php">8.1.6</pt:userdefined>
</pt:request>

but still i have problem with tag <pt:request_meta/> only have a close tag, not open tag

in response to user: @mrázek-honza my xml result its:

<pt:request_meta>
    <pt:service>instalacion_id_1</pt:service>
    <pt:time2close unit="d" value="28"/>
    <pt:time2save unit="d" value="1825"/>
    <pt:lopd>0</pt:lopd>
    <pt:retryprotocol>3</pt:retryprotocol>
    <pt:synchronous/>
    <pt:tsa>0</pt:tsa>
    <pt:userdefined name="php">8.1.6</pt:userdefined>
</pt:request_meta>

i lose <?xml version="1.0" encoding="UTF-8" standalone="yes"?><pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0"></pt:request>


Solution

  • If you want to add something as a child element, you have to do it like this. This adds the wrapping element as the child of the root one, and the you add children to the wrapping element directly.

    <?php
    
    $xml = new \SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0"></pt:request>');
    
    $wrapper = $xml->addChild('pt:request_meta');
    
    $wrapper->addChild('request_meta:service', 'instalacion_id_1', 'request_meta');
    // add the rest of the data
    
    $xml->saveXML('test.xml');
    

    The output produced by this snippet is

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <pt:request xmlns:pt="urn:logalty:schemas:core:1.0" xmlns:identity="urn:logalty:schemas:core:identity:1.0" xmlns:process_meta="urn:logalty:schemas:process:meta:1.0" xmlns:ptforms="urn:logalty:schemas:forms:1.0" xmlns:request_meta="urn:logalty:schemas:request:meta:1.0">
        <pt:request_meta>
            <request_meta:service xmlns:request_meta="request_meta">instalacion_id_1</request_meta:service>
        </pt:request_meta>
    </pt:request>