PHP导入 /解析XML文件内容保存到数据库

发布于 2025-01-25 09:50:07 字数 1397 浏览 2 评论 0原文

试图将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云醉月微眠 2025-02-01 09:50:07

尝试以下操作:

foreach ($newXML['sms'] as $attrib) {
    $date = $attrib["@attributes"]["date"];
    $body = $attrib["@attributes"]["body"];
    echo $date . " - " . $body; 
}

Try this:

foreach ($newXML['sms'] as $attrib) {
    $date = $attrib["@attributes"]["date"];
    $body = $attrib["@attributes"]["body"];
    echo $date . " - " . $body; 
}
剪不断理还乱 2025-02-01 09:50:07

考虑使用 加载xml 。以下可以作为PHP的任何其他SQL命令运行。

假设所有属性名称匹配表名称(尽管忽略了无与伦比的名称):

LOAD XML LOCAL INFILE 'input.xml' 
  INTO TABLE myTable 
  ROWS IDENTIFIED BY '<sms>';

或者,使用set的本地变量对特定列或不同命名的表名称:

LOAD XML LOCAL INFILE 'input.xml'
  INTO TABLE myTable (@date, @body) 
  ROWS IDENTIFIED BY '<sms>'
  SET date_column=@date, body_column=@body; 

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):

LOAD XML LOCAL INFILE 'input.xml' 
  INTO TABLE myTable 
  ROWS IDENTIFIED BY '<sms>';

Alternatively, use local variables with SET for specific columns or differently named table names:

LOAD XML LOCAL INFILE 'input.xml'
  INTO TABLE myTable (@date, @body) 
  ROWS IDENTIFIED BY '<sms>'
  SET date_column=@date, body_column=@body; 
梦初启 2025-02-01 09:50:07

SimpleXML来简化代码

您可以通过正确使用 属性。

$xml = simplexml_load_file('input.xml') or die('Error: Cannot load file');

foreach ($xml->sms as $attrib) {
    $date = $attrib['date'];
    $body = $attrib['body'];
    echo $date . ' - ' . $body; // test output (fails and returns empty)
}

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.

$xml = simplexml_load_file('input.xml') or die('Error: Cannot load file');

foreach ($xml->sms as $attrib) {
    $date = $attrib['date'];
    $body = $attrib['body'];
    echo $date . ' - ' . $body; // test output (fails and returns empty)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文