Elena Matovska
Elena Matovska

Reputation: 13

Get children of children from xml file

so im trying to extract data from a xml file which looks like this:

<example>
    <phrase>phrase1</phrase>
    <word>
      <definition>definition1</definition>
    </word>
    <word>
      <definition>definition2</definition>
    </word>
</example>

I want to be able to put definition1 and definition2 as seperate members of an array, for now i get them merged like this: definition1definition2

this is my code:

var $example = $xml.find("example");
                $example.each(function(){                   
        list.push($(this).children("word").children("definition").text());
                });

Does anyone have an idea? thanks

Upvotes: 0

Views: 71

Answers (3)

Vladu Ionut
Vladu Ionut

Reputation: 8193

jquery solution

    $example.find("word > definition").map(function(){ return $(this).text()}).toArray()

var xml =
    '<example>' +
    '<phrase>phrase1</phrase>' +
    '<word>' +
    '<definition>definition1</definition>' +
    '</word>' +
    '<word>' +
    '<definition>definition2</definition>' +
    '</word>' +
    '</example>' +
    '';
var xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');
var list = $("example > word > definition",xmlDoc).map(function(){ return $(this).text()}).toArray();
console.log(list);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

vanila js solution

// Code goes here
var list = [];
var txt = `<example>
    <phrase>phrase1</phrase>
    <word>
      <definition>definition1</definition>
    </word>
    <word>
      <definition>definition2</definition>
    </word>
</example>`;
if (window.DOMParser) {
  parser = new DOMParser();
  xmlDoc = parser.parseFromString(txt, "text/xml");
} else // Internet Explorer
{
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async = false;
  xmlDoc.loadXML(txt);
}

var elements = xmlDoc.getElementsByTagName("definition");

[].forEach.call(elements, function(el) {
  list.push(el.textContent)
});
console.log(list);

Upvotes: 0

Martin Honnen
Martin Honnen

Reputation: 167716

If you want to use JQuery, you need to change the

$example.each(function(){                   
        list.push($(this).children("word").children("definition").text());
                });

to

$example.children("word").children("definition").each(
  function() {                   
   list.push($(this).text());
  });

Upvotes: 1

cheralathan
cheralathan

Reputation: 572

Check out this Fiddle.

$(document).ready(function() {

var xml =
  '<example>' +
    '<phrase>phrase1</phrase>' +
    '<word>' +
      '<definition>definition1</definition>' +
    '</word>' +
    '<word>' +
      '<definition>definition2</definition>' +
    '</word>' +
  '</example>' +
'';
var xmlDoc = new DOMParser().parseFromString(xml, 'text/xml');
var x = xmlDoc.documentElement.childNodes;
var list = [];
for (i = 0; i < x.length; i++) {
  if (x[i].nodeName == 'word') {
    console.log(x[i].childNodes[0].innerHTML);
    list.push(x[i].childNodes[0].innerHTML);
  }
}
document.getElementById('demo').innerHTML = list;
console.log(list);
});

Upvotes: 0

Related Questions