2

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
  • 1,169
  • 6
  • 23
  • 35
  • XML is very flexible, and as it stands you need more information in your question... can you explain the relationships in your app? Can you give an example of what the resultant XML should look like? – msturdy Jul 02 '14 at 18:36
  • @msturdy yes I can give you all the information you need. I need an xml file that represents my laravel model. the database table has many columns and I need to extract these columns but as xml document. did you got me? – Anastasie Laurent Jul 02 '14 at 19:27

1 Answers1

7

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>
Community
  • 1
  • 1
msturdy
  • 10,479
  • 11
  • 41
  • 52
  • very nice answer, I will try it and update you. but after 1.30 hour. many thanks – Anastasie Laurent Jul 02 '14 at 20:46
  • 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? – Anastasie Laurent Jul 03 '14 at 13:23
  • @AnastasieLaurent - can you update your question with the relationships ? – msturdy Jul 03 '14 at 13:45
  • Okay I will give you all the relationships, wait please to update the question – Anastasie Laurent Jul 03 '14 at 14:00
  • 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 Jul 03 '14 at 14:10
  • 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 Jul 03 '14 at 14:17
  • 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 Jul 03 '14 at 14:27
  • I created a new account because I couldn't ask more than 50 questions each 30 days and I asked this question http://stackoverflow.com/questions/24556875/php-change-php-array-to-xml-format can you check pleaes? – Anastasie Laurent Jul 03 '14 at 14:50