Popular Select with XML - Logic

Asked

Viewed 59 times

0

Hello, I need to popular a select so that they are dynamic. The customer is a car site, IE, the user will be able to schedule the cars.

my job is to popular select, however I am having difficulties because XML seems to me poorly structured...

I’ll have 4 selects city, shop, brand and car.

That is, if the user selects São Paulo, it will show only the stores and cars available in sp...

But I think the XML is poorly organized, because it returns me the name of the city more than once...

Follow the code below...

<entry>
    <g:id>109851</g:id> 
    <title>FIAT UNO 1.0 FIREFLY FLEX ATTRACTIVE 4P MANUAL</title> 
    <g:description>Nenhuma descrição encontrada.</g:description> 
    <link></link> 
    <g:image_link></g:image_link> 
    <g:availability>in stock</g:availability> 
    <g:price>34290 BRL</g:price> 
    <g:google_product_category>916</g:google_product_category> 
    <g:brand>FIAT</g:brand> 
    <g:identifier_exists>no</g:identifier_exists>
    <g:condition>used</g:condition> 
    <g:adult>no</g:adult> 
    <g:is_bundle>no</g:is_bundle> 
    <g:color>PRATA</g:color> 
    <state>Minas Gerais</state> 
    <city>Belo Horizonte</city> 
    <store>Belo Horizonte - Estoril</store> 
  </entry> 

In each entry has a city, and if I search that city with the fetch of jquery, it returns me all the cities of all entrys(the same city about 100 times)...

Would it be right for me to ask them to reformulate this XML? or is it possible for me to manage with it?

//Edit

my code

    $.ajax({
            type: "GET",
            url: "googleshoppingcustom.xml",
            dataType: 'xml',
            success: function(xml){
              $(xml).find('entry').find('state').each(function(i){
                var state = $(this).text();
                $('#region').append(`<option value=${state}>${state}</option>`)

)}

Ele me retorna isto...

My real problem, it’s not even this repetition... but how could I get EVERY car brand for EVERY car... I’m finding the xml a bit messy...

  • The xml has about five thousand lines... It’s very confusing guys?

1 answer

0

Instead of using 2 .find, just do it like this: $(xml).find('entry state')... that already searches all tags <state> inside <entry>.

You can go making the append by checking if one state has already been entered or not, resulting in unique options with the names of States:

$.ajax({
   type: "GET",
   url: "googleshoppingcustom.xml",
   dataType: 'xml',
   success: function(xml){
      $(xml).find('entry state').each(function(i){

         var state = $(this).text();

         // verifica se já existe um option com o nome do Edtado
         if( !$('#region option[value="'+ state +'"]').length ){
            $('#region').append(`<option value='${state}'>${state}</option>`);
         }
      });
   }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.