TransWikia.com

¿como puedo insertar datos de un xml a mysql con php?

Stack Overflow en español Asked by ivanrangel on January 19, 2021

lo que pasa esque genere un .bat que que recopilara los datos de mi cpu y los datos me los gererara en un formato xml con el siguiente codigo

wmic cpu get /all /format:rawxml.xsl >cpu.xml

y me genero el siguiente xml

<COMMAND SEQUENCENUM="1" ISSUEDFROM="IVAN-PC" STARTTIME="07-29-2017T18:09:21" EVERYCOUNT="0"><REQUEST><COMMANDLINE>  cpu get /all /format:rawxml.xsl </COMMANDLINE>
<COMMANDLINECOMPONENTS><NODELIST><NODE>IVAN-PC</NODE>
</NODELIST>
</COMMANDLINECOMPONENTS>
<CONTEXT><NAMESPACE>rootcimv2</NAMESPACE>
<ROLE>rootcli</ROLE>
<IMPLEVEL>IMPERSONATE</IMPLEVEL>
<AUTHLEVEL>PKTPRIVACY</AUTHLEVEL>
<LOCALE>ms_c0a</LOCALE>
<PRIVILEGES>ENABLE</PRIVILEGES>
<TRACE>OFF</TRACE>
<RECORD>N/A</RECORD>
<INTERACTIVE>OFF</INTERACTIVE>
<FAILFAST>OFF</FAILFAST>
<OUTPUT>STDOUT</OUTPUT>
<APPEND>STDOUT</APPEND>
<USER>N/A</USER>
<AGGREGATE>ON</AGGREGATE>
</CONTEXT>
</REQUEST>
<RESULTS NODE="IVAN-PC"><CIM><INSTANCE CLASSNAME="Win32_Processor"><PROPERTY NAME="AddressWidth" CLASSORIGIN="CIM_Processor" TYPE="uint16"><VALUE>64</VALUE>
</PROPERTY>
<PROPERTY NAME="Architecture" CLASSORIGIN="Win32_Processor" TYPE="uint16"><VALUE>9</VALUE>
</PROPERTY>
<PROPERTY NAME="Availability" CLASSORIGIN="CIM_LogicalDevice" TYPE="uint16"><VALUE>3</VALUE>
</PROPERTY>
<PROPERTY NAME="Caption" CLASSORIGIN="CIM_ManagedSystemElement" TYPE="string"><VALUE>AMD64 Family 16 Model 6 Stepping 3</VALUE>
</PROPERTY>
<PROPERTY NAME="ConfigManagerErrorCode" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="uint32"></PROPERTY>
<PROPERTY NAME="ConfigManagerUserConfig" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="boolean"></PROPERTY>
<PROPERTY NAME="CpuStatus" CLASSORIGIN="Win32_Processor" TYPE="uint16"><VALUE>1</VALUE>
</PROPERTY>
<PROPERTY NAME="CreationClassName" CLASSORIGIN="CIM_LogicalDevice" TYPE="string"><VALUE>Win32_Processor</VALUE>
</PROPERTY>
<PROPERTY NAME="CurrentClockSpeed" CLASSORIGIN="CIM_Processor" TYPE="uint32"><VALUE>1700</VALUE>
</PROPERTY>
<PROPERTY NAME="CurrentVoltage" CLASSORIGIN="Win32_Processor" TYPE="uint16"><VALUE>11</VALUE>
</PROPERTY>
<PROPERTY NAME="DataWidth" CLASSORIGIN="CIM_Processor" TYPE="uint16"><VALUE>64</VALUE>
</PROPERTY>
<PROPERTY NAME="Description" CLASSORIGIN="CIM_ManagedSystemElement" TYPE="string"><VALUE>AMD64 Family 16 Model 6 Stepping 3</VALUE>
</PROPERTY>
<PROPERTY NAME="DeviceID" CLASSORIGIN="CIM_LogicalDevice" TYPE="string"><VALUE>CPU0</VALUE>
</PROPERTY>
<PROPERTY NAME="ErrorCleared" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="boolean"></PROPERTY>
<PROPERTY NAME="ErrorDescription" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="string"></PROPERTY>
<PROPERTY NAME="ExtClock" CLASSORIGIN="Win32_Processor" TYPE="uint32"><VALUE>200</VALUE>
</PROPERTY>
<PROPERTY NAME="Family" CLASSORIGIN="CIM_Processor" TYPE="uint16"><VALUE>237</VALUE>
</PROPERTY>
<PROPERTY NAME="InstallDate" CLASSORIGIN="CIM_ManagedSystemElement" PROPAGATED="true" TYPE="datetime"></PROPERTY>
<PROPERTY NAME="L2CacheSize" CLASSORIGIN="Win32_Processor" TYPE="uint32"><VALUE>1024</VALUE>
</PROPERTY>
<PROPERTY NAME="L2CacheSpeed" CLASSORIGIN="Win32_Processor" PROPAGATED="true" TYPE="uint32"></PROPERTY>
<PROPERTY NAME="L3CacheSize" CLASSORIGIN="Win32_Processor" TYPE="uint32"><VALUE>0</VALUE>
</PROPERTY>
<PROPERTY NAME="L3CacheSpeed" CLASSORIGIN="Win32_Processor" TYPE="uint32"><VALUE>0</VALUE>
</PROPERTY>
<PROPERTY NAME="LastErrorCode" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="uint32"></PROPERTY>
<PROPERTY NAME="Level" CLASSORIGIN="Win32_Processor" TYPE="uint16"><VALUE>16</VALUE>
</PROPERTY>
<PROPERTY NAME="LoadPercentage" CLASSORIGIN="CIM_Processor" PROPAGATED="true" TYPE="uint16"></PROPERTY>
<PROPERTY NAME="Manufacturer" CLASSORIGIN="Win32_Processor" TYPE="string"><VALUE>AuthenticAMD</VALUE>
</PROPERTY>
<PROPERTY NAME="MaxClockSpeed" CLASSORIGIN="CIM_Processor" TYPE="uint32"><VALUE>1700</VALUE>
</PROPERTY>
<PROPERTY NAME="Name" CLASSORIGIN="CIM_ManagedSystemElement" TYPE="string"><VALUE>AMD Athlon(tm) II Neo K125 Processor</VALUE>
</PROPERTY>
<PROPERTY NAME="NumberOfCores" CLASSORIGIN="Win32_Processor" TYPE="uint32"><VALUE>1</VALUE>
</PROPERTY>
<PROPERTY NAME="NumberOfLogicalProcessors" CLASSORIGIN="Win32_Processor" TYPE="uint32"><VALUE>1</VALUE>
</PROPERTY>
<PROPERTY NAME="OtherFamilyDescription" CLASSORIGIN="CIM_Processor" PROPAGATED="true" TYPE="string"></PROPERTY>
<PROPERTY NAME="PNPDeviceID" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="string"></PROPERTY>
<PROPERTY.ARRAY NAME="PowerManagementCapabilities" CLASSORIGIN="CIM_LogicalDevice" PROPAGATED="true" TYPE="uint16"></PROPERTY.ARRAY>
<PROPERTY NAME="PowerManagementSupported" CLASSORIGIN="CIM_LogicalDevice" TYPE="boolean"><VALUE>FALSE</VALUE>
</PROPERTY>
<PROPERTY NAME="ProcessorId" CLASSORIGIN="Win32_Processor" TYPE="string"><VALUE>078BFBFF00100F63</VALUE>
</PROPERTY>
<PROPERTY NAME="ProcessorType" CLASSORIGIN="Win32_Processor" TYPE="uint16"><VALUE>3</VALUE>
</PROPERTY>
<PROPERTY NAME="Revision" CLASSORIGIN="Win32_Processor" TYPE="uint16"><VALUE>1539</VALUE>
</PROPERTY>
<PROPERTY NAME="Role" CLASSORIGIN="CIM_Processor" TYPE="string"><VALUE>CPU</VALUE>
</PROPERTY>
<PROPERTY NAME="SocketDesignation" CLASSORIGIN="Win32_Processor" TYPE="string"><VALUE>Socket M2/S1G1</VALUE>
</PROPERTY>
<PROPERTY NAME="Status" CLASSORIGIN="CIM_ManagedSystemElement" TYPE="string"><VALUE>OK</VALUE>
</PROPERTY>
<PROPERTY NAME="StatusInfo" CLASSORIGIN="CIM_LogicalDevice" TYPE="uint16"><VALUE>3</VALUE>
</PROPERTY>
<PROPERTY NAME="Stepping" CLASSORIGIN="CIM_Processor" TYPE="string"><VALUE>3</VALUE>
</PROPERTY>
<PROPERTY NAME="SystemCreationClassName" CLASSORIGIN="CIM_LogicalDevice" TYPE="string"><VALUE>Win32_ComputerSystem</VALUE>
</PROPERTY>
<PROPERTY NAME="SystemName" CLASSORIGIN="CIM_LogicalDevice" TYPE="string"><VALUE>IVAN-PC</VALUE>
</PROPERTY>
<PROPERTY NAME="UniqueId" CLASSORIGIN="CIM_Processor" PROPAGATED="true" TYPE="string"></PROPERTY>
<PROPERTY NAME="UpgradeMethod" CLASSORIGIN="CIM_Processor" TYPE="uint16"><VALUE>6</VALUE>
</PROPERTY>
<PROPERTY NAME="Version" CLASSORIGIN="Win32_Processor" TYPE="string"><VALUE>Modelo 6, nivel 3</VALUE>
</PROPERTY>
<PROPERTY NAME="VoltageCaps" CLASSORIGIN="Win32_Processor" PROPAGATED="true" TYPE="uint32"></PROPERTY>
</INSTANCE>
</CIM>
</RESULTS>
</COMMAND>

