“Laravel simplexmlelement xml add atributo” Código de respuesta

Laravel simplexmlelement xml add atributo

<?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');
Shadow

Laravel simplexmlelement xml add atributo

<?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();

?>
Shadow

Respuestas similares a “Laravel simplexmlelement xml add atributo”

Preguntas similares a “Laravel simplexmlelement xml add atributo”

Más respuestas relacionadas con “Laravel simplexmlelement xml add atributo” en PHP

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código