简体   繁体   中英

read XML file with php and simplexml

one more n00b here :)

i have a xml file that looks something like this:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE BPS SYSTEM "bpml.dtd">
<BPS Created="2012-04-24 11:40:41">
<Machine SerialNumber="" Site="" SoftwareRelease="MAP_248204031" VersionInfo="" Name="" Type="BPS200">
<ParameterSection Number="6" StartTime="2012-04-24 11:23:01" EndTime="1970-01-01 00:00:00">
<Operator></Operator>
<HeadercardUnit HeaderCardID="1706539" DepositID="01706539" StartTime="2012-04-24 11:39:57" MilliSec="0" EndTime="2012-04-24 11:40:40" Rejects="NO">
<Counter Currency="HRK" DenomID="22550" Value="200" Quality="Fit" Output="Stacked" Number="192"></Counter>
<Counter Currency="HRK" DenomID="22550" Value="200" Quality="Unfit" Output="Stacked" Number="7"></Counter>
</HeadercardUnit>
</ParameterSection>
</Machine>
</BPS>

i need to put values from it to the mysql db but i do not know how to read nested values from that xml.

here is what i tried so far just to see what's happening:

$xml = @simplexml_load_file($filename);
if ($xml !== FALSE) {
    $row[] = $xml->Machine->ParameterSection->HeadercardUnit; // tried to throw values in the array - no go
    foreach ($row as $item) {
        echo 'item: ' . $item . "<br>";
    }
}

but that does not work.

here is my insert statement:

INSERT INTO counttable (DenomID, Quality, Number, headercard) VALUES (22550, 'Fit', 192, 1706539)

probably using associative array of values from XML. something like this

$count = array('HeaderCardID'='1706539', 'DenomID'='22550', 'Quality'='Fit', 'Number'='192')

and then:

INSERT INTO counttable (DenomID, Quality, Number, headercard) VALUES ('$count[1]', '$count[2]', '$count[3]', '$count[0]')

and so on for every Counter row in the xml. it can be many Counter rows in single xml.

Try the following:

<?php
$xml = @simplexml_load_file('asd.xml');
if ($xml !== FALSE) {
    foreach ($xml as $key => $item) {
        foreach($item->attributes() as $attr => $val){
            print_r($val);
        }
    }
}
?>

Adapt it to your needs.

$key is "element2" and "element3";

$item is the element itself;

$attr is keyXY (eg key21, key22);

$val is the value of the attribute, valueXY (eg value21, value22).

Without any extra checking if a node exists, I believe this should be it.

The trick is to get the children of HeadercardUnit and iterate them. Then you can get attributes of each Counter element. Attributes have a name but the value is a PHP SimpleXMLElement instance. In order to get a nice value, we cast it to string.

You'll end up with an array of arrays. Each one of those can be saved in the database.

 $xml = @simplexml_load_file($filename);    
 $to_save_array = [];

 if($xml)
 {
    // looping all children, assumed it always exists
    foreach($xml->Machine->ParameterSection->HeadercardUnit->children() as $counter)
    {
        // data of this Counter record
        $record_data = [];

        // getting the attributes
        foreach($counter->attributes() as $name => $value)
        {
            // casting a SimpleXMLElement to string yields the text value of the node
            $record_data[$name] = (string)$value;
        }

        $to_save_array[] = $record_data;
    }
}

var_dump($to_save_array);

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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