Hello,
I have been reading an XML every night for 2 years and capturing the data just fine into a MySQL database however the equipment has now been renewed and the format of the XML file has now changed and I have struggled trying to now read it.
I have read extra tutorials and examples but just can not get it to the new format, can anyone help me understand the issue of reading it
First off I shall show the format of the original XML file and the simple code I used to read it and parase. The xml file is call tempdata.xml and is stored on my company server and it is the the output of data from some temperature sensors.
<?xml version="1.0" encoding="UTF-8"?>
<product>
<dataseries>
<init>20111021</init>
<timepoint>3h</timepoint>
<temp2m>29</temp2m>
<prec_type>none</prec_type>
</data>
</dataseries>
<dataseries>
<init>20111021</init>
<timepoint>6h</timepoint>
<temp2m>32</temp2m>
<prec_type>none</prec_type>
</data>
</dataseries>
<dataseries>
<init>20111021</init>
<timepoint>12h</timepoint>
<temp2m>35</temp2m>
<prec_type>norm</prec_type>
</data>
</dataseries>
</product>
$url = "http://localhost/data/tempdata.xml";
$simple_xml_obj = simplexml_load_file($url);
if ($simple_xml_obj == false) {
echo "Error on load<br />";
}
foreach($simple_xml_obj->dataseries as $datapoints) {
echo "Date: {$datapoints->init}" . "<br />";
echo "Timepoint: {$datapoints->timepoint}" . "<br />";
echo "2 Minute Temp: {$datapoints->temp2m}" . "<br />";
echo "Precision: {$datapoints->prec_type}" . "<br />";
}
Now here is the new format of the xml file
<?xml version="1.0" encoding="UTF-8"?>
<product name="tempy">
<init>20111021</init>
<dataseries>
<data timepoint="3h">
<temp2m>29</temp2m>
<prec_type>none</prec_type>
</data>
<data timepoint="6h">
<temp2m>25</temp2m>
<prec_type>none</prec_type>
</data>
<data timepoint="9h">
<temp2m>23</temp2m>
<prec_type>none</prec_type>
</data>
</dataseries>
</product>
And here is my latest attempt at the coding
$url = "http://localhost/data/tempdata.xml";
$simple_xml_obj = simplexml_load_file($url);
if ($simple_xml_obj == false) {
echo "Error on load<br />";
}
foreach($simple_xml_obj->dataseries as $datapoints) {
echo "Timepoint: {$datapoints->timepoint}" . "<br />";
echo "2 Minute Temp: {$datapoints->temp2m}" . "<br />";
echo "Precision: {$datapoints->prec_type}" . "<br />";
}
Now I have tried different foreach statements as well but nothing seems to work like
foreach($simple_xml_obj->product->dataseries as $datapoints)
foreach($simple_xml_obj->product->dataseries->data as $datapoints)
I also need to capture the single <init> node to store.
Thank you