Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   чтение xml без жкуиерити (https://javascript.ru/forum/misc/61523-chtenie-xml-bez-zhkuieriti.html)

leon2009sp 13.03.2016 21:02

<script>
            function loadXMLDoc(filename) {
                if (window.ActiveXObject) {
                    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                else {
                    xhttp = new XMLHttpRequest();
                }
                xhttp.open("GET", filename, false);
                try { xhttp.responseType = "msxml-document" } catch (err) { } // Helping IE11
                xhttp.send(null);
                return xhttp.responseXML;
            }
            document.addEventListener('DOMContentLoaded', function () {
                var xml = loadXMLDoc("xml/city.xml"),
                    selCity = document.querySelector('#city'),
                    selStation = document.querySelector('#station2'),i;
                //    selStation = document.getElementById('#station2'),i;
                [].forEach.call(xml.querySelectorAll('city'), function (el) {
                  // selCity.add(new Option(el.getAttribute('name'), el.getAttribute('name')))
                  
                    document.getElementById('sec1').innerHTML = el.getAttribute('name'),el.getAttribute('name');
                })
                selCity.addEventListener('change', function () {
                    selStation.options.length = 0 , i=0;
                    [].forEach.call(xml.querySelectorAll('city[name=' + this.value + '] > station2'), function (el) {
                     //   selStation.add(new Option(el.textContent, ++i));
                        document.getElementById('sec1').innerHTML = el.textContent;
                    })
                })
            })
        </script>
        <select id=city><option selected>33333333</option></select>
       <div id="sec1">123</div>

ну где же косяк то

destus 14.03.2016 05:53

в строке 22. , зачем?

leon2009sp 14.03.2016 07:24

это я пробовал по разному :blink: если строку 22 оставить то будет загружаться в див последние значение.
а если убрать то нифига не работает. на самом деле как тока не издевался:
selStation.add('sec1');
:blink:
или вообще весь список файла вылезти в див.
а ее строка 22 отвечает за чтение из файла сити, помещает от туда name в селект (в первый).

leon2009sp 14.03.2016 09:50

это оставляю как есть!
selCity.add(new Option(el.getAttribute('name'), el.getAttribute('name')))

а строчку вывода где
selStation.add(new Option(el.textContent, ++i));
заменяю на
document.getElementById('sec1').innerHTML = el.textContent;

и по идеи должно работать:
<select id=city><option selected>11111111</option></select>
       <div id="sec1">123</div>

leon2009sp 14.03.2016 09:58

по шагам:
<script>
            function loadXMLDoc(filename) {
                if (window.ActiveXObject) {
                    xhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                else {
                    xhttp = new XMLHttpRequest();
                }
                xhttp.open("GET", filename, false);
                try { xhttp.responseType = "msxml-document" } catch (err) { } // Helping IE11
                xhttp.send(null);
                return xhttp.responseXML;
            }

загрузили хмль
document.addEventListener('DOMContentLoaded', function () {
                var xml = loadXMLDoc("xml/city.xml"),
                    selCity = document.querySelector('#city'),
                   // selStation = document.querySelector('#station2'),i;
                    selStation = document.getElementById('#station2'),i;
                [].forEach.call(xml.querySelectorAll('city'), function (el) {
                  selCity.add(new Option(el.getAttribute('name'), el.getAttribute('name')))
                 
                  //  document.getElementById('sec1').innerHTML = el.getAttribute('name'),el.getAttribute('name');
                })

прошлись по сити и именам его а т.е. (name="Новосибирск") или (name="Москва") или name="ленинград".
selCity.addEventListener('change', function () {
                     selStation.options.length = 0 , i=0;
					//selStation.id.length = 0 , i=0;
                    [].forEach.call(xml.querySelectorAll('city[name=' + this.value + '] > station2'), function (el) {
                     //   selStation.add(new Option(el.textContent, ++i));
					 
                     //   document.getElementById('sec1').innerHTML = el.textContent;
					//	 document.getElementById('station2').innerHTML = el.getAttribute('name'),el.getAttribute('name'),i;
					//selStation.add(el.textContent,++i);
					document.getElementById('sec1').innerHTML = '123123123';
                    })
                })
            })
        </script>

начиная с первой строки если выбрали то автоматом выбирается статион пр:
<station2>tpl_novosib1</station2>
.

ХМЛЬ:
<?xml version="1.0" encoding="utf-8"?>
<category>
    <city name="Россия" link="db/russia/ac/index.php">
		<station2>tpl_rus</station2>
	</city>
	<city name="Омск" link="acura.php">
		<station2>tpl_omsk</station2>
	</city>
	<city name="Новосибирск" link="acura.php">
		<station2>tpl_novosib1</station2>
	</city>
</category>

destus 14.03.2016 10:10

Цитата:

selStation = document.getElementById('#station2')
зачем #?
Цитата:

document.getElementById('sec1')
что такое sec1?

leon2009sp 14.03.2016 13:39

selStation = это как занесение в переменную. что то вроде var на delphi.
а sec1 - это как сектор блок. т.е. все что в sec1 отображается в див.
document.getElementById('sec1') можно заменить на  document.getElementById('station2')

от этого ничего не поменяется. а вто "#" юмора не понял. то что много записей? как массив?

leon2009sp 14.03.2016 16:03

если я делаю так:
selCity.add(new Option(el.getAttribute('name'), el.getAttribute('name')))
                 document.getElementById('sec1').innerHTML = el.textContent;

то выдает последний статион он у меня новосиб.
но мне же нужно в цикл его тогда переношу:
selCity.addEventListener('change', function () {
                     selStation.options.length = 0 , i=0;
	
                    [].forEach.call(xml.querySelectorAll('city[name=' + this.value + '] > station2'), function (el) {
     
					document.getElementById('sec1').innerHTML = el.textContent;
                    })

leon2009sp 14.03.2016 16:45

все доперло :dance:
проверти на ошибки пожалуйста
<script>
        function loadXMLDoc(filename) {
            if (window.ActiveXObject) {
                xhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            else {
                xhttp = new XMLHttpRequest();
            }
            xhttp.open("GET", filename, false);
            try { xhttp.responseType = "msxml-document" } catch (err) { } // Helping IE11
            xhttp.send(null);
            return xhttp.responseXML;
        }
        document.addEventListener('DOMContentLoaded', function () {
            var xml = loadXMLDoc("xml/city.xml"),
                selCity = document.querySelector('#city'),
              //  selStation = document.querySelector('#station2'),i;
				// elem = document.getElementById('my');
				
				selStation = document.getElementById('station2');
				
            [].forEach.call(xml.querySelectorAll('city'), function (el) {
                selCity.add(new Option(el.getAttribute('name'), el.getAttribute('name')))
		})
            selCity.addEventListener('change', function () {
               // selStation.options.length = 0 , i=0;
                [].forEach.call(xml.querySelectorAll('city[name=' + this.value + '] > station2'), function (el) {
                  //  selStation.add(new Option(el.textContent, ++i));
					//document.getElementById('station2').innerHTML = el.textContent;
					selStation.innerHTML = el.textContent;
                })
            })
        })
    </script>
    <select id=city><option selected>1111111</option></select>
    <div id=station2>333333</div>

destus 14.03.2016 17:15

leon2009sp,
поздравляю :victory:


Часовой пояс GMT +3, время: 19:02.