从 Laravel 模板生成 xml 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24537842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:44:48  来源:igfitidea点击:

Generate xml file from Laravel template

phpmysqlxmllaravellaravel-4

提问by Anastasie Laurent

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

我有一个Laravel模板,我想根据它生成一个 XML 文件。

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

例如,结构具有名称、XPath 和类型。所以 XML 应该是这样的:

<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?

我正在使用 MySQL,所以也许也有一个工具?或者也许是一个 PHP 脚本?

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

我已经尝试过搜索,我发现的只是一个从 XSD 生成 HTML 表单和从 XSD 生成通用 XML 的工具。

Update

更新

The tables and their columns are:

表及其列是:

  1. xml_documenttable with columns: id
  2. general_informationtable with columns: id, xml_document_id, domain, start_urland `category
  3. master_informationtable with columns: id, xml_document_id, container, and next_page
  4. master_attributetable with columns: id, master_information_id, name, xpath, and type
  5. details_attributetable with columns: id, xml_document_id, and type
  1. xml_document带列的表: id
  2. general_information表列:idxml_document_iddomainstart_url和'类别
  3. master_information表列:idxml_document_idcontainer,和next_page
  4. master_attribute表列:idmaster_information_idnamexpath,和type
  5. details_attribute表列:idxml_document_id,和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
  1. xml_document 和 master_information 是一对一的。
  2. xml_document 和general_information 是一对一的。
  3. xml_document 和 details_attribute 是一对多的。
  4. master_information 和 master_attribute 是一对多的

回答by msturdy

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

根据Laravel 文档,集合及其关系可以输出到数组:

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

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

例如,我有两个型号,UserPhone,和一个用户hasMany()电话。

    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:

我们可以使用该toArray()方法返回一个数组,并with()提取所有相关的Phones

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

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

给出这个hidden为了这个答案,我在模型上有一些字段)

  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

然后,您可以使用各种方法中的任何一种将数组转换为 XML。这是我从关于 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>