H
H
helloris2014-11-12 13:52:40
PHP
helloris, 2014-11-12 13:52:40

How to build an xml tree from a path?

Hi all!
There are lines like
OASIS Exclusive\Luxury\
OASIS Exclusive\Luxury\Brand
Gifts\Lux
From them you need to build the xml view using php

<Группы>
   <Группа имя="OASIS Exclusive">
      <Группа имя="Luxury">
         <Группа имя="Brand"></Группа>
    </Группа>
   </Группа>
   <Группа имя="Gifts">
      <Группа имя="Lux"></Группа>
   </Группа>
</Группы>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2014-11-12
@helloris

We loop through all the lines and split them into one-dimensional arrays (we use '\' as a separator), transform them using the add. recursive function into multidimensional arrays ['OASIS Exclusive' => ['Luxury' => ['Brand']]], combine the arrays into one and use another recursive function to build a dom structure. If the amount of data is not very large or you need to not only build a tree, but also perform some manipulations, then it is better to use SimpleXMLElement or DOMElement objects instead of arrays.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question