Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to display raw XML in a textarea

Status
Not open for further replies.

stevexff

Programmer
Mar 4, 2004
2,110
GB
I have a test harness that needs to display the actual XML data returned from a messaging system. Currently the result gets displayed in a <textarea>, but any XML entities such as &amp; get displayed by the browser as the translated value &

As I am trying to display the 'real' XML returned by the application, this is a bit of a problem. I don't really want to modify the data prior to display in case this introduces artefacts, is there a way to prevent entity substitution in the same way as <pre> does for formatting?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Sadly, no. The test harness is a J2EE app, the HTML is only used to display the results. I just want to display the 'real' result rather than my browser's interpretation of it.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Ended up doing a String.replaceAll("&", "&amp;") on the XML string before rendering it.

This gets over the problem of the browser interpreting any XML entities in the string, and allows the resulting display to be cut and pasted into other tools without error.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
An easier way would be to place it in a javascript variable then call a function to populate your xml

Code:
<script>
var xml = " <?xml version='1.0'?> <bookstore>   <book category='cooking'>      <title lang='en'>         everyday italian      </title>      <author>         giada de laurentiis      </author>      <year>         2005      </year>      <price>         30.00      </price>   </book>   <book category='children'>      <title lang='en'>         harry potter      </title>      <author>         j k. rowling      </author>      <year>         2005      </year>      <price>         29.99      </price>   </book>   <book category='web'>      <title lang='en'>         xquery kick start      </title>      <author>         james mcgovern      </author>      <author>         per bothner      </author>      <author>         kurt cagle      </author>      <author>         james linn      </author>      <author>         vaidyanathan nagarajan      </author>      <year>         2003      </year>      <price>         49.99      </price>   </book>   <book category='web'>      <title lang='en'>         learning xml      </title>      <author>         erik t. ray      </author>      <year>         2003      </year>      <price>         39.95      </price>   </book></bookstore>";
</script>
<html>
<head></head>
<body body onload="document.getelementbyid('mytextarea').value = xml;">
<form>
<input type="textarea" id="mytextarea" value="" >
</form>
</body>
</html>

I don't know the answer but my good friend Google does.
 
I'll give it a try, but I'm not convinced. I don't see any XML entities in your example...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I wouldn't use a javascript method as this may not work for all users. Are you using a server-side language to serve these pages?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
JSP. That's why it was easy enough to stick in the replaceAll(). Good point about the JavaScript, however - you can just turn it off in the browser, so it's not very robust.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top