我想使用php将以下XML文件插入到Mysql数据库中
<?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"
/>
如何从此XML文件中提取数据以将其插入数据库?
答案 0 :(得分:-1)
对于PHP方法,您会发现以下内容:
<?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 the '$attribs' to an array
foreach($attribs as $key=>$val) {
$arrayOfAttribs[(string)$key] = "'".(string)$val."'";
}
$namesOfColumns = implode(",", array_keys($arrayOfAttribs));
$valuesOfColumns = implode(",", array_values($arrayOfAttribs));
// your database stuff
$query = "INSERT INTO yourtable ($namesOfColumns) VALUES ($valuesOfColumns);";
?>