“laravele simplexmlelement xml Dodaj atrybut” Kod odpowiedzi

laravele simplexmlelement xml Dodaj atrybut

<?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

laravele simplexmlelement xml Dodaj atrybut

<?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

Odpowiedzi podobne do “laravele simplexmlelement xml Dodaj atrybut”

Pytania podobne do “laravele simplexmlelement xml Dodaj atrybut”

Więcej pokrewnych odpowiedzi na “laravele simplexmlelement xml Dodaj atrybut” w PHP

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu