gpt4 book ai didi

PHP - 来自指定标签的 xml 数据作为 sql 条目

转载 作者:行者123 更新时间:2023-11-30 22:50:58 25 4
gpt4 key购买 nike

我有一些数据的 XML 文件。我需要将此文件中的 sql 表数据放入 sql 表中,但只能将指定的标记作为单独的 mysql 条目。

这是 XML 数据的例子

<entry>
<published>2014-12-24T00:03:00.002-08:00</published>
<updated>2014-12-24T00:04:26.884-08:00</updated>
<title type='text'>Article title</title>
<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#kind'/>
<content type='html'>Here content data</content>
<author>
<name>Author name</name>
<uri>http://www.example.com</uri>
<email>some@email.com</email>
</author>
<thr:total>0</thr:total>
</entry>

现在我想从这个标签中获取数据并将其作为单独的条目放入 sql 表中。在 MySQL 中,我得到了带有“日期”、“标题”和“内容”列的表,并且只有从这个标签我才需要将数据放入 mysql。

我只需要知道如何从 XML 导入数据并在 PHP 中使用。仅此而已。

请为我分享一些技巧。

最佳答案

您可以使用 xml_parse_into_struct() 函数,它将 XML 文档中的数据解析为可用的 PHP 数组。

<?php
$xmlfile = 'test.xml';
$xmlparser = xml_parser_create();

$fp = fopen($xmlfile, 'r');
$xmldata = fread($fp, 4096);

xml_parse_into_struct($xmlparser,$xmldata,$values);

xml_parser_free($xmlparser);
print_r($values);
?>

您需要根据您的特定需求修改代码。

来源:http://www.w3schools.com/php/func_xml_parse_into_struct.asp

关于PHP - 来自指定标签的 xml 数据作为 sql 条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28157166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com