modificare un file .XML da database Sql

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
Buongiorno, per lavoro devo modificare un file XML complesso con molte righe che ha la sreguente struttura:

Codice:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
 <DATAPACKET Version="2.0">
 <METADATA><FIELDS>
 <FIELD attrname="Id" fieldtype="i4" readonly="true" SUBTYPE="Autoinc"/>
 <FIELD attrname="Blocco" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Domanda" fieldtype="string" WIDTH="2"/>
 <FIELD attrname="Risposta" fieldtype="boolean"/>
 <FIELD attrname="Capitolo" fieldtype="string" WIDTH="2"/>
 <FIELD attrname="Indice" fieldtype="string" WIDTH="3"/>
 <FIELD attrname="Argomento" fieldtype="string" WIDTH="1"/>
 <FIELD attrname="SubArgomento" fieldtype="string" WIDTH="2"/>
 <FIELD attrname="Figura" fieldtype="string" WIDTH="4"/>
 <FIELD attrname="FiguraBlk" fieldtype="string" WIDTH="4"/>
 <FIELD attrname="Difficolta" fieldtype="string" WIDTH="2"/>
 <FIELD attrname="Testo" fieldtype="string" WIDTH="320"/>
 <FIELD attrname="Lingua1" fieldtype="string" WIDTH="320"/>
 <FIELD attrname="Lingua2" fieldtype="string" WIDTH="320"/>
 <FIELD attrname="Lingua3" fieldtype="string" WIDTH="320"/>
 <FIELD attrname="Commento" fieldtype="string" WIDTH="256"/>
 <FIELD attrname="Aiuto" fieldtype="string" WIDTH="128"/>
 <FIELD attrname="Foto1" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Foto2" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Foto3" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Foto4" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Foto5" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Video1" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Edl1" fieldtype="string" WIDTH="15"/>
 <FIELD attrname="Video2" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Edl2" fieldtype="string" WIDTH="15"/>
 <FIELD attrname="Video3" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Edl3" fieldtype="string" WIDTH="15"/>
 <FIELD attrname="Audio1" fieldtype="string" WIDTH="12"/>
 <FIELD attrname="Audio2" fieldtype="string" WIDTH="12"/>
 <FIELD attrname="Audio3" fieldtype="string" WIDTH="12"/>
 <FIELD attrname="Html1" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Html2" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Html3" fieldtype="string" WIDTH="5"/><FIELD attrname="Libro1" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Libro1PosY" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Libro2" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Libro2PosY" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Libro3" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Libro3PosY" fieldtype="string" WIDTH="5"/>
 <FIELD attrname="Info1" fieldtype="string" WIDTH="120"/>
 <FIELD attrname="Info2" fieldtype="string" WIDTH="120"/>
 <FIELD attrname="Gruppo1" fieldtype="string" WIDTH="3"/>
 <FIELD attrname="Gruppo2" fieldtype="string" WIDTH="3"/>
 <FIELD attrname="Gruppo3" fieldtype="string" WIDTH="3"/>
 </FIELDS><PARAMS AUTOINCVALUE="7166"/></METADATA>
 <ROWDATA>
 <ROW Id="2" Blocco="11023" Domanda="02" Risposta="TRUE" Capitolo="01" Indice="A01" Argomento="A" SubArgomento="1" Figura="" FiguraBlk="" Difficolta="6" Testo="I ciclomotori possono avere due o tre ruote" Lingua1="Les motocycles légers peuvent avoir deux ou trois roues" Lingua2="Kleinkrafträder können zwei oder drei Räder haben" Lingua3="I ciclomotori possono avere due o tre ruote" Commento="infatti i CICLOMOTORI possono avere DUE, TRE e anche QUATTRO RUOTE, cilindrata fino a 50 cm³ e velocità fino a 45 km/h." Aiuto="Classificazione dei veicoli." Foto1="3113" Foto2="" Foto3="" Foto4="" Foto5="" Video1="" Video2="" Video3="" Audio1="04023_40231" Audio2="" Audio3="" Html1="" Html2="" Html3="" Libro1="1" Libro2="1" Libro3="" Info1="11023" Info2="Ciclomotori"/>
 <ROW Id="3" Blocco="11023" Domanda="03" Risposta="TRUE" Capitolo="01" Indice="A01" Argomento="A" SubArgomento="1" Figura="" FiguraBlk="" Difficolta="5" Testo="Non tutti i veicoli a motore a due ruote vengono classificati ciclomotori" Lingua1="Pas tous les véhicules à moteur à deux roues peuvent être classifiés des motocycles légers" Lingua2="Nicht alle zweirädrigen Kraftfahrzeuge werden als Kleinkrafträder eingestuft" Lingua3="Non tutti i veicoli a motore a due ruote vengono classificati ciclomotori" Commento="infatti vengono CLASSIFICATI CICLOMOTORI solo i veicoli a DUE RUOTE con CILINDRATA NON SUPERIORE a 50 cm³ e VELOCITÀ NON SUPERIORE a 45 km/h." Aiuto="Classificazione dei veicoli." Foto1="1238" Foto2="" Foto3="" Foto4="" Foto5="" Video1="" Video2="" Video3="" Audio1="04023_40232" Audio2="" Audio3="" Html1="" Html2="" Html3="" Libro1="1" Libro2="1" Libro3="" Info1="11023" Info2="Ciclomotori"/>...

