получение списка имен элементов из xml с использованием scala или javascript - PullRequest
0 голосов
/ 09 мая 2018

Мой xml-файл выглядит ниже

<?xml version="1.0" encoding="UTF-8"?>
<root>
<city>
<ID>1</ID>
<Name>Kabul</Name>
<CountryCode>AFG</CountryCode>
<District>Kabol</District>
<Population>1780000</Population>
</city>
<city>
<ID>2</ID>
<Name>Qandahar</Name>
<CountryCode>AFG</CountryCode>
<District>Qandahar</District>
<Population>237500</Population>
</city>
<city>
<ID>3</ID>
<Name>Herat</Name>
<CountryCode>AFG</CountryCode>
<District>Herat</District>
<Population>186800</Population>
</city>
</root>

Мне нужно получить имена элементов («ID», «Name», «CountryCode», «District», «Population»), используя scala или javascript. пожалуйста, помогите.

1 Ответ

0 голосов
/ 09 мая 2018

Для JavaScript, просто так, как вы это делаете с элементами в DOM.(Но не забудьте разобрать XML!)

var xml = '<?xml version="1.0" encoding="UTF-8"?><root><city><ID>1</ID><Name>Kabul</Name><CountryCode>AFG</CountryCode><District>Kabol</District><Population>1780000</Population></city><city><ID>2</ID><Name>Qandahar</Name><CountryCode>AFG</CountryCode><District>Qandahar</District><Population>237500</Population></city><city><ID>3</ID><Name>Herat</Name><CountryCode>AFG</CountryCode><District>Herat</District><Population>186800</Population></city></root>';

var parser = new DOMParser();
// Parse the xml
var parsedXML = parser.parseFromString(xml, 'text/xml');

// Now get the elements
var id = parsedXML.getElementsByTagName('ID');
var Name = parsedXML.getElementsByTagName('Name');
var countryCode = parsedXML.getElementsByTagName('CountryCode');
var district = parsedXML.getElementsByTagName('District');
var population = parsedXML.getElementsByTagName('Population');

// Get the tag names; because getElementsByTagName returns an array like HTMLCollection, we need to loop through the results like we would with an array
for (var i = 0; i < id.length; i++) {
  console.log(id[i].tagName + " " + Name[i].tagName+ " " + countryCode[i].tagName + " "+ district[i].tagName + " "+ population[i].tagName);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...