user983022
user983022

Reputation: 979

display actual xml with nodes as html but browser friendly

I want to display my XML ajax response in html for the browser. However what I need is the actual xml itself being displayed i.e. the full tree with the elements AND the content not just the content of the nodes.

So I want the xml to be displayed as the browser e.g.:

<element>content</element>
    <element2>niijojoi</element2>
    <element2>niijojoi</element2>
    <element2>niijojoi</element2>
 //etc more nested elements some with atrributes etc

I have a stylesheet that would display the text but does anyone have an xsl stylesheet that simply traverses the xml and outputs it to the browser as is with indentation etc.

thanks

Upvotes: 0

Views: 963

Answers (3)

Zoltan Toth
Zoltan Toth

Reputation: 47667

If you are getting your XML via Ajax - simply put the result in a textarea and then style the textarea any way you want.

textarea {
  background: #eee;
  width: 500px;
  height: 200px;
}
<textarea>
  
  <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
  </note>
  
</textarea>

Upvotes: 1

You
You

Reputation: 23774

Assuming you get your XML via AJAX and want to inject it into an element on the page (a <pre> element would make sense), all you have to do is to replace < characters with the &lt; entity before injecting the content into the innerHTML of the <pre> element:

// "result" is the data from the AJAX request.
$('#xml').innerHTML = result.replace('<', '&lt;');

Upvotes: 1

sizzle
sizzle

Reputation: 2332

This is automatically performed by a browser. For instance, if I create a file test.xml and put this in it:

<a>
  <element>content</element>
  <element1>
    <element2>niijojoi</element2>
    <element2>niijojoi</element2>
    <element2>niijojoi</element2>
  </element1>
</a>

and open it in Google Chrome, it shows the XML hierarchy with indentation.

Upvotes: 0

Related Questions