?
Solved

xml to php

Posted on 2012-08-16
6
Medium Priority
?
547 Views
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
artikli.xml
0
Comment
Question by:cnermin
  • 2
  • 2
  • 2
6 Comments
 
LVL 25

Expert Comment

by:lwadwell
ID: 38302933
In PHP there is the SimpleXML library to handle XML ... is that what you want?

http://php.net/manual/en/book.simplexml.php
0
 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 38303012
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.
0
 

Author Comment

by:cnermin
ID: 38303027
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',
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `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`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Erdinç Güngör Çorbacı
ID: 38303136
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
0
 
LVL 25

Accepted Solution

by:
lwadwell earned 2000 total points
ID: 38303309
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);
        $stmt->bind_param('isdiiii',$art_broj,$naziv,$cijena,$tarifa,$sank,$kuhinja,$SL);

          $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;
            $stmt->execute();
            $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

0
 

Author Closing Comment

by:cnermin
ID: 38303412
nice
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

621 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