skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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:

    <?php
    // read the XML file
    $xml = simplexml_load_file('xmlfile.xml') or die("Error: Cannot create object");
    // connect to database
    $mysqli = new mysqli("localhost", "user", "pass", "db_name");
    if ($mysqli->connect_error) {
        die("Connection failed: " . $mysqli->connect_error);
    }
    // parse XML data and prepare queries
    foreach ($xml->children() as $row) {
        // add your relevant fields
        $field1 = $row->field1;
        $field2 = $row->field2;
        // execute queries
        $stmt = $mysqli->prepare("INSERT INTO your_table (column1, column2) VALUES (?, ?)");
        $stmt->bind_param("ss", $field1, $field2);  // 'ss' denotes two string parameters
        $stmt->execute();
    }
    if ($mysqli->error) {
        echo "Error: " . $mysqli->error;
    }
    $stmt->close();
    $mysqli->close();
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search