ya logre la mayoria del trabajo..
el problema es que al momento de leer las lineas que tengo separadas por comas ','.
Si son varios 'rows' eso es la parte que quiero que se vayan agregando al xml automaticamente.
ya que esos datos se sacaran del csv y se anexaran dentro del xml
plantilla.xml
Código HTML:
<?xml version="1.0" encoding="UTF-8"?> <ROOT> <HEADER REFNR="0000000003060697" CID="414D51204341535F46525F544553542E3F40FADD206D8D3B" PLANT="3011" KEY="[ORDER]" DIRECT="1" OBJECTTYPE="LOIPRO" DATASTEP="PROMES001 Version001" LASTSTEP="B03" CREDATE="2011-06-21" CRETIME="16:18:33" ACTDATE="2011-06-21" ACTTIME="16:18:44" STATUS="22" TEST="" SYSTAT="1"> <SOURCE NAME="MANUAL" VERSION="001"/> <DESTINATION NAME="MES" VERSION="001"/> </HEADER> <DATA> <SAPMES PLANT="3011"> <LOIPRO> <E1FKOL AUFNR="[ORDER]" MATNR="A2C89902300" GAMNG="[QUANTITY]" GASMG="0.000" GSTRS="[TODAY]" GLTRS="[TODAYPLUS30]" OBJNR="MANUAL_LOIPRO"/> <E1AFPOL POSNR="1" VERID="0001"/> <E1AFFLL PLNFL="0" FLGAT="0"> <E1FVOL VORNR="20" LTXA1="ICT" ARBPL="7280818" ARBID="00000000" STEUS="MMN" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="25" LTXA1="REPARAR" ARBPL="1040091" ARBID="00000000" STEUS="MRNB" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="30" LTXA1="IMPRIME" ARBPL="7280203" ARBID="00000000" STEUS="MON" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="40" LTXA1="SOLMINI" ARBPL="0082002" ARBID="00000000" STEUS="MON" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="50" LTXA1="DESPANE" ARBPL="7281501" ARBID="00000000" STEUS="MON" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="60" LTXA1="PEGAR" ARBPL="7280502" ARBID="00000000" STEUS="MON" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="65" LTXA1="REPARAR" ARBPL="1040091" ARBID="00000000" STEUS="MRNB" MGVRG="[QUANTITY]" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> </E1AFFLL> </LOIPRO> </SAPMES> </DATA> </ROOT>
Código:
pagina.php001,A2CBOT,A2CTOP,GLTRS,GSTRS,MGVRG <<-- Header AC2M93,AC2M94,21042603,21042603,GHJT01 AC2M94,AC2M95,21042604,21042603,GHJT02 AC2M95,AC2M96,21042605,21042603,GHJT03 AC2M96,AC2M97,21042606,21042603,GHJT04 AC2M97,AC2M98,21042607,21042603,GHJT05 AC2M98,AC2M99,21042608,21042603,GHJT06 AC2M99,AC2M100,21042609,21042603,GHJT07
Código PHP:
<?php
function csv_in_array($url,$delm=",",$encl="\"") {
$doc = new DOMDocument();
$xml2 = "A2CBOT.xml";
$doc->load($xml2);
$xp = new DOMXPath($doc);
$csvxrow = file($url);
$csvxrow[0] = chop($csvxrow[0]);
$csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
$keydata = explode($delm,$csvxrow[0]);
$keydatos = explode($delm,$csvxrow[0]);
$keynumb = count($keydata);
$anzdata = count($csvxrow);
$z=0;
$date = date('Y-m-d');
$datesum = date('Y-m-d', strtotime('+1 month'));
for($x=1; $x<$anzdata; $x++) {
$csvxrow[$x] = chop($csvxrow[$x]);
$csvxrowo[$x] = chop($csvxrow[$x]);
$csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
$csv_data[$x] = explode($delm,$csvxrow[$x]);
$csv_datos[$x] = explode($delm,$csvxrow[$x]);
$i=0;
foreach($keydata as $key) {
$out[$z][$key] = $csv_data[$x][$i];
$i++;
//////
# Busco en el XML para modificar el attributo, y lo dejo fijo desde el csv que son los Header.
#
foreach($xp->query('/ROOT/HEADER/@KEY[. != ""]') as $attrib)
{
$attrib->nodeValue = $keydata[0];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@AUFNR[. != ""]') as $attrib)
{
$attrib->nodeValue = $keydata[1];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GAMNG[. != ""]') as $attrib)
{
$attrib->nodeValue = $keydata[2];
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GSTRS[. != ""]') as $attrib)
{
$attrib->nodeValue = $date;
}
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GLTRS[. != ""]') as $attrib)
{
$attrib->nodeValue = $datesum;
}
# Aqui es donde empieza a leer cada Row y se deberia ir agregando nuevos child con attributos
# al xml. Donde MGVRG="#" sera el valor obtenido del csv. y tendra mas attributos fijos.
foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1AFFLL/E1FVOL/@MGVRG[. != ""]') as $attrib)
{
$j=0;
$h=0;
foreach($keydatos as $keydato) {
$datos[$h][$keydato] = $csv_datos[$x][$j];
$j++;
$attrib->nodeValue = $datos[$h][$keydato];
}
$h++;
}
}
$z++;
}
echo $doc->saveXML();
return $out;
}
/////////////////////////////////////////
$csvdata = csv_in_array("LISTOTA.csv", ",", "\"");
echo "<pre>\r\n";
print_r($csvdata);
echo "</pre>\r\n";
?>
Código HTML:
<ROOT> <HEADER REFNR="0000000003060697" CID="414D51204341535F46525F544553542E3F40FADD206D8D3B" PLANT="3011" KEY="001" DIRECT="1" OBJECTTYPE="LOIPRO" DATASTEP="PROMES001 Version001" LASTSTEP="B03" CREDATE="2011-06-21" CRETIME="16:18:33" ACTDATE="2011-06-21" ACTTIME="16:18:44" STATUS="22" TEST="" SYSTAT="1"> <SOURCE NAME="MANUAL" VERSION="001"/> <DESTINATION NAME="MES" VERSION="001"/> </HEADER> <DATA> <SAPMES PLANT="3011"> <LOIPRO> <E1FKOL AUFNR="A2CBOT" MATNR="A2C89902300" GAMNG="A2CTOP" GASMG="0.000" GSTRS="2014-03-05" GLTRS="2014-04-05" OBJNR="MANUAL_LOIPRO"/> <E1AFPOL POSNR="1" VERID="0001"/> <E1AFFLL PLNFL="0" FLGAT="0"> <E1FVOL VORNR="20" LTXA1="ICT" ARBPL="7280818" ARBID="00000000" STEUS="MMN" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="25" LTXA1="REPARAR" ARBPL="1040091" ARBID="00000000" STEUS="MRNB" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="30" LTXA1="IMPRIME" ARBPL="7280203" ARBID="00000000" STEUS="MON" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="40" LTXA1="SOLMINI" ARBPL="0082002" ARBID="00000000" STEUS="MON" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="50" LTXA1="DESPANE" ARBPL="7281501" ARBID="00000000" STEUS="MON" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="60" LTXA1="PEGAR" ARBPL="7280502" ARBID="00000000" STEUS="MON" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> <E1FVOL VORNR="65" LTXA1="REPARAR" ARBPL="1040091" ARBID="00000000" STEUS="MRNB" MGVRG="" MEINH="PCE" BMSCH="100.000" VGE01="MIN" VGE02="MIN" VGE03="MIN" VGW01="0.000" VGW02="0" VGW03="0"/> </E1AFFLL> </LOIPRO> </SAPMES> </DATA> </ROOT>
Saludos.