Foros del Web » Programando para Internet » PHP »

Contar tags XML PHP

Estas en el tema de Contar tags XML PHP en el foro de PHP en Foros del Web. Buenas tardes, Tengo un XML con tres valores de tags que son Critical, Major y Minor, necesito contar en php cuantras alarmas son Critical, Major ...
  #1 (permalink)  
Antiguo 10/05/2012, 13:09
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Contar tags XML PHP

Buenas tardes,

Tengo un XML con tres valores de tags que son Critical, Major y Minor, necesito contar en php cuantras alarmas son Critical, Major y Minor.

Quien me ayuda con esto:


El archivo XML es así:

<?xml version="1.0" encoding="UTF-8"?>
<Alarmas>
<Alarma Serial="439962" Fecha="2012-05-10 06:49:20-04:30">
<Nombre>MTP Link Send Overload</Nombre>
<Serial>439962</Serial>
<ID>1715</ID>
<Fecha>2012-05-10 06:49:20-04:30</Fecha>
<Info>Module No.=148, Link Name=ANSI-STPVAL-6(23), Sending load=40, Receiving load=26</Info>
<Severidad>Major</Severidad>
<Tipo_A>Fault</Tipo_A>
<Tipo_F>Exchange</Tipo_F>
<Tipo_E>Signaling</Tipo_E>
<Nodo_N>CSOFTX3000_ACA</Nodo_N>
<Nodo_T>CSOFTX3000</Nodo_T>


La llamada en php del XML es:

<?php

$url = "C:\TelnetWS3\AlarmasXML.xml";
$contenido_xml = "";
if($d = fopen($url, "r")){
while ($aux= fgets($d, 1024)){
$contenido_xml .= $aux;
}
fclose($d);
}else{
echo "No se pudo abrir el XML";
}

$xml =simplexml_load_string ($contenido_xml);


?>


La tabla donde muestra los datos del XML:


<table cellspacing="0" cellpadding="0" width="100%" class="tabla" action='' id="tab">
<tr>
<th width="2%"></th>
<th width="5%"></th>
<th width="15%"></th>
<th width="25%"></th>
<th width="8%"></th>
<th width="7%"></th>
<th width="5%"></th>
<th width="13%"></th>
<th width="8%"></th>
<th width="10%"></th>


</tr>

<?php

for($i=(count($xml->Alarma)-1); $i>=0; $i--){
$j=$xml->Alarma[$i]->Severidad;
if ($j==Critical)

echo '<tr><td width="2%" id="pru"> </td><td width="5%">'.$xml->Alarma[$i]->Severidad.'</td><td width="15%">'.$xml->Alarma[$i]->Nombre.'</td><td width="25%">'.$xml->Alarma[$i]->Info.'</td><td width="8%">'.$xml->Alarma[$i]->Serial.'</td><td width="7%">'.$xml->Alarma[$i]->ID.'</td><td width="5%">'.$xml->Alarma[$i]->Tipo_A.'</td><td width="13%">'.$xml->Alarma[$i]->Fecha.'</td><td width="8%">'.$xml->Alarma[$i]->Tipo_E.'</td><td width="10%">'.$xml->Alarma[$i]->Nodo_N.'</td></tr>';
else if ($j==Minor)
echo '<tr><td width="2%" id="pru2"> </td><td width="5%">'.$xml->Alarma[$i]->Severidad.'</td><td width="15%">'.$xml->Alarma[$i]->Nombre.'</td> <td width="25%">'.$xml->Alarma[$i]->Info.'</td><td width="8%">'.$xml->Alarma[$i]->Serial.'</td><td width="7%">'.$xml->Alarma[$i]->ID.'</td><td width="5%">'.$xml->Alarma[$i]->Tipo_A.'</td><td width="13%">'.$xml->Alarma[$i]->Fecha.'</td><td width="8%">'.$xml->Alarma[$i]->Tipo_E.'</td><td width="10%">'.$xml->Alarma[$i]->Nodo_N.'</td></tr>';
else if ($j==Major)
echo '<tr><td width="2%" id="pru3"> </td><td width="5%">'.$xml->Alarma[$i]->Severidad.'</td><td width="15%">'.$xml->Alarma[$i]->Nombre.'</td><td width="25%">'.$xml->Alarma[$i]->Info.'</td><td width="8%">'.$xml->Alarma[$i]->Serial.'</td><td width="7%">'.$xml->Alarma[$i]->ID.'</td><td width="5%">'.$xml->Alarma[$i]->Tipo_A.'</td><td width="13%">'.$xml->Alarma[$i]->Fecha.'</td><td width="8%">'.$xml->Alarma[$i]->Tipo_E.'</td><td width="10%">'.$xml->Alarma[$i]->Nodo_N.'</td></tr>';
else if ($j==Warning)
echo '<tr><td width="2%" id="pru1"> </td><td width="5%">'.$xml->Alarma[$i]->Severidad.'</td><td width="15%">'.$xml->Alarma[$i]->Nombre.'</td><td width="25%">'.$xml->Alarma[$i]->Info.'</td><td width="8%">'.$xml->Alarma[$i]->Serial.'</td><td width="7%">'.$xml->Alarma[$i]->ID.'</td><td width="5%">'.$xml->Alarma[$i]->Tipo_A.'</td><td width="13%">'.$xml->Alarma[$i]->Fecha.'</td><td width="8%">'.$xml->Alarma[$i]->Tipo_E.'</td><td width="10%">'.$xml->Alarma[$i]->Nodo_N.'</td></tr>';

}
?>