in questo file all interno di ROW ho il mio dato damodificare e per ogni riga (ROW) del mio XML devo prelevare dal mio Database sql nella tabella quiz il campo "Libro3" e aggiornarlo per ogni riga, per riconoscere la riga ho il campo Id e blocco del database che corrispondono a Id e blocco dell' XML.

Come posso fare tramite php a modificare tutte le righe del file aggiornando il dato Libro3 che arriva dal mio DB?
 

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
Cioè??

i dati li estraggo così:
Codice:
$conn = myDB::open();


$sql="SELECT `Id`,`Blocco`,`Libro3` FROM `quiz` WHERE `listato` LIKE 'AB1' ORDER BY `quiz`.`Libro3` ASC";


$result = $conn->query($sql);

$xml=simplexml_load_file("NQM2016.1.XML");

poi non so più andare avanti!
 

MarcoGrazia

Utente Attivo
15 Dic 2009
852
20
28
62
Udine
www.stilisticamente.com
Una volta che $result ha dati consistenti, cioè non è vuoto, apri la connessione al file XML e lo modifichi.
PHP:
if ( $result !== false )
{
  $doc = new DOMDocument();
$doc->loadHTMLFile( $xml_file );
  $xpath = new DOMXpath( $doc );
  //  Ora viene il bello: tramite xpath ti devi cercare il punto da modificare
  foreach( $result as $res )
  {
     $elements = $xpath->query(  "*/div[@id='" $res['Id'] . '"]" );
     foreach( $elements as $el )
     {
       if ( $el == ID CERCATO )
       {
          //  MODIFICO IL BLOCCO
          //  DA QUI SPERO TI AIUTI QUALCUNO, PERCHE' HO LASCIATO I FILE XML DA TEMPO, TROPPO COMPLESSI DA GESTIRE :-D
       }
  }
}
 
  • Like
Reactions: Maxilboss93

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
Una volta che $result ha dati consistenti, cioè non è vuoto, apri la connessione al file XML e lo modifichi.
PHP:
if ( $result !== false )
{
  $doc = new DOMDocument();
$doc->loadHTMLFile( $xml_file );
  $xpath = new DOMXpath( $doc );
  //  Ora viene il bello: tramite xpath ti devi cercare il punto da modificare
  foreach( $result as $res )
  {
     $elements = $xpath->query(  "*/div[@id='" $res['Id'] . '"]" );
     foreach( $elements as $el )
     {
       if ( $el == ID CERCATO )
       {
          //  MODIFICO IL BLOCCO
          //  DA QUI SPERO TI AIUTI QUALCUNO, PERCHE' HO LASCIATO I FILE XML DA TEMPO, TROPPO COMPLESSI DA GESTIRE :-D
       }
  }
}
Mi da errore sul $doc->loadHTMLFile( $xml_file ); io Ho inserito il mio nome del file xml ma mi da questo errore:
Warning: DOMDocument::loadHTMLFile(): Tag datapacket invalid in NQM2016.1.XML
 

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
Non so che dirti.


Codice:
require('../includes/config.php');
   
$conn = myDB::open();

$sql="SELECT `Id`,`Blocco`,`Libro3` FROM `quiz` WHERE `listato` LIKE 'AB1' ORDER BY `quiz`.`Libro3` ASC";

$result = $conn->query($sql);


if ( $result !== false )
{
  $doc = new DOMDocument();
$doc->loadHTMLFile("NQM2016.1.XML");
  $xpath = new DOMXpath( $doc );
  //  Ora viene il bello: tramite xpath ti devi cercare il punto da modificare
  foreach( $result as $res )  {
     $elements = $xpath->query(  "*/div[@id='" .$res['Id'] . '"]"');
     foreach( $elements as $el ){
       if ( $el == $res['Id'] ){
           echo($res['Id']);
          //  MODIFICO IL BLOCCO
          //  DA QUI SPERO TI AIUTI QUALCUNO, PERCHE' HO LASCIATO I FILE XML DA TEMPO, TROPPO COMPLESSI DA GESTIRE :-D
       }
        }
    }
}


[CODE]
 

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
TROVATO IL MODOOOOOOOOOOOOOOOOOOOOOOOOO!!!

Codice:
while($row = mysqli_fetch_array($result)){
    
    $array1=JSON_encode($row);
    $xml=simplexml_load_file("NQM2016.1.XML");
    $array=json_encode($xml);
    $strings = $xml->xpath("//ROW");

   foreach ($strings as $string){
       if($string['Id']== $row["Id"]){
           echo $string['Id'] ." -> ".$row['Libro3']."<br>";
       }
   }
}

Ora devo solo trovare il modo di scrivere nell' XML
 

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
TROVATO IL MODOOOOOOOOOOOOOOOOOOOOOOOOO!!!

Codice:
while($row = mysqli_fetch_array($result)){
   
    $array1=JSON_encode($row);
    $xml=simplexml_load_file("NQM2016.1.XML");
    $array=json_encode($xml);
    $strings = $xml->xpath("//ROW");

   foreach ($strings as $string){
       if($string['Id']== $row["Id"]){
           echo $string['Id'] ." -> ".$row['Libro3']."<br>";
       }
   }
}

Ora devo solo trovare il modo di scrivere nell' XML
Pulito il codice è così!

Codice:
$conn = myDB::open();


$sql="SELECT `Id`,`Blocco`,`Libro3` FROM `quiz` WHERE `listato` LIKE 'AB1' ORDER BY `quiz`.`Libro3` ASC";

$result = $conn->query($sql);
$xml=simplexml_load_file("NQM2016.1.XML");

while($row = mysqli_fetch_array($result)){
    
    $strings = $xml->xpath("//ROW");
   foreach ($strings as $string){
       if($string['Id']== $row["Id"]){
           echo $string['Id'] ." -> ".$row['Libro3']."<br>";
       }
   }
}
 

Maxilboss93

Nuovo Utente
7 Gen 2020
15
1
3
Che dici? Hai trovato il modo? :p

A parte gli scherzi, bravo. Ora saprai che mettere le mani in un file XML può essere un casino :D

guarda se vuoi ancora darmi una mano ho un problema con la scrittura all' interno dell' attributo:

Codice:
<?php

require('../includes/config.php');

   
$conn = myDB::open();


$sql="SELECT `Id`,`Blocco`,`Libro3` FROM `quiz` WHERE `listato` LIKE 'AB1' ORDER BY `quiz`.`Libro3` ASC";

$result = $conn->query($sql);
$xml=simplexml_load_file("NQM2016.1.XML");
$att = 'Libro3';
while($row = mysqli_fetch_array($result)){
    
    $strings = $xml->xpath("//ROW");
   foreach ($strings as $string){
       
       if($string['Id']== $row["Id"]){
        //   echo $string['Id'] ." -> ".$row['Libro3']."<br>";
           $string['Libro3'] .= '"'.$row['Libro3'].'"';
           echo("OK");
           $string->attributes()->$att = 'New value of the attribute';
           echo $xml->asXML();
       }
   }
  
}
 
Discussioni simili
Autore Titolo Forum Risposte Data
IImanuII Modificare file xml PHP 1
J Come inserire una jpeg in un file XML o modificare il Font Flash 2
Q impossibile modificare un file ....... C/C++ 2
paloppa Bootstrap.min.css modificare il file HTML e CSS 6
Merlina3377 [PHP] modificare file su form immagine thump PHP 1
G Access in VBA - Come si fa a modificare testo nel'intestazione di file Word MS Access 0
M Modificare le variabili di un file php attraverso un form PHP 1
S Aprire e modificare file SCT Programmazione 2
C Fare modificare un file txt da utenti registrati Classic ASP 1
rok Non posso modificare un file flash perchè... Flash 1
Maverick1000tt Modificare file Flash 1
T Modificare file .swf Flash 12
M [cerco webmaster] sito web da modificare Offerte e Richieste di Lavoro e/o Collaborazione 0
G Come modificare un pdf in php PHP 1
napuleone modificare il valore dellascroll bar di webkit HTML e CSS 8
Shyson Modificare codice e creare link PHP 0
J Modificare panorami 360 ps 2020 Photoshop 0
napuleone modificare il contenitore di un div Javascript 2
napuleone modificare il contenitore di un div Javascript 1
D modificare questo codice per inserimento in text e non in tabella jQuery 1
C modificare sito in locale con dreamWeaver HTML e CSS 3
T Modificare ruolo utenti in fase di registrazione E-Commerce 0
S Modificare intestazione documento pdf con LO Draw Linux e Software 0
felino Radio button e Bootstrap: modificare il colore di default! HTML e CSS 1
Shyson Modificare funzione php PHP 15
W Modificare il Type di un Input box in javascript ovunque si trovi Javascript 0
F Modificare report di etichette di access con vba MS Access 0
D [PHP] Tabella: modificare direttamente i valori PHP 6
P [Woocommerce] Modificare il Bottone Checkout di Woocommerce in base alla categoria di prodotto E-Commerce 10
C [HTML] modificare la scritta della copyright HTML e CSS 2
G [PHP] MODIFICARE DATI TABELLA CONTESTUALMENTE PHP 6
Y [PHP] [HTML] Modificare i quadranti PHP 1
A [PHP] Modificare campo database in base alla scelta dell'utente PHP 2
W Non riesco più a modificare i dati di una tabella da phpmyadmin MySQL 0
V [WordPress] Come modificare link in tasto menu css WordPress 21
Y Modificare script php PHP 5
G Fullcalendar: modificare colore testo, backgroup e bordi per tutti gli eventi jQuery 3
G [HTML] Modificare ordine dei div in mobile responsive HTML e CSS 5
pfranco [PHP] Modificare pagina web online PHP 9
Shyson Modificare codice jQuery 0
A [Javascript] modificare il contenuto della barra indirizzi di un browser Javascript 1
asevenx modificare lista elementi togliendo l'elemento selezionato jQuery 1
Kolop "Modificare HTML" HTML e CSS 13
K [ASP.Net] Utilizzare e modificare dati linq ASP.NET 0
G Modificare navbar in base allo scroll jQuery 6
R [Photoshop] Modificare luminosità dei bordi in una foto Photoshop 2
A come modificare gli elementi di una lista e gli editText? Sviluppo app per Android 0
A modificare .jar con classe personalizzata Java 9
Mer556 BLOCCATO nel modificare un sito HTML e CSS 3
T modificare un template html5 HTML e CSS 6

Discussioni simili