0

This multidimensional array with attributes:

Array
(
    [EPaperPage] => Array
        (
            [@attributes] => Array
                (
                    [ePdfPageSize] => 1
                    [eJpegPageSize] => 0
                )

            [EDayOfPub] => Array
                (
                    [@attributes] => Array
                        (
                            [dtmCreatedDate] => 2016-02-02T14:40:01
                            [dtmLastUpdate] => 2016-02-02T14:40:01
                            [strCreatorLoginName] => sdsdt
                            [strLastUpdaterLoginName] => sdsdst
                            [nContentVersion] => 3
                        )

                    [OrigId] => Array
                        (
                            [@attributes] => Array
                                (
                                    [kLocationId] => 960
                                    [strPubCustId] => OND
                                    [eLogType] => 20
                                    [strDocId] => fd34c3c5-053b-45e7-bd92-66f04db013aa
                                    [nVersion] => 0
                                )

                        )
[...]

must be save in file like this:

<?xml version="1.0" encoding="UTF-8"?>
    <DialogEPaperPage>
        <EPaperPage ePdfPageSize="1" eJpegPageSize="0">
            <EDayOfPub dtmCreatedDate="2016-02-02T14:40:01" dtmLastUpdate="2016-02-02T14:40:01" strCreatorLoginName="sdfsdf" [...]
3
  • look at the php XMLWriter php class. Commented Feb 3, 2016 at 9:31
  • I tried this: techlister.com/php/convert-associative-arrays-into-xml-in-php/… and get this error: XMLWriter::startElement(): Invalid Element Name in ... Commented Feb 3, 2016 at 10:13
  • And the saved file is also not usefull:<EPaperPage><ePdfPageSize>1</ePdfPageSize> instead of <EPaperPage ePdfPageSize="1" eJpegPageSize="0"> Commented Feb 3, 2016 at 10:23

2 Answers 2

1

You can use a recursive function to walk through your array. Also, I would suggest you use SimpleXML library as it more simple than DOM. So generation code is:

<?php    
function build($value, $node) {
    if (is_array($value) == true) {
        foreach ($value as $key => $element) {
            if ($key == '@attributes') {
                foreach ($element as $n => $v) {
                    $node->addAttribute($n, $v);
                }
            } else {
                $newNode = $node->addChild($key);
                build($element, $newNode);
            }
        }
    } else {
        $node->nodeValue = $value;
    }
}

$node = new SimpleXMLElement('<root/>');
build($test_array, $node);
$node->asXML('/tmp/filename.xml');

?>

For the given array:

$test_array = [
    "EPaperPage" => [
        "@attributes" => [
                "ePdfPageSize" => 1,
                "eJpegPageSize" => 0
        ],

        "EDayOfPub" => [
            "@attributes" => [
                    "dtmCreatedDate" => "2016-02-02T14:40:01",
                    "dtmLastUpdate" => "2016-02-02T14:40:01",
                    "strCreatorLoginName" => "sdsdt",
                    "strLastUpdaterLoginName" => "sdsdst",
                    "nContentVersion" => 3
                ],

            "OrigId" => [
                    "@attributes" => [
                            "kLocationId" => "960",
                            "strPubCustId" => "OND",
                            "eLogType" => "20",
                            "strDocId" => "fd34c3c5-053b-45e7-bd92-66f04db013aa",
                            "nVersion" => 0
                    ]
            ]
        ]
    ]
];

it produces following xml:

<?xml version="1.0"?>
<root>
    <EPaperPage ePdfPageSize="1" eJpegPageSize="0">
        <EDayOfPub dtmCreatedDate="2016-02-02T14:40:01" dtmLastUpdate="2016-02-02T14:40:01" strCreatorLoginName="sdsdt" strLastUpdaterLoginName="sdsdst" nContentVersion="3">
            <OrigId kLocationId="960" strPubCustId="OND" eLogType="20" strDocId="fd34c3c5-053b-45e7-bd92-66f04db013aa" nVersion="0"/>
        </EDayOfPub>
    </EPaperPage>
</root>
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you. I get an error: Call to undefined method SimpleXMLElement::setValue()
I've updated the code. It should be $node->nodeValue = $value instead of $node->setValue($value). Sorry.
Super. Now i get only a warning: Warning: SimpleXMLElement::addAttribute() expects parameter 2 to be string, array given (on line: $node->addAttribute($n, $v);) and the xml-file is not readable: </TypeAreaDefs><OnEPage @attributes="0"><1 nReadOrder="1" mmXBeg="0" ...> instead of: </OnEPage><OnEPage nReadOrder="1" mmXBeg="0" mmYBeg="22.800000000000001" ...>
The array from the warning looks like: Array ( [nReadOrder] => 0 [mmXBeg] => 0 [mmYBeg] => 0 [mmWidth] => 284 [mmHeight] => 8.4749999999999996 [dblRotation] => 0 [bDirectChild] => true [nPartIdx] => 1 )
The problem is, that the node „OnEPage“ more then one times exist.
|
0

You can use the DOMDocument class:

/* Create new XML document: */
$xml = new DOMDocument( '1.0', 'utf-8' );

/* Add DialogEPaperPage child: */
$xml->appendChild( $root = $xml->createElement( 'DialogEPaperPage' ) );

/* Add EPaperPage child: */
$root->appendChild( $EPaperPage = $xml->createElement( 'EPaperPage' ) );
/* Set EPaperPage child attributes: */
$EPaperPage->setAttribute( 'ePdfPageSize', 1 );
$EPaperPage->setAttribute( 'eJpegPageSize', 0 );

/* Add EDayOfPub child: */
$EPaperPage->appendChild( $EDayOfPub = $xml->createElement( 'EDayOfPub' ) );
/* Set EDayOfPub child attributes: */
$EDayOfPub->setAttribute( 'dtmCreatedDate', '2016-02-02T14:40:01' );
$EDayOfPub->setAttribute( 'dtmLastUpdate', '2016-02-02T14:40:01' );
$EDayOfPub->setAttribute( 'strCreatorLoginName', 'sdsdt' );
$EDayOfPub->setAttribute( 'strLastUpdaterLoginName', 'sdsdt' );
$EDayOfPub->setAttribute( 'nContentVersion', '3' );

(...)

/* Set option to pretty output: */
$xml->formatOutput = True;
/* Echoes XML: */  
$xml->saveXML());

See more about DOMDocument


(Edit: corrected bug in code)

1 Comment

Thank you. But the input file is every time different, so i cant use this. I get the array by import: $loadfile = file_get_contents($url); $xml = simplexml_load_string( $loadfile ); $json = json_encode( $xml); $options = json_decode( $json, TRUE ); ... then I change some attributes an now i want to save it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.