y lo que quiero es que lo que esta en values llevarlo a un formulario y introducirlos en una base de datos o pasarlo directamente al php alguien sabe como puedo hacer esto?

One Answer

Utiliza la siguiente funcion

function loadXml($str, &$errors = array()){
    $bk = libxml_use_internal_errors(true);
    $xmlDom = simplexml_load_string($str);
    if (!$xmlDom)
        $xmlDom = simplexml_load_string(mb_convert_encoding($str, "UTF-8"));

    if (!$xmlDom){
        foreach(libxml_get_errors() as $error){
            $errors[] = "error >> level: {$error->level}, code: {$error->code}, line: {$error->line}, column: {$error->column}, message: {$error->message}";
        }
        libxml_clear_errors();
    }
    libxml_use_internal_errors($bk);

    return $xmlDom;
}

Luego haces

$xmlDom = loadXml(file_get_contents("cpu.xml"), $errors);
if (!$xmlDom){
    // Manejar los errores aqui. Los errores esta en $errors
}

// Leer las variables xml
$dict = array();
foreach ($xmlDom->xpath("//PROPERTY") as $prop){
    $name = $prop['NAME'];
    $value = (string)$prop->VALUE;
    $dict[$name] = $value;
}

var_dump($dict);

Aplicado a tu xml de ejemplo devuelve

