从 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
Generate xml file from Laravel template
提问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:
表及其列是:
xml_document
table with columns:id
general_information
table with columns:id
,xml_document_id
,domain
,start_url
and `categorymaster_information
table with columns:id
,xml_document_id
,container
, andnext_page
master_attribute
table with columns:id
,master_information_id
,name
,xpath
, andtype
details_attribute
table with columns:id
,xml_document_id
, andtype
xml_document
带列的表:id
general_information
表列:id
,xml_document_id
,domain
,start_url
和'类别master_information
表列:id
,xml_document_id
,container
,和next_page
master_attribute
表列:id
,master_information_id
,name
,xpath
,和type
details_attribute
表列:id
,xml_document_id
,和type
As you may notice, the relationships between:
您可能会注意到,以下关系之间的关系:
- xml_document and master_information is one to one.
- xml_document and general_information is one to one.
- xml_document and details_attribute is one to many.
- master_information and master_attribute is one to many
- xml_document 和 master_information 是一对一的。
- xml_document 和general_information 是一对一的。
- xml_document 和 details_attribute 是一对多的。
- 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, User
and Phone
, and a user hasMany()
phones.
例如,我有两个型号,User
和Phone
,和一个用户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 hidden
some 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>