TExcelTAD
Home › XML › DOM Get Values

XML DOM Get Values

Most of the time you don't care about nodes for their own sake — you want the values: an element's text, an attribute's value, a number to calculate with. This lesson collects the ways to read them.

Element text

Way Returns Notes
el.textContent All descendant text, joined Best default choice
el.firstChild.nodeValue Text of the first child node Fails if the element is empty
el.childNodes[0].data Same as above Old style
html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<book id="b1">
      <title>The Hobbit</title>
      <subtitle/>
      <blurb>A <em>reluctant</em> hobbit goes on an adventure.</blurb>
    </book>`;
    const xmlDoc = new DOMParser().parseFromString(xml, 'application/xml');

    console.log('title    :', xmlDoc.querySelector('title').textContent);
    console.log('subtitle :', JSON.stringify(xmlDoc.querySelector('subtitle').textContent));
    console.log('blurb    :', xmlDoc.querySelector('blurb').textContent);
    console.log('blurb first text node only:', xmlDoc.querySelector('blurb').firstChild.nodeValue);
  </script>
</body>
</html>

Notice that textContent on <blurb> includes the text inside <em>, while firstChild.nodeValue stops at "A ".

Note: textContent keeps all white space exactly as written. Use .trim() when the XML is indented and you only want the words.

Attribute values

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xmlDoc = new DOMParser().parseFromString(
      '<book id="b1" genre="fantasy" pages="310"><title lang="en">The Hobbit</title></book>', 'application/xml');
    const book = xmlDoc.documentElement;

    console.log('getAttribute:', book.getAttribute('genre'));
    console.log('missing attr:', book.getAttribute('isbn'));        // null
    console.log('with default:', book.getAttribute('isbn') ?? 'unknown');
    console.log('attribute node value:', book.getAttributeNode('pages').value);
    console.log('nested attribute:', book.querySelector('title').getAttribute('lang'));
  </script>
</body>
</html>

getAttribute returns null when the attribute doesn't exist — the ?? operator gives a neat fallback.

Converting types

Every value read from XML is a string. Convert it before doing maths or comparisons:

Want Use Watch out for
Number Number(s) or parseFloat(s) Number('') is 0, Number('abc') is NaN
Integer parseInt(s, 10) Always pass the radix 10
Boolean s === 'true' Boolean('false') is true!
Date new Date(s) for ISO dates like 2026-05-02 Prefer ISO strings in the XML
html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<library>
      <book inStock="true"><title>The Hobbit</title><price>8.99</price><year>1937</year></book>
      <book inStock="false"><title>A Brief History of Time</title><price>12.50</price><year>1988</year></book>
      <book inStock="true"><title>Cien años de soledad</title><price>10.00</price><year>1967</year></book>
    </library>`;
    const xmlDoc = new DOMParser().parseFromString(xml, 'application/xml');

    const books = [...xmlDoc.querySelectorAll('book')].map(b => ({
      title: b.querySelector('title').textContent,
      price: Number(b.querySelector('price').textContent),
      year: parseInt(b.querySelector('year').textContent, 10),
      inStock: b.getAttribute('inStock') === 'true',
    }));

    console.table(books);
    const available = books.filter(b => b.inStock);
    console.log('In stock:', available.length, '— value €' + available.reduce((s, b) => s + b.price, 0).toFixed(2));
    console.log('Oldest:', books.reduce((a, b) => (a.year < b.year ? a : b)).title);
  </script>
</body>
</html>

Turning XML into an array of plain objects like this is a very common first step: after that, the rest of your code doesn't need to know anything about the DOM.

Values with XPath

XPath can return a value directly, and handles missing nodes gracefully (empty string instead of an error):

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xmlDoc = new DOMParser().parseFromString(
      '<book id="b1"><title>The Hobbit</title><price>8.99</price></book>', 'application/xml');
    const str = (xp) => xmlDoc.evaluate(xp, xmlDoc, null, XPathResult.STRING_TYPE, null).stringValue;

    console.log('title :', str('/book/title'));
    console.log('id    :', str('/book/@id'));
    console.log('isbn  :', JSON.stringify(str('/book/isbn')));   // missing → ""
  </script>
</body>
</html>

Test yourself

What does getAttribute('isbn') return if the element has no isbn attribute?

  1. ""
  2. undefined
  3. null
  4. It throws an error

What does Boolean("false") evaluate to?

  1. false
  2. true
  3. null
  4. NaN

For <p>Hello <b>world</b></p>, what is p.textContent?

  1. Hello
  2. Hello world
  3. world
  4. <b>world</b>