Problems with XML request with AJAX

0

I'm having a problem I made a Javascript code to display via AJAX an XML with a list: My XML looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<telas>
<item>
<title>Lorem ipsum</title>
<title>Dolor sit amet</title>
</item>

<item>
<title>Lorem ipsum dolor</title>
<title>Ipsum dolor sit amet</title>
<title>Ipsum dolor sit amet</title>
</item>
</telas>

I want to list the content of the <item> (of XML) tag and put the <li> (in my HTML) tag in the <title> (of XML), but only shows the contents of the first tag title , I need it to show all the <title> tags I have in XML.

I think the problem is this part of my code:

exibir+=tela[valor].getElementsByTagName('title')[0|1].firstChild.nodeValue;

How can I change this to work properly (the one I want)?

    
asked by anonymous 29.01.2014 / 19:43

3 answers

1

If I understand correctly, what do you want to do is create a list based on XML items? If so, you can do it as follows:

//levando em conta que o XML já esta "parseado" dentro da variável 'xml'
// e sua lista em uma chamada 'ul'

//pega todos os items...
var items = xml.getElementsByTagName("item");

for(var i = 0; i < items.length; i++){ //itera sobre os mesmos
   var item = items[i];
   var li = document.createElement("li"); //cria um LI para cada Item
   ul.appendChild(li); //insere na UL

   var titles = item.getElementsByTagName("title"); //recupera os titles

   for(var j = 0; j < titles.length; j++){
       var title = titles[j]; 
       var span = document.createElement("span"); //aqui criei um span para cada
       li.appendChild(span);  //inseri no LI 
       span.textContent = title.textContent; //e passei o valor do title para o span

   }
}

The result of this algorithm for your XML is

<ul>
   <li>
      <span>Lorem ipsum</span>
      <span>Dolor sit amet</span>
   </li>
   <li>
      <span>Lorem ipsum dolor</span>
      <span>Ipsum dolor sit amet</span>
      <span>Ipsum dolor sit amet</span>
   </li>
</ul>
    
03.02.2014 / 13:53
0
x = obj.getElementsByTagName('title');
y = document.querySelectorAll('li');
for(i=0;i<title.length){
    li[i].innerHTML = obj[i].firstChild.nodeValue;
}
    
29.01.2014 / 19:48
0

Here is a script I did for ZIP code capture:

$.getScript("http://www.buscarcep.com.br/?cep="+$("#cep").val()+"&formato=xml&chave=1iyeEHOY7.SVCKZSalTVl5SnTc34470", function(){
    if(resultadoCEP["resultado"] != 0){
        $('#loading').css({display:'none'});
        $('#selectEstado').fadeOut(400);
        $('#inputEstado').fadeIn(600);
        $("#rua").val(unescape(resultadoCEP["tipo_logradouro"])+' '+unescape(resultadoCEP["logradouro"])).prop("readonly", "readonly");
        $("#bairro").val(unescape(resultadoCEP["bairro"])).prop("readonly", "readonly");
        $("#cidade").val(unescape(resultadoCEP["cidade"])).prop("readonly", "readonly");
        $("#estado").val(unescape(resultadoCEP["uf"])).prop("readonly", "readonly");
        $('#numero').focus();
    }else{
        $('#inputEstado').fadeOut(400);
        $('#selectEstado').fadeIn(600);
        $("#rua").removeAttr('readonly').val('').focus();  
        $("#bairro").removeAttr('readonly').val('');  
        $("#cidade").removeAttr('readonly').val('');  
        $("#estado").removeAttr('readonly').val('');
        $('#loading').html('Endereço não encontrado.');
    }  
});

You need to have jquery included on the page. I think I already have some idea.

    
30.01.2014 / 13:48