Created
November 26, 2022 22:04
-
-
Save elycruz/46396d563993e8b499004f1f69d9c139 to your computer and use it in GitHub Desktop.
Basic usage of `DOMParser` (for XML)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {log} = console, | |
groupContent = '.'.repeat(5).split('').reduce((agg, _, i) => { | |
return agg + `<item>` + | |
`<title>Item ${i}</title>` + | |
`</item>`; | |
}, ''), | |
docContent = `<group>${groupContent}</group>`, | |
parsedToDOM = new DOMParser().parseFromString(docContent, 'application/xml'), | |
groups = parsedToDOM.children.item(0), | |
groupsLen = groups.childElementCount; | |
log(`Num children in \`group\`: | |
${parsedToDOM.children.item(0).childElementCount}`); | |
// Looping through elements in `NodeList` | |
for (let i = 0; i < groupsLen; i += 1) { | |
const group = parsedToDOM.children.item(i); | |
// ... | |
} | |
// Looping through elements via modern (potentially slower (depending on browser impl.)) approach | |
for (const group of parsedToDOM.children) { | |
log('group: '); | |
// ... | |
for (const item of group.children) { | |
log('item: ', item); | |
// ... | |
// if (failedPredicate) continue; | |
// if (matchingPredicate) { | |
// performSomeWork(); | |
// } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment