简体   繁体   English

使用JavaScript显示特定的XML数据

[英]Display specific XML data using JavaScript

Hello friends of Stack Overflow 堆栈溢出的朋友你好

I can get an specific data from my XML file, but I have a question. 我可以从XML文件中获取特定数据,但是我有一个问题。 How can I get the "subchapter" data, only if the chapter id is 1 or 2? 仅当章节ID为1或2时,如何才能获得“子章节”数据?

This is my XML code: 这是我的XML代码:

<?xml version="1.0" encoding="utf-8" ?>
<chapters module="TALLER DE COMPROMISO ORGANIZACIONAL" img="fondoprincipal.jpg">
    <chapter id="1" title="Presentación Curso" img="img_chapter4.jpg" frase="">
        <subchapter id="1" title="Bienvenidos">
            <pag id="1" img="logoeconomia2.jpg" />
        </subchapter>
        <subchapter id="2" title="Instrucciones para ingresar al Taller">
            <pag id="1" img="instrucciones.jpg"/>
        </subchapter>
        <subchapter id="3" title="Instrucciones para estudiar este Taller">
            <pag id="1" img="instrucciones-3.jpg"/>
            <pag id="2" img="instrucciones-2.jpg"/>
            <pag id="3" img="foro.jpg"/>
        </subchapter>
        <subchapter id="4" title="Controles del curso">
            <pag id="1" img="controles.jpg" />
        </subchapter>
        <subchapter id="5" title="Tutorías del Taller">
            <pag id="1" img="e-learning.jpg"/>
        </subchapter>
        <subchapter id="6" title="Programación del Taller">
            <pag id="1" img="calendario.jpg"/>
        </subchapter>
        <subchapter id="7" title="Descripción del Taller">
            <pag id="1" img="descripcioncurso.jpg"/>
        </subchapter>
        <subchapter id="8" title="Objetivo General del Taller">
            <pag id="1" img="objetivo_general.jpg"/>
        </subchapter>
    </chapter>

    <chapter id="2" title="El Compromiso Organizacional" img="img_chapter3.jpg" frase="“Cuando alguien ama lo que hace se nota.
Cuando no amas lo que haces, se nota aún más”
Steve Jobs">
        <subchapter id="1" title="Objetivos de Aprendizaje"  >
            <pag id="1" img="objetivo_c1.jpg"/>
        </subchapter>
        <subchapter id="2" title="Aportes del Funcionario a la Misión Institucional"  >
            <pag id="1" img="generacion_valor_1.jpg"/>
        </subchapter>
        <subchapter id="3" title="Expresiones del Compromiso Organizacional">
            <pag id="1" img="responsabilidad_1.jpg"/>
        </subchapter>
        <subchapter id="4" title="Compromiso Organizacional y Servicio Público" >
            <pag id="1" img="comp_sp_1.jpg"/>
            <pag id="2" img="comp_sp_2.swf"/>
            <pag id="3" img="comp_sp_3.jpg"/>
        </subchapter>
        <subchapter id="5" title="Bases del Compromiso Organizacional" >
            <pag id="1" img="Base_Comp.swf"/>
        </subchapter>
    </chapter>
</chapters>

And this is my JavaScript code, contained in a PHP file. 这是我的JavaScript代码,包含在PHP文件中。 With this code I can parse an specific data, in this case I can get "img" and "title" attributes from a chapter, but I can't get the subchapter data if the id of chapter is "1" or "2" . 使用此代码,我可以解析特定的数据,在这种情况下,我可以从章节中获取“ img”和“ title”属性,但是如果章节的ID为“ 1”或“ 2”,则无法获得子章节数据。

<script>
    var ruta = '<?php echo "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]; ?>';
  camino = ruta.replace("chapter.php", "data/chapter.xml");
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
      if (xhttp.readyState == 4 && xhttp.status == 200) {
      myFunction(xhttp);
      }
  };
  xhttp.open("GET", camino, true);
  xhttp.send();

  function myFunction(xml) {
      var x, y, i, xmlDoc, txt, id_capitulo;

      xmlDoc = xml.responseXML;
      id_capitulo = <?php echo $id_capitulo; ?>;
      txt_img = "";
      txt_id = "";
      txt_2 = "";
      y = xmlDoc.getElementsByTagName('capitulo');
      for (i = 0; i < y.length; i++) {
        txt_id = y[i].getAttribute('id');
        if(txt_id == id_capitulo){

            txt_img = y[i].getAttribute('img');
            img = ruta.replace("chapter.php", "images/"+txt_img);
            document.getElementById("imagen_cap").innerHTML = '<img src="'+img+'" style="max-width:100%; width: 100%;" />';

            txt_2 += y[i].getAttribute('titulo');
            if(txt_id != 1){
                document.getElementById("titulo_barra").innerHTML = 'Capítulo '+(txt_id - 1)+' - '+txt_2;
                document.getElementById("presentacion_cap").innerHTML = txt_2+'<br><br><i class="fa fa-arrow-right boton_principio" id="boton_inicio_curso" style="font-size: 1em;" onclick="inicia_subcapitulo('+txt_id+'); return false;"></i>';
            }
            else{
                document.getElementById("titulo_barra").innerHTML = txt_2;
                document.getElementById("presentacion_cap").innerHTML = txt_2+'<br><br><i class="fa fa-arrow-right boton_principio" id="boton_inicio_curso" style="font-size: 1em;" onclick="inicia_subcapitulo('+txt_id+'); return false;"></i>';
            }
        }
      }
    }
</script>

Thanks guys and greetings. 谢谢大家的问候。

Friends, I have found the solution to my issue. 朋友,我找到了解决我问题的方法。

I just added this JS code to my original JavaScript and I can retrieve the subchapter attribute data if chapter has a determinated id. 我只是将此JS代码添加到我的原始JavaScript中,并且如果章节具有确定的ID,则可以检索子章节属性数据。

        titulo_sub="";
        z = xmlDoc.getElementsByTagName('chapter')[id_capitulo - 1].childNodes;
        cont = 1;
        for (j = 0; j < z.length; j++) {
            impar = j % 2;
            if(impar == 1){
                a = z[j].attributes;
                titulo_sub += cont +'.  '+ a.getNamedItem("title").nodeValue.toString() + '<br>';
                cont = cont + 1;
            }
        }
        document.getElementById("menu_cap").innerHTML = titulo_sub;

Kind regards and have a nice day. 亲切的问候,并度过愉快的一天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM