Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMParser Typescript- Parsing HTML

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.

like image 798
Meena Mana Avatar asked Dec 13 '17 21:12

Meena Mana


People also ask

How do I parse an HTML string in domparser?

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:

What is the use of DOM parser?

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.

How to get the content of a specific element using domparser?

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.

How do I parse an HTML string from a variable?

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:


1 Answers

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);
 }
like image 189
edkeveked Avatar answered Sep 25 '22 10:09

edkeveked