简体   繁体   English

PHP XML解析到MySQL

[英]PHP XML Parse to MySQL

My question is based more on theory than syntax, but any syntax suggestions are more than welcome. 我的问题更多是基于理论而不是语法,但是任何语法建议都值得欢迎。 My project has a larger scope and a smaller scope. 我的项目范围较大,范围较小。 I will explain both, but I'm most concerned about the smaller scope immediately. 我将同时解释这两者,但我最担心的是立即缩小范围。 Any suggestions for the larger scope are welcome. 欢迎对更大范围提出任何建议。

The shorthand of what I need to accomplish(smaller scope): 我需要完成的速记(范围较小):

Parse multiple(2-4) xml files. 解析多个(2-4)xml文件。 Target a single element that reoccurs. 定位再次出现的单个元素。 Insert the data from within that element(a string of characters) into mySQL from each occurrence. 从该元件(字符的字符串)到MySQL从每次出现内插的数据。 Read back from the database how many rows of that data were inserted(I only need a count of how many times that element occurred in each file, I'm not immediately concerned with the data within the element). 从数据库中读回插入了多少行数据(我只需要计算该元素在每个文件中出现了多少次,就不必立即关注该元素中的数据了)。
I still need to store the data, but once again am ultimately only concerned with how many times the element showed up in each xml file. 我仍然需要存储数据,但最终还是要再次关注该元素在每个xml文件中显示多少次。 I will then need to echo back the summation for each xml file, and ultimately e-mail the results. 然后,我需要回显每个xml文件的总和,并最终通过电子邮件发送结果。

For the larger scope: Eventually this script will need to be automated. 对于更大的范围:最终,该脚本将需要自动化。 It will need to, once an hour, be scheduled to look for new occurrences of these xml files that are stored on a local server, inserted in mySQL, then echo back the new summation for that hour. 需要每小时安排一次,以查找存储在本地服务器中,插入到mySQL中的这些xml文件的新出现,然后回显该小时的新总和。 My thought for this would be cron jobs. 我认为这将是cron工作。

I know this is a very loaded question, but any help would be appreciated. 我知道这是一个非常棘手的问题,但是任何帮助都将不胜感激。 Thanks ahead of time for any help/suggestions. 提前感谢您的任何帮助/建议。

My recommendation would be to start writing the script, setup a DB connection, use SimpleXML , and yeah, unless you can trigger the script using organic traffic, just crontab it. 我的建议是开始编写脚本,建立数据库连接,使用SimpleXML ,是的,除非您可以使用自然流量触发脚本,否则只需crontab即可。

It's not really difficult, it's just the fact that it's a labor intensive script to write. 并不是很困难,只是事实是它是一个劳动密集型脚本。

But you can do something like this (pseudo code)... 但是您可以执行以下操作(伪代码)...

$XML = new SimpleXMLElement($xmlstr);

    foreach($XML->elements as $XML)
    {
     if(isset($XML->tag_you_are_looking_for))
     {
      //Insert $XML->tag_you_are_looking_for to DB
     }
    }

For Multiple files, just loop the parser class you made. 对于多个文件,只需循环您创建的解析器类。 For remote files, get them first using cURL . 对于远程文件,请首先使用cURL获得它们。

My suggestion overall is just start with those things in mind and if you have any more specific problems you run into, ask those questions separately. 我的建议总体而言只是从考虑这些问题开始,如果您遇到任何其他更具体的问题,请分别提出这些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM