使用JavaScript显示特定的XML数据
堆栈溢出的朋友们好 我可以从XML文件中获取特定数据,但我有一个问题。仅当章节id为1或2时,如何获取“子章节”数据 这是我的XML代码:使用JavaScript显示特定的XML数据,javascript,xml,Javascript,Xml,堆栈溢出的朋友们好 我可以从XML文件中获取特定数据,但我有一个问题。仅当章节id为1或2时,如何获取“子章节”数据 这是我的XML代码: <?xml version="1.0" encoding="utf-8" ?> <chapters module="TALLER DE COMPROMISO ORGANIZACIONAL" img="fondoprincipal.jpg"> <chapter id="1" title="Presentación Curs
<?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>
这是我的JavaScript代码,包含在一个PHP文件中。有了这段代码,我可以解析一个特定的数据,在这种情况下,我可以从章节中获取“img”和“title”属性,但是如果章节的id是“1”或“2”,我就无法获取子章节数据
var-ruta='';
camino=ruta.replace(“chapter.php”,“data/chapter.xml”);
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
myFunction(xhttp);
}
};
xhttp.open(“GET”,camino,true);
xhttp.send();
函数myFunction(xml){
变量x,y,i,xmlDoc,txt,id_capitalulo;
xmlDoc=xml.responseXML;
id_capitalulo=;
txt_img=“”;
txt_id=“”;
txt_2=“”;
y=xmlDoc.getElementsByTagName('capitalulo');
对于(i=0;i
';
}
否则{
document.getElementById(“titulo_barra”).innerHTML=txt_2;
document.getElementById(“presentacion_cap”).innerHTML=txt_2+'
';
}
}
}
}
谢谢大家,大家好。朋友们,我已经找到了解决问题的办法 我刚刚将这个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;
titulo_sub=”“;
z=xmlDoc.getElementsByTagName('chapter')[id_capitalulo-1].childNodes;
cont=1;
对于(j=0;j';
cont=cont+1;
}
}
document.getElementById(“menu_cap”).innerHTML=titulo_sub;
向您致以亲切的问候,祝您度过愉快的一天
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;