Skip to content

Instantly share code, notes, and snippets.

@MichaelDimmitt
Last active October 12, 2024 15:17
Show Gist options
  • Save MichaelDimmitt/b578db7be725bdf82d2861a42650e4d7 to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/b578db7be725bdf82d2861a42650e4d7 to your computer and use it in GitHub Desktop.
how to parse xml

Extensible Markup Language is a strict serialization of the Document Object Model:
https://developer.mozilla.org/en-US/docs/Web/XML

Using this browser api DOMParser() you can parsing xml uses a node system that is similar to parsing html:
https://developer.mozilla.org/en-US/docs/Web/API/XMLDocument
https://developer.mozilla.org/en-US/docs/Web/API/DOMParser

Google: domparser example xml
https://stackoverflow.com/a/58279236/5283424

const xml = `<Abstract>
    <AbstractText Label="BACKGROUND">A large ...</AbstractText>
    <AbstractText Label="METHODS">We modeled....</AbstractText>
    <AbstractText Label="RESULTS">Mammary glands ... </AbstractText>
    <AbstractText Label="CONCLUSIONS">We report...</AbstractText>
</Abstract>`;

parser = new DOMParser();
xmlDoc = parser.parseFromString(xml, "text/xml");
const abstracts = xmlDoc.querySelectorAll("AbstractText");

console.log(abstracts[0])

// abstracts.forEach(a => {
//   console.log(a.textContent, a.getAttribute('Label'));
// });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment