使用php将XML转换为MySQL


XML to MySQL with php

我的xml中有一个字段,如下所示:

<radar snb="09H0000" ver="1023" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="icomsData.xsd">

如何获取变量"xsi:noNamespaceSchemaLocation"

我获得"snb"answers"ver"的代码运行良好,但不适用于其他两个:/

我的代码:

    $fichier = 'test.xml'; 
    $radar = simplexml_load_file($fichier);
    echo '</br> Radar : 
        </br> snb : <strong>'.$radar['snb'].' </strong>ver : <strong>'.$radar['ver'].'</strong> xmlns:xsi : <strong>'.$radar['xmlns:xsi'].' </strong> xsi:noNamespaceSchemaLocation : <strong>'.$radar['xsi:noNamespaceSchemaLocation'].'</strong>';

我试过用"它不会改变任何东西。

您可以使用此代码访问xml文件中的变量:

<?php
$xmlFile = 'test.xml';
$xml = simplexml_load_file($xmlFile);
$radarAttr = $xml->attributes();
$noNamespaceSchemaLocation = $xml->attributes('xsi', true)->noNamespaceSchemaLocation->__tostring();
$radarSnb  = $radarAttr['snb']->__tostring();
$radarVer  = $radarAttr['ver']->__tostring();
foreach($xml->log as $log) {
    $LogAttrs = $log->attributes();
    $logTs = $LogAttrs['ts']->__tostring();
    $logVb = $LogAttrs['vb']->__tostring();
    $site = $log->site->__tostring();
    $evArray = $log->ev;
    foreach($evArray as $ev) {
        $EvAttrs = $ev->attributes();
        $evTs = $EvAttrs['ts']->__tostring();
        $evData = $ev->children();
        $ev_sp = $evData->sp->__tostring();
        $ev_lg = $evData->lg->__tostring();
        $ev_dir = $evData->dir->__tostring();
    }

}

对于此解决方案,您需要采取以下步骤

  1. 首先编写一个脚本,将所有xml文件转换为CSV格式
  2. 制作一个将CSV文件上传到DB的脚本