Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

Import XML in PHP to add to Mysql DB

I've been trying to crack this one and just haven't been able to.

Basically I have an XML file that contains train line timetable information, and it is really complex.

I need to capture every bit of information and add to a MySQL DB, but as you'll see the parent/child and key/pair is quite dynamic and sometimes it has more or less stuff.

I have been looking a SimpleXML, but cannot get the values out of the XML, in a way it is flexible to the change in information/data.

Please note that from the 1st Journey to the second, there new fields, which as you probably guessed, it needs to captured.

How can get the elements inside the XLM below:

And also for each of the elements like:

The OR is really important to capture as also the other values.

Many thanks

Lucio

Experimental PHP code:

<?php
$xml = simplexml_load_file($argv[1]) or die("Error: Cannot create object");
//
//  TimeTableID
//
$timetableId = $xml->attributes()->timetableID;
echo "Timetable id: " . $timetableId . "

";
//
// Journey
// I've added the atributes->
foreach ($xml->Journey as $Journey) {
    echo "Journey data: " . $Journey["rid"] . "

";
    echo "Journey data: " . $Journey["uid"] . "

";
    echo "Journey data: " . $Journey["trainId"] . "

";
    echo "Journey data: " . $Journey["ssd"] . "

";
    echo "Journey data: " . $Journey["toc"] . "

";
}
?>

XML file extract:

<PportTimetable timetableID="20180124020740">
<Journey rid="201801247176474" uid="G76474" trainId="2H98" ssd="2018-01-24" toc="SN">     
    <OR tpl="BCKNMJC" act="TB" ptd="07:15" wtd="07:15" />
    <IP tpl="BIRKBCK" act="T " pta="07:18" ptd="07:18" wta="07:17:30" wtd="07:18" />
    <PP tpl="CRYSBRJ" wtp="07:20" />
    <IP tpl="CRYSTLP" act="T " plat="1" pta="07:22" ptd="07:22" wta="07:21:30" wtd="07:22" />
    <IP tpl="GIPSYH" act="T " pta="07:25" ptd="07:25" wta="07:24:30" wtd="07:25" />
    <IP tpl="WNORWOD" act="T " plat="1" pta="07:28" ptd="07:28" wta="07:27:30" wtd="07:28" />
    <PP tpl="WNORWDJ" wtp="07:29" />
    <IP tpl="TULSEH" act="T " plat="3" pta="07:31" ptd="07:34" wta="07:31" wtd="07:34" />
    <IP tpl="NDULWCH" act="T " pta="07:37" ptd="07:37" wta="07:36:30" wtd="07:37:30" />
    <IP tpl="EDULWCH" act="T " pta="07:39" ptd="07:40" wta="07:39" wtd="07:40" />
    <IP tpl="PKHMRYC" act="T " plat="2" pta="07:42" ptd="07:43" wta="07:42" wtd="07:43" />
    <IP tpl="PCKHMQD" act="T " plat="1" pta="07:45" ptd="07:45" wta="07:45" wtd="07:45:30" />
    <PP tpl="OLDKRDJ" wtp="07:46" />
    <DT tpl="SBRMNDS" act="TF" pta="07:52" wta="07:52" />
  </Journey>

  <Journey rid="201801248980806" uid="Y80806" trainId="5G45" ssd="2018-01-24" toc="VT" trainCat="EE" isPassengerSvc="false">
    <OPOR tpl="WVRMPTN" act="TB" plat="1" wtd="22:40" />
    <PP tpl="WVRMTNJ" wtp="22:41:30" />
    <PP tpl="WVRMSRJ" wtp="22:44" />
    <OPDT tpl="OXLEYCS" act="TF" wta="22:47" />
  </Journey>
</PportTimetable>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could loop the children of the SimpleXMLElement:

foreach ($Journey->children() as $child) {
    if ($child->getName() === "IP") {
        echo $child['tpl'];
    }
}

Example output php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...