Search
 
SCRIPT & CODE EXAMPLE
 

PHP

laravel simplexmlelement xml add attribute

<?php
  function array_to_xml( $data, &$xml_data ) {
        foreach( $data as $key => $value ) {
            if (!empty($value)) {
                if( is_array($value)) {
                    if (!empty($value["@attributes"])) {
                            $subnode = $xml_data->addChild($key, $value["@value"]);
                            foreach ($value["@attributes"] as $key1 => $val1) {
                                $subnode->addAttribute($key1, $val1);
                            }
                    } else if ($key == "@value") {
                        foreach ($value as $attr => $attrVal) {
                            $subnode = $xml_data->addChild("$attr", $attrVal);
                            array_to_xml($attrVal, $subnode);
                        }
                    } else {
                            if (!empty($value)) {
                                    $subnode = $xml_data->addChild($key);
                                    array_to_xml($value, $subnode);
                            }
                    }
                } else {
                        $xml_data->addChild("$key",$value);
                }
            }
        }
    }

$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');

$arrXml = [
    "categories" => [
        'category' => [
            '@attributes' => [
                'id' => '123',
                'parent_id' => '12345'
            ],
            '@value' => 'Bikes'
        ]
    ],
    "properties" => [
        'property' => [
            'id' => '123',
            'categoryId' => '1',
            'name' => 'Color',
            'values' => [
                'value' => [
                    "id" => '1',
                    "name" => 'Black'
                ],
                'value' => [
                    "id" => '2',
                    "name" => 'White'
                ]
            ]
        ]
    ],
    "products" => [
        'products' => [
            'id' => '1231231',
            'categoryId' => '123',
            'model' => [
                    '@attributes' => [
                            'foo' => 'bar',
                    ],
                '@value' => 'Avalanche'
            ],
            'article' => '1.0 2011',
            'vendor' => 'GT',
        ]
    ]
];

array_to_xml($arrXml,$xml_data);

//saving generated xml file;
$result = $xml_data->asXML('test.xml');
Comment

laravel simplexmlelement xml add attribute

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');

echo $sxe->asXML();

?>
Comment

PREVIOUS NEXT
Code Example
Php :: make pagination wordpress admin panel 
Php :: contact form 7 select disabled option 
Php :: indian time laravel 
Php :: how to add a text to image in php 
Php :: php get prameter 
Php :: count in string php 
Php :: how to set session in laravel 
Php :: php detect base64 encoding 
Php :: cut out the beginning of the text in php 
Php :: DB::beginTransaction() 
Php :: how push to array whit key in laravel 
Php :: tcpdf error unable to create output file in php 
Php :: ci count 
Php :: php.ini location mac 
Php :: php is day light saving time 
Php :: php déclarer une constante URL 
Php :: php remove html tags 
Php :: laravel relationship with for single data 
Php :: how to use required_with in laravel to array element 
Php :: prestashop get all products 
Php :: wordpress get user by id 
Php :: PHP CSV File Export Using fputcsv() 
Php :: check if all values in array are equal php 
Php :: tolower php 
Php :: command to run php file on chrome 
Php :: sha256 php 
Php :: boot method laravel life cycle 
Php :: composer 
Php :: convert string to array laravel 
Php :: artisan show routes for model 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =