Can I filter an XML file?

0

I would like to know if I can filter a file XML . The file is as follows:

<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.

The XML file traversed it in PHP in 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"];
    }
    }
    
asked by Jose Dario Correa 22.08.2018 в 16:01
source

1 answer

0

What you have to do is quite complicated, there are three options in principle.

  • Parse it with php and make an ajax every time you make a selection, you can use SimpleXML for this.
  • Parse it with Javascript as if it were DOM. For this, you would use DOMParser () , basic example here . This form can be good but you have to make queries with selectors, which can become quite complicated.
  • Transform the xml into JSON.

I will give you an example of this last form, 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 literal object of javascript, because doing this from scratch does not make much sense, and there are several libraries that do the same, and for more than a library stop working, another will do the same, because the result always has to be the same. I chose this one: link

With the parser what you have to do is go through the object that we have as a result and extract the information we want from it. Something like this:

    //vamos a la parte del objeto que nos interese
    var obj = x2js.xml_str2json(xml).Boletin_Consolidado.Boletin.Detalle_Circunscripcion.lin;

    var detalleOpcion = obj.Detalle_Opcion.lin;//<-- accedemos directamente a propiedades que no es necesario iterar.
    var detallePregunta = obj.Detalle_Pregunta.lin; 

    var info = document.getElementById("info");

    detalleOpcion.forEach(function(o,i){//<-- recorremos un array

      for(let p in o){//<-- recorremos un objeto literal
      info.innerHTML += p+": "+o[p]._V+"<br>";
      }

A functional example: link I do not know what specific information you need, but I think that it covers almost all the forms you can use to extract the information.

As for the select, the select will give you I suppose some data to search in this object, so that almost all the search, the forEach or fors are going to have to be in the callback of the event onchange of the select. I hope that this will serve you and others as a starting point to deal with this.

    
answered by 22.08.2018 в 17:36