0

I'm doing a small XML enricher based on JS, which, given a XML file, computes some stuff in the tree, and print on a <pre></pre> tag (prettified with highlight.js) the content of my modified file.

Because I use jQuery for computations, I obtain at the end of the process a jQuery object.

For now, my code is only : $("#output").append(xmlComputed)

But obviously, in the browser, I obtain a parsed form of my xml content, without any tags.

So I would like to know what kind of method I can use to obtain the full xml node in string from a jQuery node, like with .html() I suppose?

And so, I'll be able to escape then the special characters and print the result.

Thank you in advance!

[Edit] I've ever tried this : Convert xml to string with jQuery But I obtain only the beginning of my xml document, eg <!--xml version=1.0-->

Community
  • 1
  • 1
DGallet
  • 44
  • 1
  • 5

3 Answers3

0
$("button").click(function(){
    $("pre").load("test.xml", function(responseTxt, statusTxt, xhr){
        if(statusTxt == "success")
            alert("External content loaded successfully!");
        if(statusTxt == "error")
            alert("Error: " + xhr.status + ": " + xhr.statusText);
    });
});

Like that u can get your xml file using jquery. Here you can get the entire xml in the format of string. Then you can play with your own style

Parthasarathy
  • 308
  • 2
  • 10
  • Thank you for your answer, but unfortunately, the problem comes from the final phase, so from a jQuery object to a string, the file is already loaded. – DGallet Sep 19 '16 at 05:10
0
You can use dataType: 'xml' within your ajax.
sunilsingh
  • 503
  • 1
  • 5
  • 9
  • There's nothing about ajax in my script, but maybe if you've an idea how to turn the problem into sonething similar, I could solve the situation... – DGallet Sep 19 '16 at 05:12
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Sep 19 '16 at 14:00
0

Finally I've manage to find something little by little...

So at the beginning I had a jQuery object namely xmlComputed.

I have converted in Javascript node using .get() function, without any parameter, which returns an array of nodes.

The first entry of the array was xml initial tag <!--xml version=1.0-->, then the second was a text content, and my main node was the third (index 2) (which solves actually the Failed to execute 'serializeToString' on 'XMLSerializer': parameter 1 is not of type 'Node'. problem).

Using the XMLSerializer, as mentionned on many posts, I get a string in xmlString

var oSerializer = new XMLSerializer(); 
var xmlString = oSerializer.serializeToString(gpxContent.get()[2]);

Then for escaping, just following another answer how to escape xml entities in javascript? namely

if (!String.prototype.encodeHTML) {
  String.prototype.encodeHTML = function () {
    return this.replace(/&/g, '&amp;')
               .replace(/</g, '&lt;')
               .replace(/>/g, '&gt;')
               .replace(/"/g, '&quot;')
               .replace(/'/g, '&apos;');
  };
}

And by doing xmlString.encodeHTML(), I obtain a quasi prettified text.

Hope it could be useful for someone !

Community
  • 1
  • 1
DGallet
  • 44
  • 1
  • 5