I would like to know if I can filter a file XML
. The file is the following:
<Boletin_Consolidado>
<Boletin>
<Numero V="0122"/>
<Tipo_Boletin V="DEPARTAMENTAL"/>
<Departamento V="31"/>
<Desc_Departamento V="VALLE"/>
<Detalle_Circunscripcion>
<lin>
<Detalle_Pregunta>
<lin>
<Pregunta V="001"/>
<Total_Sufragantes V="0"/>
<Porc_Sufragantes V="000,00"/>
<Abstencion V="3488628"/>
<Porc_Abstencion V="100,00"/>
<Votos_Validos V="0"/>
<Porc_Votos_Validos V="000,00"/>
<Votos_No_Marcados V="0"/>
<Porc_Votos_No_Marcados V="000,00"/>
<Votos_Nulos V="0"/>
<Porc_Votos_Nulos V="000,00"/>
</lin>
</Detalle_Pregunta>
<Detalle_Opcion>
<lin>
<Opcion V="001"/>
<Votos V="0"/>
<Porc V="000,00"/>
<Pregunta V="001"/>
</lin>
<lin>
<Opcion V="002"/>
<Votos V="0"/>
<Porc V="000,00"/>
<Pregunta V="001"/>
</lin>
</Detalle_Opcion>
</lin>
</Detalle_Circunscripcion>
</Boletin>
</Boletin_Consolidado>
The list of Departments continues under other labels called Boletin
. What I want to do is filter by the Department code Departamento
using a select
and show me that data.
I run the file XML
in PHP
the following way:
$NacionalXML = simplexml_load_file("./ConsultaAnticorrupcion/Archivos/XML/$Nac") or die("Error: Cannot create object");
foreach ($NacionalXML as $key => $Boletin) {
// ========== CABECERA BOLETIN ==========
$Numero = $Boletin->Numero["V"];
$Tipo_Boletin = $Boletin->Tipo_Boletin["V"];
$Fecha = $Boletin->Dia["V"] . "/" . $Boletin->Mes["V"] . "/" . $Boletin->Anio["V"];
$Hora = $Boletin->Hora["V"] . ":" . $Boletin->Minuto["V"];
$Mesas_Instaladas = $Boletin->Mesas_Instaladas["V"];
$Mesas_Informadas = $Boletin->Mesas_Informadas["V"];
$Potencial_Sufragantes = $Boletin->Potencial_Sufragantes["V"];
$Boletin_Departamental = $Boletin->Boletin_Departamental["V"];
$Desc_Departamento = $Boletin->Desc_Departamento["V"];
$Porc_Mesas_Informadas = $Boletin->Porc_Mesas_Informadas["V"];
$Departamento = $Boletin->Departamento["V"];
$Municipio = $Boletin->Municipio["V"];
// ========== PREGUNTAS ==========
foreach ($Boletin->Detalle_Circunscripcion->lin->Detalle_Pregunta->lin as $Pregunta) {
$Total_Sufragantes = $Pregunta->Total_Sufragantes["V"]++;
$Votos_Nulos = $Pregunta->Votos_Nulos["V"]++;
$Votos_No_Marcados = $Pregunta->Votos_No_Marcados["V"]++;
$Votos_Validos = $Pregunta->Votos_Validos["V"]++;
$Total_Votos = $Votos_Nulos + $Votos_No_Marcados + $Votos_Validos;
}
// ========== RESPUESTAS ==========
foreach ($Boletin->Detalle_Circunscripcion->lin->Detalle_Opcion->lin as $Respuesta) {
$Opcion = $Respuesta->Opcion["V"];
$Votos = $Respuesta->Votos["V"];
$Porc = $Respuesta->Porc["V"];
$Pregunta = $Respuesta->Pregunta["V"];
}
}
What you have to do is quite complicated, there are three options in principle.
I am going to give you an example of this last way, it is quite complicated if you are not used to handling objects and arrays, but it is the one that in the long run, in my opinion, allows more flexibility. In principle, you have to download a library that does the job of transforming the xml to JSON and then to a literal javascript object, because doing this from scratch does not make much sense, and there are several libraries that do the same, and for more than one library stop working, another will do the same, because the result always has to be the same. I chose this one: https://cdn.rawgit.com/abdmob/x2js/master/xml2json.js
With the parser, what we have to do is go through the object that we have as a result and gradually extract the information that we want from it. Something like this:
A working example: https://jsfiddle.net/x1hwfvan/ I don't know what specific information you need, but I think that covers almost all the ways you can use to extract the information.
As for the select, I suppose the select will give you some data to search for in this object, that is, almost all the search, the forEach or fors will have to be in the callback of
onchange
the select event. I hope this will serve you and others as a starting point to deal with this.