Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple elements for one level. How? #5

Closed
1271 opened this issue Nov 6, 2018 · 1 comment
Closed

Multiple elements for one level. How? #5

1271 opened this issue Nov 6, 2018 · 1 comment
Labels

Comments

@1271
Copy link

1271 commented Nov 6, 2018

Example xml:

 <item>
  <image src="">
  <image src="">
  <image src="">
  <image src="">
 </item>

Example code:

$item= [
    '@attributes' => $this->attributes,
    '@value'      => [
        'language'      => $this->language,
        'author'        => $this->author,
        'category'      => $this->category,
    ]
];

$dom = new \DOMDocument();
foreach ($this->images as $image)
{
    $enclosure = $dom->createElement('image');
    $enclosure->setAttribute('src', $image);
    $item['@value'][] = $enclosure; // error
//    $item['@value']['image'] = $enclosure;   // ok, but incorrect structure and one only element allowed
}

$xml = new XMLReader(null);
$xmlData = [
...
'item' => $item,
];
$result = $xml->asXML([...], 'xml', [...]);

Thank you

@rez1dent3
Copy link
Member

rez1dent3 commented Nov 26, 2018

Hey, @1271.

Try to do so.

<?php

include_once __DIR__ . '/vendor/autoload.php';

$items = [
    '@attributes' => [],
    '@value' => [
        'language' => 'ru',
        'author' => 'test',
        'category' => 'test',
        'image' => [
            ['@attributes' => ['src' => ''], '@value' => ''],
            ['@attributes' => ['src' => ''], '@value' => ''],
            ['@attributes' => ['src' => ''], '@value' => ''],
            ['@attributes' => ['src' => ''], '@value' => ''],
        ]
    ]
];

$xml = new Bavix\XMLReader\XMLReader();
echo $xml->asXML($items, 'products');

@rez1dent3 rez1dent3 pinned this issue Dec 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants