php - How can i insert data from an XML into a database -


this question has answer here:

i want insert following xml file mysql database using php

<?xml version="1.0" encoding="utf-8"?> <printletterbarcodedata      uid="725733706873"      name="ravinder kumar"      gender="m"      yob="1996"      co="s/o kaka ram"      house="460a"      street="ward no. 6"      lm="na"      loc="na"      vtc="nanyola (292)"      po="naneola"      dist="ambala"      state="haryana"      pc="134003" /> 

how extract data xml file insert them database ?

for php approach, find following useful:

<?php $string = <<<xml   <printletterbarcodedata      uid="725733706873"      name="ravinder kumar"      gender="m"      yob="1996"      co="s/o kaka ram"      house="460a"      street="ward no. 6"      lm="na"      loc="na"      vtc="nanyola (292)"      po="naneola"      dist="ambala"      state="haryana"      pc="134003"   /> xml;   $xml = simplexml_load_string($string); $attribs = $xml->attributes(); // convert '$attribs' array foreach($attribs $key=>$val) {     $arrayofattribs[(string)$key] = "'".(string)$val."'"; } $namesofcolumns = implode(",", array_keys($arrayofattribs)); $valuesofcolumns = implode(",", array_values($arrayofattribs));  // database stuff $query = "insert yourtable ($namesofcolumns) values ($valuesofcolumns);"; ?> 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -