I have parsed an XML file to JSON and need the values to store in MySQL.
The JSON data below contains of ‘one-off’ and Array data.
What is the best way to catch get values with PHP?
How to get the one-off values and determine the array count to get the array values.
JSON output:
[mRID] => f0d72343f9d94bf3a1405cdb2f7ef373
[revisionNumber] => 1
[type] => A44
[sender_MarketParticipant.mRID] =>
stdClass Object (
[@attributes] => stdClass Object (
[codingScheme] => A01
[nodeValue] => 10X1001A1001A450 ) )
[sender_MarketParticipant.marketRole.type] => A32
[receiver_MarketParticipant.mRID] =>
stdClass Object (
[@attributes] => stdClass Object (
[codingScheme] => A01
[nodeValue] => 10X1001A1001A450 ) )
[receiver_MarketParticipant.marketRole.type] => A33
[createdDateTime] => 2023-11-27T15:56:55Z
[period.timeInterval] => stdClass Object (
[start] => 2023-11-26T23:00Z
[end] => 2023-11-27T23:00Z )
[TimeSeries] => stdClass Object (
[mRID] => 1 [businessType] => A62
[in_Domain.mRID] =>
stdClass Object (
[@attributes] => stdClass Object (
[codingScheme] => A01
[nodeValue] => 10YNL----------L ) )
[out_Domain.mRID] => stdClass Object (
[@attributes] => stdClass Object (
[codingScheme] => A01
[nodeValue] => 10YNL----------L ) )
[currency_Unit.name] => EUR
[price_Measure_Unit.name] => MWH
[curveType] => A01
[Period] => stdClass Object (
[timeInterval] => stdClass Object (
[start] => 2023-11-26T23:00Z
[end] => 2023-11-27T23:00Z )
[resolution] => PT60M
[Point] => Array (
[0] => stdClass Object (
[position] => 1
[price.amount] => 99.31 )
[1] => stdClass Object (
[position] => 2
[price.amount] => 94.15 )
[2] => stdClass Object (
[position] => 3
[price.amount] => 89.90 )
[3] => stdClass Object (
[position] => 4
[price.amount] => 88.82 )
[4] => stdClass Object (
[position] => 5
[price.amount] => 87.91 )
[5] => stdClass Object (
[position] => 6
[price.amount] => 94.09 )
(and so on...)`
2
Answers
Stick to the XML format and check out PHP:s SimpleXML extension. It makes it quite easy to traverse an XML tree in the way you want.
Rather than using JSON, use PHP to read the XML file. There is a similar question here: parse xml-file and insert into mysql database
This can be done using SimpleXML or DOMDocument class. Then extract the relevant information and store it in your database. Here is an example: