Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2831 connectés 

  FORUM HardWare.fr
  Programmation
  PHP

  Parser fichier XML

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Parser fichier XML

n°1899344
Stuntman
Posté le 26-06-2009 à 10:18:58  profilanswer
 

Salut, a tous je tente de realise un parsage de fichier XML que je recupere d'une machine sur le reseau, mais j'ai quelques soucis...

Code :
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. - <runtime-data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:cli_show_output.xsd" display-level="normal">
  3. - <hierarchy name="show" type="static">
  4. - <hierarchy name="xdsl" type="static">
  5. - <hierarchy name="operational-data" type="static">
  6. - <hierarchy name="line" type="static">
  7.   <hier-id name="if-index" is-named="no" type="Itf::XdslLineAndChannelItf::XdslLineAndChannel">1/1/4/1</hier-id>
  8. - <instance>
  9.   <res-id name="if-index" short-name="if-index" type="Itf::XdslLineAndChannelItf::XdslLineAndChannel">1/1/4/1</res-id>
  10.   <info name="adm-state" short-name="adm-state" type="Itf::ifAdminStatus">up</info>
  11.   <info name="opr-state/tx-rate-ds" short-name="opr-state/tx-rate-ds" type="Xdsl::combinedCol">up :24252</info>
  12.   <info name="tx-rate/us" short-name="tx-rate/us" type="Xdsl::bitrate">1189</info>
  13.   <info name="tx-rate/ds" short-name="tx-rate/ds" type="Xdsl::bitrate">24252</info>
  14.   <info name="cur-op-mode" short-name="cur-op-mode" type="Xdsl::OneOpModeTypeLinePreDef">g992-5-a</info>
  15.   </instance>
  16.   </hierarchy>
  17.   </hierarchy>
  18.   </hierarchy>
  19.   </hierarchy>
  20.   </runtime-data>


Voila le fichier XML qu je recupere. Mon soucis est que le nom des elements est compose, genre cur-op-mode. Du coup la variable n'est pas accepter quand je parse le document cf. mon code.

Code :
  1. <?php
  2. $dom = new DomDocument();
  3. $dom->load('line.xml');
  4. //$dom->save('line.xml');
  5. //$dom->validate();
  6. $line = $dom->getElementsByTagName('if-index');
  7. foreach($line as $if-index)
  8. echo $if-index->firstChild->nodeValue . "<br />";
  9. echo "---<br />";
  10. ?>


Ya t-il un moyen de pourvoir tout de meme utiliser ce nom de variable en la rendant valide par le parser? Car il m'est impossible de modify le format du fichier XML recu!
Ma source pour ce parse : http://eusebius.developpez.com/php5dom/  
Merci d'avance.


Message édité par Stuntman le 29-06-2009 à 14:02:43
mood
Publicité
Posté le 26-06-2009 à 10:18:58  profilanswer
 

n°1899348
SICKofitAL​L
misanthrope
Posté le 26-06-2009 à 10:23:05  profilanswer
 

Tu es obligé de conserver cet notation : foreach($line as $if-index) ?
Pour pas tt simplement foreach($line as $ifindex) :??:


---------------
We deserve everything that's coming...
n°1899356
Stuntman
Posté le 26-06-2009 à 10:37:06  profilanswer
 

Non, comme je l'ai dit, le fichier que je récupère de ma machine est forme comme ça! Et c'est justement ça mon problème :(, merci Alcatel...

n°1899357
pataluc
Posté le 26-06-2009 à 10:40:26  profilanswer
 

sauf erreur de ma part, la variable $if-index dans le foreach c'est la variable locale au foreach, peu importe son nom...

n°1899363
SICKofitAL​L
misanthrope
Posté le 26-06-2009 à 10:46:59  profilanswer
 

Ben oui [:spamafote]
Les variables portent le nom que tu veux, aucun rapport avec le contenu du XML


---------------
We deserve everything that's coming...
n°1899370
Stuntman
Posté le 26-06-2009 à 10:56:35  profilanswer
 

Ok autant pour moi, mais dans ce cas ce n'est pas vraiment ce que je recherche.
Je souhaite pouvoir mettre dans un array[] chacun des éléments :
adm-state, opr-state/tx-rate-ds , bref tout ce qui est dans "name" dans mon fichier xml. Ou dois je utiliser le mot clef "info", puis splitter son contenus pour ensuite le mettre dans mon array[] ?

n°1899391
Stuntman
Posté le 26-06-2009 à 11:28:31  profilanswer
 

Je ne trouve pas la méthode a appeler pour récupérer un par un  les items de mon fichier...  

n°1899403
SICKofitAL​L
misanthrope
Posté le 26-06-2009 à 11:41:00  profilanswer
 

Tu as regardé du coté de SimpleXML ? Ca te permet de faire tes "requetes" en XPath, et ca te renvoi des arrays


---------------
We deserve everything that's coming...
n°1899782
CyberDenix
Posté le 28-06-2009 à 02:34:36  profilanswer
 

Quand le nom de la balise pose probleme (style namespace ou nom de balise peu commun), tu fais simplement :  
 

Code :
  1. foreach ($toto->children() as $pouet) {
  2.  ...
  3. }


 
Tu peux si besoin passer en parametre un namespace à la fonction children.


---------------
Directeur Technique (CTO)
n°1900124
Stuntman
Posté le 29-06-2009 à 13:46:18  profilanswer
 

Je reviens a la charge avec ceci :  

Code :
  1. $fp = fsockopen($target, $port); // socket creation
  2.  if(!$fp) {
  3.   echo 'Telnet connexion to '.$target.'failed'; //if connexion fails
  4.  }
  5.  else {
  6.   // login
  7.   fputs($fp,$login."\r\n" );
  8.   sleep(1);
  9.   //password
  10.   fputs($fp,$mdp."\r\n" );
  11.   sleep(1);
  12.   //sending command
  13.   fputs($fp,$command4);   
  14.   sleep(5);
  15.   $output.=fread($fp,2000);
  16.   //echo $output;
  17.   //writting commands results into xml file
  18.   $myFile = "line.xml";
  19.   $fh = fopen($myFile, 'w') or die("can't open file" );
  20.   //cropping XML file from crap commands
  21.   //$data = removeEmptyLines($output);
  22.   $data = substr($output, 162);
  23.   $data2 = substr($data, 0, -15);
  24.   //writting into XML file
  25.   fwrite($fh, $data2);
  26.   fclose($fh);
  27.   //logout
  28.   fputs($fp,$close."\r" );
  29.   fclose($fp);
  30.   //Diplay XML file with parser
  31.   operationnal_data();
  32.   //atm_pvc();
  33.   //echo $output;
  34.   //open_file();
  35.   <hr>


 

Code :
  1. /* Parser of operationnal data command */
  2. function operationnal_data(){
  3. $dom = new DomDocument();
  4. $dom->load('line.xml');
  5. $line = $dom->getElementsByTagName('info');
  6. echo "<table border=1>
  7. <tr align=center><td>Admin state</td><td>Status</td><td>Upstream</td><td>Downstream</td><td>Mode</td></tr><tr align=center>
  8. ";
  9. foreach($line as $info)
  10. echo "<td>".$info->nodeValue . "</td>";
  11. echo "</table>---<br />";
  12. echo  " - " . $info->getAttribute("name" );
  13. }


et mon fichier xml

Code :
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <runtime-data  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:cli_show_output.xsd" display-level="normal">
  3.   <hierarchy name="show" type="static">
  4.     <hierarchy name="xdsl" type="static">
  5.       <hierarchy name="operational-data" type="static">
  6.         <hierarchy name="line" type="static">
  7.           <hier-id name="if-index" is-named="no" type="Itf::XdslLineAndChannelItf::XdslLineAndChannel">1/1/4/1</hier-id>
  8.           <instance>
  9.             <res-id name="if-index" short-name="if-index" type="Itf::XdslLineAndChannelItf::XdslLineAndChannel">1/1/4/1</res-id>
  10.             <info name="adm-state" short-name="adm-state" type="Itf::ifAdminStatus">up</info>
  11.             <info name="opr-state/tx-rate-ds" short-name="opr-state/tx-rate-ds" type="Xdsl::combinedCol">up :24543</info>
  12.             <info name="tx-rate/us" short-name="tx-rate/us" type="Xdsl::bitrate">1209</info>
  13.             <info name="tx-rate/ds" short-name="tx-rate/ds" type="Xdsl::bitrate">24543</info>
  14.             <info name="cur-op-mode" short-name="cur-op-mode" type="Xdsl::OneOpModeTypeLinePreDef">g992-5-a</info>
  15.           </instance>
  16.         </hierarchy>
  17.       </hierarchy>
  18.     </hierarchy>
  19.   </hierarchy>
  20. </runtime-data>


Le truc c'est que ma fonction de parse ne parvient pas a parser mon fichier xml car la première ligne est vide et du coup il y a une erreur... ma manipulation pour enlever toutes les commandes inutiles avant n'aident pas.  
Voila la situation actuelle...

mood
Publicité
Posté le 29-06-2009 à 13:46:18  profilanswer
 

n°1907355
the_bigboo
Posté le 21-07-2009 à 17:46:46  profilanswer
 

Je viens de faire un test avec ton bout de XML le rendu m'a l'air assez bien, j'avais eu un besoin similaire.
 
Aussi j'ai fait une class de lecture/écriture XML en PHP5
Et je te laisse une autre qui est un print_r assez amélioré :)
 
Je te laisse juger par toi même ;)
 
http://darknesss.free.fr/xml.zip


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  PHP

  Parser fichier XML

 

Sujets relatifs
[Python]Parser un fichier XML contenant des accentsParser un fichier XML
Parser un fichier XML en PHP[RSS] fichier XML ok mais faut-il obligatoirement le parser avec PHP?
Parser un fichier Atom XML ?[JavaScript/XML] Parser un fichier XML en variable arbre
[java] Parser un fichier XMLParser un fichier XML
[Java-XML] Arret de lecture d'un fichier XML avec un parser SAX[java] parser XML : fichier XML dans un jar ou non....
Plus de sujets relatifs à : Parser fichier XML


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR