1

I've been reading other posts similar to this issue but im still unable to render my xml properly in my app.

I've used the following to get my xml ready:

  const parser = new DOMParser();
  const xml = parser.parseFromString(response, 'application/xml');
  this.questionSet = xml.documentElement;

  console.log(this.questionSet);

This is what is in the console:

enter image description here

How can i now render this in the HTML?

Im currently trying this:

<div class="application-xml" innerHtml="{{questionSet}}"></div>

But its just printing: [object Element]

Any help would be great, thanks

4
  • You need to implement dom sanitizer.. angular security blocks this Commented Feb 11, 2019 at 12:38
  • how would i do this? Commented Feb 11, 2019 at 12:42
  • adding answer wait Commented Feb 11, 2019 at 12:43
  • can you paste your xml data Commented Feb 11, 2019 at 12:43

2 Answers 2

2

Angular security Blocks dynamic rendering of HTML and other scripts. You need to bypass them using DOM Sanitizer.

Read more here : Angular Security

DO below changes in your code :

// in your component.ts file

//import this 
import { DomSanitizer } from '@angular/platform-browser';


// in constructor create object 

constructor( 
...
private sanitizer: DomSanitizer

...
){

}

someMethod(){

  const parser = new DOMParser();
  const xml = parser.parseFromString(response, 'application/xml');
  this.questionSet = xml.documentElement;
  console.log(this.questionSet);
  
 this.htmlData = this.sanitizer.bypassSecurityTrustHtml( this.questionSet); // this line bypasses angular scurity
 
 

}

 
<!-- In Your html file-->

<div [innerHtml]="htmlData">
   
</div>

Here is the StackBlitz working Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Im getting [object Element] in your stackBlitz. Are you not getting that?
why dont you post your xml..so that i can check
Because its massive! Is the StackBlitz working for you?
The Stack Blitz is now working and i've got it working in my app. Thanks very much for your help.
0

Try to put innerHtml property between brackets: <div class="application-xml" [innerHtml]="questionSet"></div>.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.