PHP导入 /解析XML文件内容保存到数据库
试图将SMS登录到PHP中以在MySQL DB中解析。 无法解析以前抓取所有记录的整个数组的单个数组值。
文件负载,能够成功打印整个输入的数组,但是foreach循环仅返回一个结果,不是全部13个,并且是空的。
// Load xml file else check connection
$xml = simplexml_load_file("input.xml") or die("Error: Cannot load file");
$con = json_encode($xml);
$newXML = json_decode($con, true);
print_r($newXML['sms']); //Output: Prints 0-4 lines successfully all together in array
foreach ($newXML['sms'] as $attrib) {
$date = $attrib->date;
$body = $attrib->body;
echo $date . " - " . $body; // test output (fails and returns empty)
//Save Each Separate array line (sms message record) info to Db...
...
XML文件布局:
<?xml version="1.0" encoding="UTF-8"?>
<smsgroup>
<sms address="1234567" time="Apr 30, 2022 1:00:00 PM" date="1555987654339" type="2" body="message 5" read="1" />
<sms address="1234567" time="Apr 30, 2022 1:00:00 PM" date="1555987654333" type="1" body="sms 4" read="1" />
<sms address="5555555" time="Apr 30, 2022 1:00:00 PM" date="1555987654329" type="1" body="another message 3" read="1" />
<sms address="5555555" time="Apr 30, 2022 1:00:00 PM" date="1555987654324" type="1" body="message 2" read="1" />
<sms address="1234567" time="Apr 30, 2022 1:00:00 PM" date="1555987654321" type="2" body="message 1" read="1" />
</smsgroup>
Trying to import an sms log into php for parsing in a mysql db.
Unable to parse individual array values past grabbing the full array of all records.
File loads, able to print array of entire input successfully, but foreach loop returns only one result, not all 13, and is empty.
// Load xml file else check connection
$xml = simplexml_load_file("input.xml") or die("Error: Cannot load file");
$con = json_encode($xml);
$newXML = json_decode($con, true);
print_r($newXML['sms']); //Output: Prints 0-4 lines successfully all together in array
foreach ($newXML['sms'] as $attrib) {
$date = $attrib->date;
$body = $attrib->body;
echo $date . " - " . $body; // test output (fails and returns empty)
//Save Each Separate array line (sms message record) info to Db...
...
XML File layout:
<?xml version="1.0" encoding="UTF-8"?>
<smsgroup>
<sms address="1234567" time="Apr 30, 2022 1:00:00 PM" date="1555987654339" type="2" body="message 5" read="1" />
<sms address="1234567" time="Apr 30, 2022 1:00:00 PM" date="1555987654333" type="1" body="sms 4" read="1" />
<sms address="5555555" time="Apr 30, 2022 1:00:00 PM" date="1555987654329" type="1" body="another message 3" read="1" />
<sms address="5555555" time="Apr 30, 2022 1:00:00 PM" date="1555987654324" type="1" body="message 2" read="1" />
<sms address="1234567" time="Apr 30, 2022 1:00:00 PM" date="1555987654321" type="2" body="message 1" read="1" />
</smsgroup>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下操作:
Try this:
考虑使用
加载xml
。以下可以作为PHP的任何其他SQL命令运行。假设所有属性名称匹配表名称(尽管忽略了无与伦比的名称):
或者,使用
set
的本地变量对特定列或不同命名的表名称:Consider directly importing that relatively flat, attribute-centric XML into MySQL table using
LOAD XML
. Below can be run as any other SQL command from PHP.Assuming all attribute names match table names (though unmatched names are ignored):
Alternatively, use local variables with
SET
for specific columns or differently named table names:SimpleXML来简化代码
您可以通过正确使用 属性。
You can simplify your code by using SimpleXML properly and not having to json encode and decode the data...
Use
->
to access the elements and[]
to access the attributes.