array(47) {
  ["AddressWidth"]=>
  string(2) "64"
  ["Architecture"]=>
  string(1) "9"
  ["Availability"]=>
  string(1) "3"
  ["Caption"]=>
  string(34) "AMD64 Family 16 Model 6 Stepping 3"
  ["ConfigManagerErrorCode"]=>
  string(0) ""
  ["ConfigManagerUserConfig"]=>
  string(0) ""
  ["CpuStatus"]=>
  string(1) "1"
  ["CreationClassName"]=>
  string(15) "Win32_Processor"
  ["CurrentClockSpeed"]=>
  string(4) "1700"
  ["CurrentVoltage"]=>
  string(2) "11"
  ["DataWidth"]=>
  string(2) "64"
  ["Description"]=>
  string(34) "AMD64 Family 16 Model 6 Stepping 3"
  ["DeviceID"]=>
  string(4) "CPU0"
  ["ErrorCleared"]=>
  string(0) ""
  ["ErrorDescription"]=>
  string(0) ""
  ["ExtClock"]=>
  string(3) "200"
  ["Family"]=>
  string(3) "237"
  ["InstallDate"]=>
  string(0) ""
  ["L2CacheSize"]=>
  string(4) "1024"
  ["L2CacheSpeed"]=>
  string(0) ""
  ["L3CacheSize"]=>
  string(1) "0"
  ["L3CacheSpeed"]=>
  string(1) "0"
  ["LastErrorCode"]=>
  string(0) ""
  ["Level"]=>
  string(2) "16"
  ["LoadPercentage"]=>
  string(0) ""
  ["Manufacturer"]=>
  string(12) "AuthenticAMD"
  ["MaxClockSpeed"]=>
  string(4) "1700"
  ["Name"]=>
  string(36) "AMD Athlon(tm) II Neo K125 Processor"
  ["NumberOfCores"]=>
  string(1) "1"
  ["NumberOfLogicalProcessors"]=>
  string(1) "1"
  ["OtherFamilyDescription"]=>
  string(0) ""
  ["PNPDeviceID"]=>
  string(0) ""
  ["PowerManagementSupported"]=>
  string(5) "FALSE"
  ["ProcessorId"]=>
  string(16) "078BFBFF00100F63"
  ["ProcessorType"]=>
  string(1) "3"
  ["Revision"]=>
  string(4) "1539"
  ["Role"]=>
  string(3) "CPU"
  ["SocketDesignation"]=>
  string(14) "Socket M2/S1G1"
  ["Status"]=>
  string(2) "OK"
  ["StatusInfo"]=>
  string(1) "3"
  ["Stepping"]=>
  string(1) "3"
  ["SystemCreationClassName"]=>
  string(20) "Win32_ComputerSystem"
  ["SystemName"]=>
  string(7) "IVAN-PC"
  ["UniqueId"]=>
  string(0) ""
  ["UpgradeMethod"]=>
  string(1) "6"
  ["Version"]=>
  string(17) "Modelo 6, nivel 3"
  ["VoltageCaps"]=>
  string(0) ""
}     

A partir de ahi puedes hacer lo que quieras, por ejemplo, insertar los valores en una base de datos Mysql.

Answered by Abraham TS on January 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP