1

I have an AJAX response of XML file in a variable. After doing changes in the elements, I want to save it as formatted XML.

function sendXML() {
    var serializer = new XMLSerializer();
    var toString = serializer.serializeToString(xml_dom);
    $.ajax({
        type: "POST",
        url: "/ReceiverService/printerconfigxml",
        dataType: "text",
        data: {
            val: toString
        },
    });    
};

xml_dom is the variable in which the AJAX response is stored (entire XML).

On the other side, the servlet is handling the saving part, but I want to send formatted XML to the servlet.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
dixit thareja
  • 83
  • 1
  • 11
  • why can't you format at server? – charlietfl Feb 09 '16 at 13:43
  • I want to do it on client side..i want that my servlet will handle the save request only – dixit thareja Feb 09 '16 at 13:52
  • Then I suggest you find a library for this because formatting xml is not a standard javascript process – charlietfl Feb 09 '16 at 13:54
  • http://stackoverflow.com/questions/376373/pretty-printing-xml-with-javascript .............i had used this function but its not formatting properly – dixit thareja Feb 09 '16 at 13:55
  • Isnt it possible without using any external library? – dixit thareja Feb 09 '16 at 13:57
  • Can someone help me how to format XML on client side !! – dixit thareja Feb 16 '16 at 15:45
  • no...do it at the server where you have access to libraries that will do this for you. Or find a library that will do it in javascript (not likely as easy to find since it is not a common problem) – charlietfl Feb 16 '16 at 15:50
  • But I want to achieve this result on client side as this is my requirement...!! – dixit thareja Feb 16 '16 at 15:51
  • Then look for a library to do it – charlietfl Feb 16 '16 at 15:52
  • The reason I had posted here that I dont want to use any library ..thats why i am looking for an help !! – dixit thareja Feb 16 '16 at 15:54
  • well if the solution you found above isn't formatting properly how would anyone here know what the specific problem is? – charlietfl Feb 16 '16 at 15:56
  • Not correctly means it is not coming according to formatted xml file and in my case all the childrens of parent have no spaces or tabs as it should be in XML file – dixit thareja Feb 16 '16 at 16:03
  • create a demo that reflects this. Nobody can help without code – charlietfl Feb 16 '16 at 16:23
  • Possible duplicate of [How to print pretty xml in javascript?](http://stackoverflow.com/questions/1187451/how-to-print-pretty-xml-in-javascript) – emartinelli Feb 16 '16 at 18:22
  • @charlietfl Identation, removing unwanted spaces , tabs ...this way i want to format my xml file... as you do in notepad++ editor which I think written in C++, same thing I want to do but in javascript – dixit thareja Feb 17 '16 at 13:24
  • @emartinelli This is not helpful for me because it is if you want to show in a browser...In my case I am sending ajax request to xml and getting response in a variable...after that doing some changes in that variable and then using ajax post to save it on server. xml_dom is the response in above code I am getting and after changes sending to the ajax post after serializing – dixit thareja Feb 17 '16 at 13:31

0 Answers0