7

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.

3 Answers 3

3

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);
 }
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for the quick response. I'm getting 'domdoc: {"location":null}...that's not quite what I wanted...
@MeenaMana, do you want to get the exact text message of you xml file?
Yes. @edkeveked
@MeenaMana, could you please put in your question your XML content? it will be easier to query it
It's copyright-protected so I couldn't do that...basically it's a list of names of programs.
|
1

console.log("domdoc: " + this.domdoc); this line need some changes. Please remove the string "domdoc:" appended with the object.

Final code looks like:

  public domparser = new DOMParser();  
  this.domdoc = this.domparser.parseFromString(this.xmlDoc, 'text/xml');
  console.log(this.domdoc);

Comments

0

For loop like the first answer cannot be used in Angular 11.

This can't be used

for (element of elements){
}

This can be used

let parsedXMLResponse = (new DOMParser()).parseFromString("some string value", "text/xml");
let result = parsedXMLResponse.getElementsByTagName("<name of tag>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.