I'm making an api call to a site using the code as shown below :
$xmlData = file_get_contents("http://isbndb.com/api/books.xml?access_key=XXXXXX&index1=isbn&value1=0596002068");
echo $xmlData;
However xmlData when displayed on the browser is auto parsed to HTML. For e.g. The element <title> of the returned XML which is actually a book title is converted to HTML essentially becoming the page title, and the other XML elements are displayed as plain text without the tags. I want the client side XMLHttpRequest Object to get raw XML data from the server side.
Why does this happen and how do I ensure that XML is not auto parsed?
PHP just sees it as text. For instance, do echo "<b>Bold</b>"; and it will "automatically" be in bold. It is the browser that processes the HTML and renders it.
This is what htmlspecialchars is for.
This got nothing to do with php. you spit out elements which browser interprets as HTML (that's why it sets title). Build your html page right, use <pre> tags around your content, or. when needed, send your content with correct content-type header (like text/plain to display your xml for viewing or text/xml for other purposes) so it will not parse your data as html.
Related
I'm pulling data from a url string thats converting to XML. I have it placed on my webpage and most of it is displaying as expected. But the developer in their wisdom used an HTML tag as an XML tag <option>.
I can't share the whole thing but it's basically this url string displaying the xml below:
http://private.com/webservice6.asp?c=00000000&p=Int0000000&stock_symbols=AAPL,GE&classes=BOD,,OPTION&from=5/1/2021&to=6/1/2021&v=2&o=EVENTS
Its outputting like this:
<option>
<event_id>4ICO1KYD</event_id>
<company_id>1084</company_id>
</option>
So the browser thinks its an HTML webform select option. It's displaying the data but as one option block and I can't target the sub tags.
I have no control of the xml markup.
Is there any way of escaping or wrapping the whole thing to tell the browser to treat it as xml vs html?
I am working on a SpringBoot based MVC application which uses mongoDB to store the data. I am using thymeleaf as the template engine. In one of the scenarios, the user needs to fill a form which is then displayed on some view.
The problem I am facing is that the user can use html tags to format the data while writing in the textArea of the form (code snippets, tabular format etc). But when I am displaying that text, the html is not being parsed and is displayed as is.
For Ex: <b>String</b> should be displayed as String but is being displayed as <b>String</b> only. When I check the source code of the page, the html tags are displayed as encoded i.e. < is showing as < ; etc and hence the parsing is not happening.
Can someone please help
You can output unescaped text with th:utext. From the official turorial
If we want Thymeleaf to respect our XHTML tags and not escape them, we will have to use a different attribute: th:utext (for “unescaped text”):
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
The tutorial assumes that home.welcome is a string with html-tags:
home.welcome=Welcome to our <b>fantastic</b> grocery store!.
It goes without saying that this needs very careful validation so that only safe (whatever safe is for the particular use case) HTML is stored into the database (and no possibly malicious code like <script/> tags).
All html tags goes to uppercase letters when get body's innerHTML by webbrowser.
how should I do to get the real htmlcode?
There is no way to get the "real" HTML code from innerHTML.
By the time you read innerHTML, the HTML has already been parsed to a DOM and then discarded. innerHTML will serialise the DOM to HTML, it won't give you the original HTML.
The only way to get the original HTML would be to use XMLHttpRequest to get a fresh copy of the source code from the server and then parse it with a custom parser instead of the one built into the browser.
So I have a servlet which prints content of various files. But when I want to print .xml file my servlet page doesn't print anything, because page uses this xml tags as html and is parsing them istead of printing. And I want to print this tags. I am reading file line by line and lines are stored in variable line.
If you want to print xml content in your HTMl page, you can use StringEscapeUtils.escapeHtml() function from Apache commons lang library to write xml file contents to your HTML page
PrintWriter writer = response.getWriter();
writer.write("<html><head></head><body>");
writer.write(StringEscapeUtils.escapeHtml(xmlContent);
writer.write("</body></html>");
If you are attempting to Display XML as content in an HTML document:
Browsers can't tell the difference better a < that the author intends to mean "Start of tag" and one that the author intends to mean "Render this".
You need to represent it as < if you want it to appear as data.
The answer to htmlentities equivalent in JSP? explains how to convert a string of text into a string of HTML.
If you are attempting to Output an XML document instead of an HTML document:
You need to specify an XML content type (such as application/xml) instead of an HTML content-type.
See How to set the content type on the servlet for an explanation.
I am trying to display the real value of ¼ and ”, but the browser is displaying it as ? (question mark) at a area where AJAX is enabled. While at a different browser locations it displays ¼”.
I want to display 1" but it getting displayed as 1â€.
Please advise.
I would guess you're doing something like fetching HTML from a second page and writing it to innerHTML.
Make sure that all your HTML is saved in the UTF-8 encoding. XMLHttpRequest.responseText will decode content from UTF-8, unless the response contains a Content-Type: ...;charset=something-else header. (A <meta> element is not good enough as XMLHttpRequest doesn't parse HTML to get the meta out; it has to be a proper HTTP header.)