Browse Source

Guardar en Base de datos archivo XML

master
Bartt0 2 years ago
parent
commit
cf7f837a19
6 changed files with 135 additions and 104609 deletions
  1. +42
    -0
      app/Http/Controllers/SaveXmlController.php
  2. +12
    -0
      app/Models/XmlData.php
  3. +33
    -0
      database/migrations/2022_11_04_120522_create_xml_data_table.php
  4. +1
    -104609
      public/places.xml
  5. +43
    -0
      resources/views/xml-data.blade.php
  6. +4
    -0
      routes/web.php

+ 42
- 0
app/Http/Controllers/SaveXmlController.php View File

@@ -0,0 +1,42 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\XmlData;

class SaveXmlController extends Controller
{
public function index(Request $req)
{
if($req->isMethod("POST")){

$xmlDataString = file_get_contents(public_path('places.xml'));
$xmlObject = simplexml_load_string($xmlDataString);

$json = json_encode($xmlObject);
$phpDataArray = json_decode($json, true);



if(count($phpDataArray['place']) > 0){

$dataArray = array();

foreach($phpDataArray['place'] as $index => $data){

$dataArray[] = [
"place place_id" => $data['place_id'],
"name" => $data['name'],
];
}

XmlData::insert($dataArray);

return back()->with('success','Data saved successfully!');
}
}

return view("xml-data");
}
}

+ 12
- 0
app/Models/XmlData.php View File

@@ -0,0 +1,12 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class XmlData extends Model
{
use HasFactory;
public $timestamps = false;
}

+ 33
- 0
database/migrations/2022_11_04_120522_create_xml_data_table.php View File

@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateXmlDataTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('xml_data', function (Blueprint $table) {
$table->id();
$table->integer("place_id");
$table->string("name", 250);

});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('xml_data');
}
}

+ 1
- 104609
public/places.xml
File diff suppressed because it is too large
View File


+ 43
- 0
resources/views/xml-data.blade.php View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>arquitectura</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<style>
#frm-create-post label.error{
color:red;
}
</style>
</head>

<body>

<div class="container" style="margin-top: 50px;">
<h4 style="text-align: center;">Cómo cargar y guardar datos XML</h4>

@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
@endif

<form action="{{ route('xml-upload') }}" id="frm-create-course" method="post">
@csrf
<div class="form-group">
<label for="file">Seleccionar xml:</label>
<input type="file" class="form-control" required id="file" name="file">
</div>

<button type="submit" class="btn btn-primary" id="submit-post">enviar</button>
</form>
</div>
</body>

</html>

+ 4
- 0
routes/web.php View File

@@ -2,6 +2,7 @@

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReadXmlController;
use App\Http\Controllers\SaveXmlController;

/*
|--------------------------------------------------------------------------
@@ -16,6 +17,9 @@ use App\Http\Controllers\ReadXmlController;
Route::get("read-xml", [ReadXmlController::class, "index"]);


Route::match(["get", "post"], "save-xml", [SaveXmlController::class, "index"])->name('xml-upload');


Route::get('/', function () {
return view('welcome');
});

Loading…
Cancel
Save