xml-wrangler icon indicating copy to clipboard operation
xml-wrangler copied to clipboard

$reader->elements() loosing attributes if node same name

Open SDJeff opened this issue 2 years ago • 1 comments

<ORDERPOSITIONS>
    <ORDERPOSITION>
 ...
 <REMARK type="costcenter"/>
 <REMARK type="order"/>
 ...
 </ORDERPOSITION>
</ORDERPOSITIONS>

with $reader->values() the result looks like this

CleanShot 2024-01-04 at 13 00 29

How to keep the attributes as keys?

SDJeff avatar Jan 04 '24 12:01 SDJeff

Hey @SDJeff!

This is the expected behaviour - when there are multiple child elements with the same name, the library will decode it into an array of values. If you are looking to have the attributes on the element, you can try using $reader->elements() instead which is slightly harder to traverse, but gives you an array of Element classes which contains all of the information.

You could also read the REMARK property separately with the $reader->element() method. You can use dot-notation to find the node:

$elements = $reader->element('orderpositions.remark')->get(); // [Element::class, Element::class]

foreach($elements as $element) {
     $attributes = $element->getAttributes(); // e.g 'type' => 'order'
}

Sammyjo20 avatar Jan 04 '24 19:01 Sammyjo20