ziko
Version:
A versatile JavaScript library offering a rich set of Hyperscript Based UI components, advanced mathematical utilities, interactivity ,animations, client side routing and more ...
47 lines (44 loc) • 1.41 kB
JavaScript
function parseXML(xmlString) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const rootNode = xmlDoc.documentElement;
const result = parseNode(rootNode);
return result;
}
function parseNode(node) {
const obj = {
type: node.nodeName,
attributes: {},
children: []
};
for (let i = 0; i < node.attributes.length; i++) {
const attr = node.attributes[i];
obj.attributes[attr.name] = attr.value;
}
for (let i = 0; i < node.childNodes.length; i++) {
const child = node.childNodes[i];
if (child.nodeType === Node.ELEMENT_NODE) {
obj.children.push(parseNode(child));
} else if (child.nodeType === Node.TEXT_NODE) {
obj.text = child.textContent.trim();
}
}
return obj;
}
export default parseXML;
// function htmlParser(element) {
// const obj = {
// type: element.tagName,
// attributes: {},
// children: [],
// };
// for (let i = 0; i < element.attributes.length; i++) {
// const attr = element.attributes[i];
// obj.attributes[attr.name] = attr.value;
// }
// for (let i = 0; i < element.children.length; i++) {
// const child = element.children[i];
// obj.children.push(htmlParser(child));
// }
// return obj;
// }