You are here: Reference > JavaScript > client-side > xml handling > properties > xml (DocumentFragment, XMLDocument)

xml property (DocumentFragment, XMLDocument)

Browser support:
Returns the node and its descendants as a string.
In other browsers, the XMLSerializer object provides similar functionality. See the example below.


You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

String that contains the contents of the node.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the xml property:
    <script type="text/javascript" src="ajax.js"></script>
    <script type="text/javascript">
        var httpRequest = null;
        function SendRequest () {
            if (!httpRequest) {
                httpRequest = CreateHTTPRequestObject ();   // defined in ajax.js
            if (httpRequest) {          
                    // The requested file must be in the same domain that the page is served from.
                var url = "news.xml";
       ("GET", url, true);    // async
                httpRequest.onreadystatechange = OnStateChange;
                httpRequest.send (null);

        function OnStateChange () {
            if (httpRequest.readyState == 0 || httpRequest.readyState == 4) {
                if (IsRequestSuccessful (httpRequest)) {    // defined in ajax.js
                    DisplayContent ();
                else {
                    alert ("Operation failed.");

        function DisplayContent () {
            var xmlDoc = ParseHTTPResponse (httpRequest);   // defined in ajax.js
            if (!xmlDoc)

            if (window.XMLSerializer) { // all browsers, except IE before version 9
                var serializer = new XMLSerializer();
                    // the serializeToString method raises an exception in IE9
                try {
                    var str = serializer.serializeToString (xmlDoc.documentElement);
                    alert (str);
                catch (e) {

            if ('xml' in xmlDoc) {  // Internet Explorer
                alert (xmlDoc.xml);

            alert("Cannot display the contents of the XML document as a string.");
    <button onclick="SendRequest ()">Display XML file content</button>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content