xml to php

Posted on 2012-08-16
Last Modified: 2012-08-16
have some automatic way of reading XML in PHP or JScript etc.

XML example in the appendix...

I want to take this inportuje XML in MySQL. application was made in PHP
Question by:cnermin
    LVL 25

    Expert Comment

    In PHP there is the SimpleXML library to handle XML ... is that what you want?
    LVL 9

    Expert Comment

    There are tons of method to read that XML with PHP and the  generic one is SimpleXML as lwadwell mentioned.

    But the question is how do you like to store that in db. Do you have a specially created database for this xml or do you need to shape data which xml contains.

    I guess you wish to import data -seems like a dealer integration data xml-
    to your already used datafields. So you first need to map every data in xml to your database fields.

    then pull xml data with any xml parser (SimpleXML is coming with PHP already) and then store them according to your mappings by iterating through xml data.

    You need to care lots of things here  like
    - what if that data already exists ? what action will be taken ? skip , overwrite or add another (you need to check for first and second one)
    - Maybe you will need to update some portions of data for existing items.
    - you may need to combine some input categories to one target category.
    - etc.

    So i wish you luck with these. If you need a specific help , we can help but as you can see this can not have a one line solution.

    Author Comment

    This is a mysql table ..

    from this table is created in vb net xml, app, upload to FTP, then PHP should read xml and entries in mysql.
    everything is ok just need PHP to read XML data from tables and insert into MySQL. I have several different tables this is an example of a table and XML

    CREATE TABLE `artikli` (
      `art_broj` int(11) DEFAULT '0',
      `naziv` varchar(50) DEFAULT ' ',
      `cijena` decimal(18,2) DEFAULT '0.00',
      `tarifa` int(11) DEFAULT '0',
      `sank` int(11) DEFAULT '0',
      `kuhinja` int(11) DEFAULT '0',
      `SL` int(11) DEFAULT '0',
      `Alkohol` int(11) DEFAULT '0',
      `Bezalkohol` int(11) DEFAULT '0',
      `Ulcijenabezpdv` decimal(15,4) DEFAULT '0.0000',
      `Ulcijenasapdv` decimal(23,8) DEFAULT '0.00000000',
      `Minzalihe` decimal(10,0) DEFAULT '0',
      `Maxzalihe` decimal(10,0) DEFAULT '0',
      `Izcijenabezbdv` decimal(23,8) DEFAULT '0.00000000',
      `Izcijenasapdv` decimal(15,4) DEFAULT '0.0000',
      `Jedmjer` varchar(50) DEFAULT '0',
      `Tip` varchar(50) DEFAULT '0',
      `Stanje` decimal(15,4) DEFAULT '0.0000',
      `M1` decimal(15,4) DEFAULT '0.0000',
      `M2` decimal(15,4) DEFAULT '0.0000',
      `M3` decimal(15,4) DEFAULT '0.0000',
      `M4` decimal(15,4) DEFAULT '0.0000',
      `M5` decimal(15,4) DEFAULT '0.0000',
      `Kasa` int(11) DEFAULT '0',
      `Art_Usl` int(11) DEFAULT '0',
      `NazivArtikla` varchar(50) DEFAULT '0',
      `Cigarete` int(11) DEFAULT '0',
      `Ostalo` int(11) DEFAULT '0',
      `Grupa` int(11) DEFAULT '0',
      `Ulaz` int(11) DEFAULT '0',
      `Slika` varchar(50) DEFAULT '0',
      `sanklista` int(11) DEFAULT '0',
      `lokacija` smallint(6) DEFAULT '0',
      `prsank` int(11) DEFAULT '0',
      `dusl` smallint(6) DEFAULT '0',
      `bar` varchar(50) DEFAULT '0',
      `novo` int(11) DEFAULT '0',
      `unic` varchar(50) DEFAULT '0',
      `cod` varchar(50) DEFAULT '0',
      `code` varchar(50) DEFAULT '0',
      PRIMARY KEY (`ID`)
    LVL 9

    Expert Comment

    Reading XML and converting to an php object can be done like this ;
    last line is just for debuging $data content..
                $kaynak = file_get_contents("ee-artikli.xml");
                $root   = simplexml_load_string($kaynak);
                $data   = get_object_vars($root);
                echo "<pre>";var_dump($data[$itemName][0]);echo "</pre>";

    Open in new window

    I think you can build your sql query syntax now
    LVL 25

    Accepted Solution

    Here is a cut down version with a few columns only ... as an example.
    $mysqli = new mysqli($db_host, $db_user, $db_word, $db_name);
    if ( $mysqli->connect_errno ) {
        die("Connect error (". $mysqli->connect_errno .") ". $mysqli->connect_error);
    echo "<pre>";
    $filename = '/Apps/DATA/artikli.xml';
    if ( file_exists($filename) ) {
        if ( $xml = simplexml_load_file($filename) ) {
            $sql = "INSERT INTO artikli (art_broj,naziv,cijena,tarifa,sank,kuhinja,SL) VALUES (?,?,?,?,?,?,?)";
            $stmt = $mysqli->prepare($sql);
              $total = 0;
            foreach ( $xml->xpath('//Table1') as $ele ) {
                $art_broj   = $ele->art_broj;
                $naziv      = $ele->naziv;
                $cijena     = $ele->cijena;
                $tarifa     = $ele->tarifa;
                $sank       = $ele->sank;
                $kuhinja    = $ele->kuhinja;
                $SL         = $ele->SL;
                $total += $stmt->affected_rows;
            echo "Total rows inserted = $total", PHP_EOL;
        } else {
            exit("Failed to load $filename.");
    } else {
        exit("File $filename was not found.");

    Open in new window


    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now