In my XML data, I have extracted all the tag names using a for loop and some other logic. Now, I am looking to find the word 'author' from the output values that are displayed in the console during the loop. If any of the output values match 'author', then I need to execute a specific condition. Here is the code snippet:
app.component.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'project';
ngOnInit(): void {
let xmldata = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
var node = (new DOMParser()).parseFromString(xmldata, "text/xml").documentElement;
var nodes = node.querySelectorAll("*");
// console.log(nodes[4].tagName)
for (var i = 0; i < nodes.length; i++) {
var text = null;
if (nodes[i].childNodes.length == 1 && nodes[i].childNodes[0].nodeType == 3) //if nodeType == text node
text = nodes[i].textContent; //get text of the node
// console.log("TageName : ", nodes[i].tagName, ", Text : ", text);
console.log(nodes[i].tagName)
}
}
}