- How to post XML to server thru HTML form?
- 5 Answers 5
- How to parse the xml data into html?
- 3 Answers 3
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- XML Applications
- The XML Document Used
- Display XML Data in an HTML Table
- Example
- Display the First CD in an HTML div Element
- Example
- Navigate Between the CDs
- Example
- Show Album Information When Clicking On a CD
- Example
- Display XML content in HTML page
How to post XML to server thru HTML form?
All I know is it goes to one of the CRM applications run on different domain. Now I’m not sure what is the best way to do this. I was thinking of just wrapping values of fields in my form when user submits the form. So if user typed «Homer» in «firstname» field and clicks submit, my JS would change the value of the field to
5 Answers 5
Posting XML without javascript or browser plugins is impossible. The two possible formats of posting html forms are application/x-www-form-urlencoded and multipart/form-data .
I just got this to work in chrome, the key is having the blank space in the text area name:
The best way I can think of is to intercept the form-submit action, and convert the form details into XML format, and then submit that to the server. There are many ways to do this, but the easiest would be to implement a solution via a framework like jQuery:
An example of this very thing can be found online at http://www.docunext.com/. data-to-xml-with-jquery which utilizes the JSON to XML Plugin:
$("#myform").submit(function()< var formjson = $('#myform').serializeArray(); var formxml = json2xml(formjson); $.post("/collect.php", < 'data': formxml >, function(data)< // callback logic >); return false; >);
@thomaskonrad I’ve fixed the links (by redirecting to archives on wayback). It’s likely some of the downloads and other resources may not work. Let me know if you encounter any further issues.
If servers-side code is an option, you could use a custom php CURL script as a middleman to forward your request on to the third party in an actual xml format. I’m not sure if CURL comes with a standard php installation, and if it’s not an option, you could probably use fsocketopen instead (though personally I think that tactic is harder). But CURL is easy enough to install and extremely useful for basically allowing php to send requests as if it’s a browser. The difference that you may be interested in here, is that it does actually allow you to set the header ‘Content-type: text/xml’.
So have your html form send off some regular GET or POST values to your php script. Then have that personal php script convert them into the XML format that the 3rd party is expecting. (Don’t forget to precede it with the tag, with whatever attribute values are appropriate for you.) And then send it off via this code:
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-type: text/xml', 'Content-length: '.strlen($xmlRequest), )); $output = curl_exec($ch); curl_close($ch);
How to parse the xml data into html?
In pure html, it’s impossible. You’ll have to use javascript or create the html page at server-sided.
HTML is a static programming language. You will need another language like php or javascript to do anything like this.
3 Answers 3
To do this your HTML file should contain some JavaScript code, so you will want to learn how to parse XML in Javascript.
Here is a good StackOverflow question on this topic: XML parsing in JavaScript
You can do by using PHP’s XML Library called simplexml for more information check this link http://www.w3schools.com/php/php_xml_simplexml.asp
NOTE : If you can elaborate on what technology you’re using, I’ll try to provide a more complete example.
I would suggest using XSLT for this. With XSLT, you can pass in the XML fragment, parse it, and return formatted HTML.
Here’s an example XSLT document that converts XML to HTML:
My CD Collection
Title Artist
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.27.43548
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
XML Applications
This chapter demonstrates some HTML applications using XML, HTTP, DOM, and JavaScript.
The XML Document Used
In this chapter we will use the XML file called «cd_catalog.xml».
Display XML Data in an HTML Table
This example loops through each element, and displays the values of the and the elements in an HTML table:
Example
For more information about using JavaScript and the XML DOM, go to DOM Intro.
Display the First CD in an HTML div Element
This example uses a function to display the first CD element in an HTML element with >
Example
function displayCD(i) var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() if (this.readyState == 4 && this.status == 200) myFunction(this, i);
>
>;
xmlhttp.open(«GET», «cd_catalog.xml», true);
xmlhttp.send();
>
function myFunction(xml, i) var xmlDoc = xml.responseXML;
x = xmlDoc.getElementsByTagName(«CD»);
document.getElementById(«showCD»).innerHTML =
«Artist: » +
x[i].getElementsByTagName(«ARTIST»)[0].childNodes[0].nodeValue +
«
Title: » +
x[i].getElementsByTagName(«TITLE»)[0].childNodes[0].nodeValue +
«
Year: » +
x[i].getElementsByTagName(«YEAR»)[0].childNodes[0].nodeValue;
>
Navigate Between the CDs
To navigate between the CDs, in the example above, add a next() and previous() function:
Example
function next() <
// display the next CD, unless you are on the last CD
if (i < x.length-1) <
i++;
displayCD(i);
>
>
function previous() // display the previous CD, unless you are on the first CD
if (i > 0) i—;
displayCD(i);
>
>
Show Album Information When Clicking On a CD
The last example shows how you can display album information when the user clicks on a CD:
Example
function displayCD(i) <
document.getElementById(«showCD»).innerHTML =
«Artist: » +
x[i].getElementsByTagName(«ARTIST»)[0].childNodes[0].nodeValue +
«
Title: » +
x[i].getElementsByTagName(«TITLE»)[0].childNodes[0].nodeValue +
«
Year: » +
x[i].getElementsByTagName(«YEAR»)[0].childNodes[0].nodeValue;
>
Display XML content in HTML page
Simple solution is to embed inside of a element, which will preserve both the formatting and the angle brackets. I have also removed the border with style=»border:none;» which makes the textarea invisible.
Thanks for this wonderful tips and introducing me to jsfiddle.net site. Will I be able to make textarea collapse-able ? like a section ?
Awesome answer! I was able to display XML text inside a kendo-grid cell with the help of this answer and the other link here: telerik.com/forums/multiline-textarea-in-a-grid-databinding. Thanks so much!
Awesome and smart answer. Even worked for java problem. I was trying to create html page based on dynamic data in java.
You can use the old tag. I don’t know about browser support, but it should still work.
your code/tables Bob Dylan USA Columbia 10.90
your code/tables Bob Dylan USA Columbia 10.90
«This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.» developer.mozilla.org/en/docs/Web/HTML/Element/xmp
worked for me too! I have been displaying xml content in a
tag for quite some time without any trouble. I just recently noticed it was not longer displaying as xml, this tiny fix took care of it
If you treat the content as text, not HTML, then DOM operations should cause the data to be properly encoded. Here’s how you’d do it in jQuery:
Here’s how you’d do it with standard DOM methods:
document.getElementById('container') .appendChild(document.createTextNode(xmlString));
If you’re placing the XML inside of HTML through server-side scripting, there are bound to be encoding functions to allow you to do that (if you add what your server-side technology is, we can give you specific examples of how you’d do it).