0
    <?xml version="1.0" encoding="UTF-8"?>
<testSuiteResults>
  <testSuite>
    <startTime>13:46:06</startTime>
    <status>FAILED</status>
    <testSuiteName>Web TestSuite</testSuiteName>
    <timeTaken>13907</timeTaken>
    <testRunnerResults>
      <testCase>
        <reason>Failing due to failed test step</reason>
        <startTime>13:46:06</startTime>
        <status>FAILED</status>
        <testCaseId>2c0c10d3-f76f-43ae-900d-6e64bb96203f</testCaseId>
        <testCaseName>CorTestCase</testCaseName>
        <timeTaken>7021</timeTaken>
        <testStepResults>
          <result>
            <message>Step 1 [www.coriolis.eu.org] OK: took 2309 ms</message>
            <name>www.coriolis.eu.org</name>
            <order>2</order>
            <started>13:46:06.266</started>
            <status>OK</status>
            <timeTaken>2309</timeTaken>
          </result>
</testSuite>
</testCase>
</testSuiteResults>

hi, i have this XML file i'm trying to get the "status, timeTaken" tags in an array. I use this function :

function XMLtoARRAY($fichier,$item,$champs) {
  // on lit le fichier
  if(file_exists($fichier)){
        if($chaine = @implode("",@file($fichier))) {

                // on éclate les objets <item>
                $tmp = preg_split("/<\/?"."\/".$item.">/",$chaine);
               // $tmp4 = preg_split("/<\\/?".$item.">/", $chaine);

                //for($j=0;$j<sizeof($tmp4)-1;$i+=1){
                // on parcours les <item>
                for($i=0;$i<sizeof($tmp)-1;$i+=1){
                        // on recherche les champs demandés
                        foreach($champs as $champ) {
                                $tmp2 = preg_split("/<\/?".$champ.">/",$tmp[$i]); 
                                // on ajoute l'élément au tableau
                                $tmp3[$i][$champ] = @$tmp2[1];
                        }
                }
                //}
                // retourne le tableau associatif
                return $tmp3;


        }
  }else{
          return "Le fichier n'existe pas";
  }
}

the probleme is when i want to put it in my data base empty lines. here is my function for the data base :

function XMLtoSQL($fichier,$item,$champs,$test=0){

        //On recupère le tableau PHP correspondant au fichier XML
        $xml = XMLtoARRAY($fichier,$item,$champs);
        if(is_array($xml)){
                getconnection();
                $nom_table="result";
                $requetes_insert=array();
                $requete="";
                foreach($xml as $un_enregistrement){
                        $requete="INSERT INTO ".$nom_table;
                        $col_name="(";
                        $value="(";
                        foreach($un_enregistrement as $champs=>$valeur){
                                $col_name.=$champs.",";

                                $value.="\"".$valeur."\",";

                        }
                        //$col_name=substr($col_name,0,-1);
                        $value=substr($value,0,-1);
                        $requete.=" VALUES ".$value.")";
                        $requetes_insert[]=$requete;

                }

                //Si tout est ok on vide la table
               /* $vidange_table="TRUNCATE TABLE ".$nom_table;

                if($test==0){

                        $res_vidange=mysql_query($vidange_table);
                        if(!$res_vidange){

                                return "Erreur lors de la vidange de la table : ".$nom_table;
                        }
                }
                */
                //Puis on execute les requêtes une par une
                foreach($requetes_insert as $key=>$une_requete){
                        if($test==0){
                                $res_requete=mysql_query($une_requete);
                                if(!$res_requete){
                                        return "Erreur lors de l'execution de la requete num ".$key." : ".$une_requete;

                                }
                        }

                }
                return "Importation des ".sizeof($requetes_insert)." requetes reussie";
        }else{
                return "L'erreur suivante a ete detectee : ".$xml;
        }
}

i want to remove this empty lines. Thank you

cooper
  • 1

1 Answers1

0

PHP has a built in XML parser you should use rather than trying to write your own parser (which in this case might be rather error prone when encountering a malformed file structure).

There's a whole "book" about this, which can be found in the PHP manual (linked the French version).

As for the empty lines: Where exactly do you get empty lines? Can you pinpoint the line where those empty strings "appear"?

PS: If possible, use English in comments. Makes people here understand them more easily. :)

Mario
  • 35,726
  • 5
  • 62
  • 78
  • it makes empty lines because the parser read item : like if he found , I want to get juste the tags between and – cooper Jun 06 '13 at 12:00
  • Don't make the `/` optional, instead expand the expression to only grab complete tags, e.g. something like `<$item>(.*?)$item>`. This will be a non-greedy match, meaning it will take as few characters as possible to match the expression. You can then use this result to do further parsing (or expand the expression with sub groups). – Mario Jun 06 '13 at 13:30