I'm running into some trouble with the DOMParser. I'm using intelliJ IDE and writing in Typescript.
Here is my code:
public domparser = new DOMParser();
this.domdoc = this.domparser.parseFromString(this.xmlDoc, 'text/xml');
console.log("domdoc: " + this.domdoc);
I'm seeing
domdoc: [object XMLDocument]
in my console.
Any suggestions on how to print the XML document, rather than just '[object XMLDocument]'?
Thank you.
First you instantiate a new DOMParser instance and pass it your HTML string using parseFromString (). For this example, let’s say that we stored the HTML string in a variable called htmlContent:
DOM (Document Object Model) allows us to dynamically access and manipulate the HTML data. All the text data from an HTML file can also be extracted using DOMParser. DOM parser returns an HTML/XML/SVG object.
Declare an instance of DOMParser. Parse the document using .parseFromString () function. It takes two arguments, the string to be parsed and the type of document. Use doc.all element to access the whole HTML page, now get its root element which is stored at 0 th index. We can also use getElementByID () to get content of a specific element.
First you instantiate a new DOMParser instance and pass it your HTML string using parseFromString (). For this example, let’s say that we stored the HTML string in a variable called htmlContent: And now parsedHtml is a DOM object that can be interacted with. Let’s extract a few things from it:
You can use the querySelector and the wildcard *
to select all the xml element.
public domparser = new DOMParser();
this.domdoc = this.domparser.parseFromString(this.xmlDoc, 'text/xml');
let elements = this.domdoc.querySelectorAll("*");
for (element of elements){
console.log(element.innerHTML);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With