Quiero es que me diga: POR EJEMPLO
Critical: 5 Alarmas
Major: 2 Alarmas
Minor: 0 Alarmas
  #2 (permalink)  
Antiguo 10/05/2012, 13:52
Avatar de enlinea777  
Fecha de Ingreso: mayo-2008
Ubicación: frente al pc
Mensajes: 1.830
Antigüedad: 15 años, 11 meses
Puntos: 127
pregunta: Contar tags XML PHP

ese es el xml????????
no puedes ponder un ejemplo completo del archivo?
  #3 (permalink)  
Antiguo 10/05/2012, 14:04
 
Fecha de Ingreso: diciembre-2010
Mensajes: 788
Antigüedad: 13 años, 4 meses
Puntos: 51
Respuesta: Contar tags XML PHP

Podrias usar expresiones regulares, algo asi:

Código PHP:
Ver original
  1. preg_match_all('/<Severidad>(.*?)<\/Severidad>/si', $contenido_xml, $res);
  2. $repeticiones = array_count_values($res[1]);
  3. print_r($repeticiones);
  #4 (permalink)  
Antiguo 10/05/2012, 14:09
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Contar tags XML PHP

Amigo este es el ejemplo del XML y necesito contar cuantas Critical, Major y Minor hay. Espero su ayuda.


Código:
<?xml version="1.0" encoding="UTF-8"?>
<Alarmas>
  <Alarma Serial="1451329" Fecha="2012-02-01 11:45:52">
    <Nombre>E1/T1 Remote Alarm</Nombre>
    <Serial>1451329</Serial>
    <ID>1103</ID>
    <Fecha>2012-02-01 11:45:52</Fecha>
    <Info>Subrack No.=4, Slot No.=0, Subsystem No.=0, Link No.=12, BTS ID=39, BTS    Name=BTS39_Alto_Barinas</Info>
    <Severidad>Major</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Trunk</Tipo_E>
    <Nodo_N>BSC_ACA1</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
  <Alarma Serial="713394" Fecha="2011-02-03 21:12:15">
    <Nombre>Instant E1/T1 Link BER Threshold Crossed Alarm</Nombre>
    <Serial>713394</Serial>
    <ID>1123</ID>
    <Fecha>2011-02-03 21:12:15</Fecha>
    <Info>Subrack No.=4, Slot No.=0, Subsystem No.=0, Link No.=4, BTS ID=41, BTS    Name=BTS41_Barinitas_MOVISTAR</Info>
    <Severidad>Minor</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Trunk</Tipo_E>
    <Nodo_N>BSC_ACA1</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
  <Alarma Serial="713387" Fecha="2011-02-03 21:11:15">
    <Nombre>E1/T1 Link BER Threshold Crossed Alarm</Nombre>
    <Serial>713387</Serial>
    <ID>1122</ID>
    <Fecha>2011-02-03 21:11:15</Fecha>
    <Info>Subrack No.=4, Slot No.=0, Subsystem No.=0, Link No.=7, BTS ID=55, BTS    Name=BTS55_Hato_Palma_Sola_BRN</Info>
    <Severidad>Minor</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Trunk</Tipo_E>
    <Nodo_N>BSC_ACA1</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
  <Alarma Serial="713386" Fecha="2011-02-03 21:11:15">
    <Nombre>E1/T1 Link BER Threshold Crossed Alarm</Nombre>
    <Serial>713386</Serial>
    <ID>1122</ID>
    <Fecha>2011-02-03 21:11:15</Fecha>
    <Info>Subrack No.=4, Slot No.=0, Subsystem No.=0, Link No.=4, BTS ID=41, BTS    Name=BTS41_Barinitas_MOVISTAR</Info>
    <Severidad>Minor</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Trunk</Tipo_E>
    <Nodo_N>BSC_ACA1</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
  <Alarma Serial="713239" Fecha="2011-02-03 20:47:14">
    <Nombre>Instant E1/T1 Link BER Threshold Crossed Alarm</Nombre>
    <Serial>713239</Serial>
    <ID>1123</ID>
    <Fecha>2011-02-03 20:47:14</Fecha>
    <Info>Subrack No.=4, Slot No.=0, Subsystem No.=0, Link No.=7, BTS ID=55, BTS    Name=BTS55_Hato_Palma_Sola_BRN</Info>
    <Severidad>Minor</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Trunk</Tipo_E>
    <Nodo_N>BSC_ACA1</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
  <Alarma Serial="174618" Fecha="2011-12-09 11:29:07">
    <Nombre>IMA/UNI Link LOCD</Nombre>
    <Serial>174618</Serial>
    <ID>1007</ID>
    <Fecha>2011-12-09 11:29:07</Fecha>
    <Info>Subrack No.=4, Slot No.=0, Subsystem No.=0, Link No.=3</Info>
    <Severidad>Major</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Trunk</Tipo_E>
    <Nodo_N>BSC_ACA2</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
  <Alarma Serial="201396" Fecha="2012-03-01 00:04:05">
    <Nombre>Emergency workstation disconnected</Nombre>
    <Serial>201396</Serial>
    <ID>27013</ID>
    <Fecha>2012-03-01 00:04:05</Fecha>
    <Info>BAM Process No=0, Process Name=SMirror</Info>
    <Severidad>Minor</Severidad>
    <Tipo_A>Fault</Tipo_A>
    <Tipo_F>BSC</Tipo_F>
    <Tipo_E>Communication</Tipo_E>
    <Nodo_N>BSC_ACA2</Nodo_N>
    <Nodo_T>BSC6600</Nodo_T>
  </Alarma>
</Alarmas>
  #5 (permalink)  
Antiguo 10/05/2012, 14:14
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Contar tags XML PHP

Cita:
Iniciado por walterdevel Ver Mensaje
Podrias usar expresiones regulares, algo asi:

Código PHP:
Ver original
  1. preg_match_all('/<Severidad>(.*?)<\/Severidad>/si', $contenido_xml, $res);
  2. $repeticiones = array_count_values($res[1]);
  3. print_r($repeticiones);
Amigo, si uso su codigo me muestra esto:

Array ( [Major] => 88 [Critical] => 32 [Minor] => 5 [MAJOR] => 2 )

Es algo así, pero cada tiene que ser una variable diferente:

$Critical = tantas alarmas
$Major = tantas alarmas
$Minor = tantas alarmas
  #6 (permalink)  
Antiguo 10/05/2012, 14:22
 
Fecha de Ingreso: diciembre-2010
Mensajes: 788
Antigüedad: 13 años, 4 meses
Puntos: 51
Respuesta: Contar tags XML PHP

Podes modificarlo, no muerde...

Código PHP:
Ver original
  1. $Critical =$repeticiones['Critical'];
.....
.....
  #7 (permalink)  
Antiguo 10/05/2012, 14:27
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Contar tags XML PHP

Cita:
Iniciado por walterdevel Ver Mensaje
Podes modificarlo, no muerde...

Código PHP:
Ver original
  1. $Critical =$repeticiones['Critical'];
.....
.....
En donde agrego ese codigo. Disculpa. Gracias
  #8 (permalink)  
Antiguo 10/05/2012, 14:29
 
Fecha de Ingreso: diciembre-2010
Mensajes: 788
Antigüedad: 13 años, 4 meses
Puntos: 51
Respuesta: Contar tags XML PHP

Debajo de esto:

