Generate xml file from Laravel template

15,666

As per the Laravel Documentation, Collections and their relationships can be output to arrays:

$roles = User::find(1)->roles->toArray();

for example, I have two models, User and Phone, and a user hasMany() phones.

    users                  phones
    +----+--------+        +----+-----------+---------+
    | id | name   |        | id | number    | user_id |
    +----+--------+        +----+-----------+---------+
    |  1 | user 1 |        |  1 | 111111111 |       1 |
    |  2 | user 2 |        |  2 | 222222222 |       2 |
    +----+--------+        |  3 | 333333333 |       1 |
                           +----+-----------+---------+

we can return an array of this using the toArray() method, and with() to pull out all the related Phones:

$users = User::with('phones')->get()->toArray();

giving this (I have hidden some of the fields on the model for the purposes of this answer):

  Array (
    [0] => Array (
            [id] => 1
            [name] => user 1
            [phones] => Array (
                    [0] => Array (
                            [id] => 1
                            [number] => 111111111
                            [user_id] => 1
                        )

                    [1] => Array (
                            [id] => 3
                            [number] => 333333333
                            [user_id] => 1
                        )
                )    
        )    
    [1] => Array (
            [id] => 2
            [name] => user 2
            [phones] => Array (
                    [0] => Array (
                            [id] => 2
                            [number] => 222222222
                            [user_id] => 2
                        )    
                )    
        )    
)

Then you can use any of the various methods for turning arrays into XML. Here's one I've lifted from another answer on SO

function array_to_xml(array $arr, SimpleXMLElement $xml)
{
    foreach ($arr as $k => $v) {
        is_array($v)
            ? array_to_xml($v, $xml->addChild($k))
            : $xml->addChild($k, $v);
    }
    return $xml;
}

Example output:

<?xml version="1.0"?>
<root><0><id>1</id><name>user 1</name><phones><0><id>1</id><number>111111111</number><user_id>1</user_id></0><1><id>3</id><number>333333333</number><user_id>1</user_id></1></phones></0><1><id>2</id><name>user 2</name><phones><0><id>2</id><number>222222222</number><user_id>2</user_id></0></phones></1></root>
Share:
15,666
Anastasie Laurent
Author by

Anastasie Laurent

Updated on June 04, 2022

Comments

  • Anastasie Laurent
    Anastasie Laurent almost 2 years

    I have a Laravel template and I want to generate an XML file depending on it.

    For example, the structure has a name, XPath and type. So the XML should look like this:

    <name>
    </name>
    <type>
    </type>
    <xpath>
    </xpath>
    

    Of course I am looking for something more complex that can deal with relationships.

    Is there any tool for that?

    I am using MySQL, so maybe there is a tool for that, too? Or maybe a PHP script?

    I already tried to search and all I have found is a tool to generate HTML forms from XSD and general XML from XSD.

    Update

    The tables and their columns are:

    1. xml_document table with columns: id
    2. general_information table with columns: id, xml_document_id, domain, start_url and `category
    3. master_information table with columns: id, xml_document_id, container, and next_page
    4. master_attribute table with columns: id, master_information_id, name, xpath, and type
    5. details_attribute table with columns: id, xml_document_id, and type

    As you may notice, the relationships between:

    1. xml_document and master_information is one to one.
    2. xml_document and general_information is one to one.
    3. xml_document and details_attribute is one to many.
    4. master_information and master_attribute is one to many
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    very nice answer, I will try it and update you. but after 1.30 hour. many thanks
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    when I tried this:$xmlDocument = XmlDocument::find(4)->get()->toArray(); I got all the rows in the database not just the row 4. then I tried this $xmlDocument = XmlDocument::find(4)->toArray(); so I just get the columns not the relationships. I mean I just get the id(witch is only column in the table xmldocument). However, I should have got the other relationships. for exaple, I have one to many relationship with a table master_information, I didn't get these values. could you help please?
  • msturdy
    msturdy almost 10 years
    @AnastasieLaurent - can you update your question with the relationships ?
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    Okay I will give you all the relationships, wait please to update the question
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    until now I have this code $xmlDocument = XmlDocument::with('MasterInformation', 'GeneralInformation', 'DetailsAttributes')->find(4)->toArray(); and it gives me all the information. I just still have to add the MasterAttribute to this question. I didn't know how. that is because the MasterAttribute is one to many with MasterInformation not with XmlDocument can you help please?
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    here we go $xmlDocument = XmlDocument::with('MasterInformation', 'GeneralInformation', 'MasterInformation.MasterAttributes', 'DetailsAttributes')->find(4)->toArray(); it works, now I will check your code that transfer the array to xml.
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    That is enough for this question. I will accept your answer and then ask another answer about changing array to xml. will give you the link in case you would like to answer. appreicate your help
  • Anastasie Laurent
    Anastasie Laurent almost 10 years
    I created a new account because I couldn't ask more than 50 questions each 30 days and I asked this question stackoverflow.com/questions/24556875/… can you check pleaes?