Stev
Stev

Reputation: 307

How to get plain xml from file

I search for a method to get plain xml(with tags ect.) from a file and save it so the localStorage.

I found a few opportunities, but every one of them returns the xml without tags. I prefer jQuery to do this...

I tried $.get, $("").load() and AJAX but I don't get it. I just want to save the whole xml as string into the localStorage and read it out later (and work with it).

Does anyone have a idea?

Regards

Upvotes: 0

Views: 738

Answers (1)

Minko Gechev
Minko Gechev

Reputation: 25682

You can use:

$.ajax({
   url: 'http://example.com',
   dataType: 'text',
   success: function (data) {
      localStorage.setItem('xml-content', data);
   }
});

This will provide you the XML document as plain text and save it to the localStorage.

Here is a full solution:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<pre id="output">
</pre>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
    function setXML() {
        $.ajax({
           url: 'test.xml',
           dataType: 'text',
           success: function (data) {
              localStorage.setItem('xml-content', data);
              getXML();
           }
        });
    }
    function getXML() {
        var xml = localStorage.getItem('xml-content');
        $('#output').text(xml);
    }
    setXML();
</script>

</body>
</html>

Upvotes: 2

Related Questions