Código PHP:
Ver original
  1. preg_match_all('/<Severidad>(.*?)<\/Severidad>/si', $contenido_xml, $res);
  2. $repeticiones = array_count_values($res[1]);

Veo que no tienes mucha idea de php...
  #9 (permalink)  
Antiguo 10/05/2012, 14:38
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Contar tags XML PHP

Cita:
Iniciado por walterdevel Ver Mensaje
Debajo de esto:

Código PHP:
Ver original
  1. preg_match_all('/<Severidad>(.*?)<\/Severidad>/si', $contenido_xml, $res);
  2. $repeticiones = array_count_values($res[1]);

Veo que no tienes mucha idea de php...
Gracias me has ayudado mucho con esto. Te lo agradezco.
  #10 (permalink)  
Antiguo 10/05/2012, 14:45
Avatar de linuxzero  
Fecha de Ingreso: noviembre-2011
Ubicación: Argentina
Mensajes: 778
Antigüedad: 12 años, 5 meses
Puntos: 160
Respuesta: Contar tags XML PHP

Tambien podes hacer esto, es menos performante, pero capaz lo ves mejor:

Código PHP:
Ver original
  1. $critical = 0;
  2. $major = 0;
  3. $minor = 0;
  4. foreach ($xml as $alarma) {
  5.     switch ($alarma->Severidad) {
  6.         case "Critical": $critical++;
  7.         case "Major": $major++;
  8.         case "Minor": $minor++;
  9.     }
  10. }

Y de paso te aviso, cuando ejecutaste el codigo de walterdevel te devolvio 2 posiciones del mismo tipo, una major y otra MAJOR, fijate de que esten escritas iguales las cosas que estan dentro del tag <Severidad></Severidad> porque te las esta separando en 2 algo que deberian estar juntas, major y MAJOR supongo que son iguales, ojo con eso.
__________________
Si todo fuera tan sencillo como un symfony cc la vida seria más fácil.
http://phpnico.wordpress.com
  #11 (permalink)  
Antiguo 10/05/2012, 14:54
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Contar tags XML PHP

Cita:
Iniciado por linuxzero Ver Mensaje
Tambien podes hacer esto, es menos performante, pero capaz lo ves mejor:

Código PHP:
Ver original
  1. $critical = 0;
  2. $major = 0;
  3. $minor = 0;
  4. foreach ($xml as $alarma) {
  5.     switch ($alarma->Severidad) {
  6.         case "Critical": $critical++;
  7.         case "Major": $major++;
  8.         case "Minor": $minor++;
  9.     }
  10. }

Y de paso te aviso, cuando ejecutaste el codigo de walterdevel te devolvio 2 posiciones del mismo tipo, una major y otra MAJOR, fijate de que esten escritas iguales las cosas que estan dentro del tag <Severidad></Severidad> porque te las esta separando en 2 algo que deberian estar juntas, major y MAJOR supongo que son iguales, ojo con eso.
Si son dos variables distintas, pero quiero sumarlas. Tienes idea como hacerlo?

Saludos y gracias
  #12 (permalink)  
Antiguo 10/05/2012, 14:56
 
Fecha de Ingreso: diciembre-2010
Mensajes: 788
Antigüedad: 13 años, 4 meses
Puntos: 51
Respuesta: Contar tags XML PHP

Código PHP:
Ver original
  1. $contenido_xml = str_replace('MAJOR', 'Major', $contenido_xml);

Antes del preg_match...
  #13 (permalink)  
Antiguo 10/05/2012, 15:02
 
Fecha de Ingreso: enero-2012
Mensajes: 109
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Contar tags XML PHP

Listo, gracias a todos por la ayuda. Me sirvió todo. Saludos
  #14 (permalink)  
Antiguo 10/05/2012, 15:03
Avatar de linuxzero  
Fecha de Ingreso: noviembre-2011
Ubicación: Argentina
Mensajes: 778
Antigüedad: 12 años, 5 meses
Puntos: 160
Respuesta: Contar tags XML PHP

Cita:
Iniciado por walterdevel Ver Mensaje
Código PHP:
Ver original
  1. $contenido_xml = str_replace('MAJOR', 'Major', $contenido_xml);

Antes del preg_match...
100% de acuerdo
__________________
Si todo fuera tan sencillo como un symfony cc la vida seria más fácil.
http://phpnico.wordpress.com

Etiquetas: tabla, tags, xml
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:34.