Hello:
I need help to parse the content of an XML file so that I can store all the data fields into a database.
The structure of the XML is something like this:
<Licitacion>
<Numero>000060040092005</Numero>
<Modificada>1</Modificada>
<PrecioBases>$881.00</PrecioBases>
<Estado>9</Estado>
<Dependencia>SECRETARÍA DE HACIENDA Y CRÉDITO PÚBLICO</Dependencia>
<AceptaElectronica>SI</AceptaElectronica>
<Caracter>NACIONAL</Caracter>
<Publicacion>06/09/2005</Publicacion>
<Tipo>SERVICIOS</Tipo>
<LimiteBases>21/09/2005</LimiteBases>
<Junta>20/09/2005, 10:00</Junta>
<AperturaTecnica>27/09/2005, 11:00</AperturaTecnica>
<AperturaEconomica>27/09/2005, 11:01</AperturaEconomica>
<Visita></Visita>
<AreaConsulta>DIRECCIÝN DE CONTRATACIÝN DE SERVICIOS EN INTERNET EN HTTP://COMPRANET.GOB.MX</AreaConsulta>
<Domicilio>CALZADA DE LA VIRGEN No. 2799, EDIFICIO "C", SEGUNDO PISO COLONIA UNIDAD CTM CULHUACÝN, C.P. 04480 , DELEGACION COYOACÁN , DISTRITO FEDERAL</Domicilio>
<DiasVenta>LUNES A VIERNES</DiasVenta>
<HorarioVenta>9:00 A 14:00</HorarioVenta>
<Telefono>91-58-45-89</Telefono>
<Fax>91-58-45-97</Fax>
<FormadePago></FormadePago>
<LugarEntrega>CENTRO DE CÝMPUTO INSTITUCIONAL TRIARA, SITA EN LIBRAMIENTO A SANTA ROSA NÝMERO 111, COL. FUTURO APODACA, CÝDIGO POSTAL 66600, APODACA, NUEVO LEÝN</LugarEntrega>
<DiasEntrega>DE LUNES A DOMINGO LOS 365 DÝAS DEL AÝO</DiasEntrega>
<HorarioEntrega>LAS 24 HORAS DEL DÝA</HorarioEntrega>
<PlazoEntrega>DEL 17 DE OCTUBRE DE 2005 AL 16 DE OCTUBRE DE 2008 (36 MESES)</PlazoEntrega>
<UnidadCompradora>
<Nombre>DIRECCIÓN DE CONTRATACIÓN DE SERVICIOS</Nombre>
<DomicilioUnidad>CALZADA DE LA VIRGEN No. 2799, EDIFICIO C, SEGUNDO PISO COLONIA UNIDAD CTM CULHUACÁN, C.P. 04480 , DELEGACION COYOACÁN , DISTRITO FEDERAL</DomicilioUnidad>
</UnidadCompradora>
<Partidas>
<DetallePartida>
<NumeroPartida>1</NumeroPartida>
<CABMS>C810800000</CABMS>
<Descripcion>SERVICIOS DE CÝMPUTO INSTITUCIONAL DE VALOR AGREGADO, CON ALTOS NIVELES DE DISPONIBILIDAD, PARA CENTRALIZAR LA PLATAFORMA DE COMPUTO HP </Descripcion>
<Cantidad>1</Cantidad>
<Unidad>SERVICIO</Unidad>
<EspecificacionTecnica>SERVICIOS DE CÝMPUTO INSTITUCIONAL DE VALOR AGREGADO, CON ALTOS NIVELES DE DISPONIBILIDAD, PARA CENTRALIZAR LA PLATAFORMA DE COMPUTO HP</EspecificacionTecnica>
</DetallePartida>
</Partidas>
<Documentos>
<Archivo>
<Liga>http://web.compranet.gob.mx:8002/HSM/UNICOM/00006/004/2005/009/217249.doc</Liga>
<Docto>Convocatoria</Docto>
</Archivo>
</Documentos>
<FormaPago>http://web.compranet.gob.mx:8001/pls/cnet2k/C2Pagos.reg_pago?NumeroLc=000060040092005&TipCon=1</FormaPago>
<Proveedores>
<Proveedor>
<NombreProveedor>HEWLETT-PACKARD MEXICO, S. DE R.L. DE C.V.</NombreProveedor>
<FechaCompra>06/09/2005</FechaCompra>
</Proveedor>
</Proveedores>
</Licitacion>[/quote]
And I need to have the output like:
000060040092005
1
$881.00
9
SECRETARÍA DE HACIENDA Y CRÉDITO PÚBLICO
SI
NACIONAL
06/09/2005
SERVICIOS
21/09/2005
20/09/2005, 10:00
27/09/2005, 11:00
27/09/2005, 11:01DIRECCIÝN DE CONTRATACIÝN DE SERVICIOS EN INTERNET EN HTTP://COMPRANET.GOB.MX
CALZADA DE LA VIRGEN No. 2799, EDIFICIO "C", SEGUNDO PISO COLONIA UNIDAD CTM CULHUACÝN, C.P. 04480 , DELEGACION COYOACÁN , DISTRITO FEDERAL
LUNES A VIERNES
9:00 A 14:00
91-58-45-89
91-58-45-97CENTRO DE CÝMPUTO INSTITUCIONAL TRIARA, SITA EN LIBRAMIENTO A SANTA ROSA NÝMERO 111, COL. FUTURO APODACA, CÝDIGO POSTAL 66600, APODACA, NUEVO LEÝN
DE LUNES A DOMINGO LOS 365 DÝAS DEL AÝO
LAS 24 HORAS DEL DÝA
DEL 17 DE OCTUBRE DE 2005 AL 16 DE OCTUBRE DE 2008 (36 MESES)
DIRECCIÓN DE CONTRATACIÓN DE SERVICIOS CALZADA DE LA VIRGEN No. 2799, EDIFICIO C, SEGUNDO PISO COLONIA UNIDAD CTM CULHUACÁN, C.P. 04480 , DELEGACION COYOACÁN , DISTRITO FEDERAL 1
C810800000
SERVICIOS DE CÝMPUTO INSTITUCIONAL DE VALOR AGREGADO, CON ALTOS NIVELES DE DISPONIBILIDAD, PARA CENTRALIZAR LA PLATAFORMA DE COMPUTO HP
1
SERVICIO
SERVICIOS DE CÝMPUTO INSTITUCIONAL DE VALOR AGREGADO, CON ALTOS NIVELES DE DISPONIBILIDAD, PARA CENTRALIZAR LA PLATAFORMA DE COMPUTO HPhttp://web.compranet.gob.mx:8002/HSM/UNICO.../009/217249.doc
Convocatoria
[url=http://web.compranet.gob.mx:8001/pls/cnet2k/C2Pagos.reg_pago?NumeroLc=000060040092005&TipCon=1]http://web.compranet.gob.mx:8001/pls/cnet2...05&TipCon=1[/url]
HEWLETT-PACKARD MEXICO, S. DE R.L. DE C.V.
06/09/2005Pretty much what I need is to strip all the XML tags and be able to add each line to the database matching field.
I have tried this regular expression: <(\w+)(\s(\w=".?")?)((/>)|((/?)>.?</\1>)) using the tester found here: http://www.regexlib.com/RETester.aspx?regexp_id=433 and it works* in the way that it does separate the XML fields, even though it leaves the opening and closing tag.
But when I tried the above regular expresion using preg_split I dont get any output, I am using this script for that:
<?php if( isset($HTTP_POST_VARS['first']) && strlen($HTTP_POST_VARS['first'])>1 ) { $handle = fopen($HTTP_POST_VARS['first'] , "r"); $contents = ''; if($handle) { while (!feof($handle)) { $contents .= fread($handle, 8192); } fclose($handle); } $tt=preg_split('<(\w+)(\s(\w*=".*?")?)*((/>)|((/*?)>.*?</\1>))',$contents); $start = 0; $end = count($tt); while($start <= $end) { echo $tt[$start + 1]; $start = $start + 1; } ask($HTTP_POST_VARS['first']); } else { if( isset($HTTP_POST_VARS['first']) && strlen($HTTP_POST_VARS['first'])<=1 ) { ask($HTTP_POST_VARS['first']); } else { ask("AdVig01JAN0131DEC05.xml"); } } function ask($init) { ?> <FORM ACTION="extract_dependencias.php" METHOD="POST"> Archivo: <INPUT TYPE="text" NAME="first" VALUE="<?php echo $init; ?>"> <BR> <INPUT TYPE="SUBMIT"> </FORM> <?php } ?>
So my two problems:
a) how can I use a regular expression with preg_split so that I can strip all the XML tags from my example in order to add the data into a mysql database.
b) which regular expression would be best for this issue in order to only get the text between the tags and not the whole line including the tags.
Sorry for the huge post, but I couldn´t think of any other way to explain everything without so much information.
Hope someone can help me.
Thanks.
pd. By the way, the information (as well as the tags) are in spanish, so I have to deal with characters other than a-z, like accents and such.