DOMParser Typescript- Parsing HTML

Meena Mana picture Meena Mana · Dec 13, 2017 · Viewed 8.7k times · Source

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.

Answer

edkeveked picture edkeveked · Dec 13, 2017

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);
 }