[jdom-interest] saving xml file

Simpson S simpson54 at rediffmail.com
Sat Nov 15 21:58:32 PST 2003


An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20031116/66e14b92/attachment.htm
-------------- next part --------------
I have found the solution. 
      XMLOutputter fmt = new XMLOutputter();
      fmt.setTextTrim(true);  //this would remove all spaces
      fmt.setIndent("  ");    // this will do indentation
      fmt.setNewlines(true);
thank you JDOM team for the nice product. good work!!

On Sat, 15 Nov 2003 Simpson  S wrote :
>To save my xml Document in to a file I am using this:
>
>XMLOutputter fmt=new XMLOutputter("  ",true); //true to generate new lines
>fmt.output(doc,fileStream);
>
>Sample xml file in my case is like
>
><rootElement>
><person>
>      student
>      <name>Alex></name>
></person>
></rootElement>
>
>When i get the content of person node, i get "    student  \n \n"
>Trimming it would probably give me the desired text "student". When i reopen the xml file generated by above code, alter it and save i get two more new lines and spaces like :
>
>
><rootElement>
>
><person>
>
>      student
>
>      <name>Alex></name>
>
></person>
>
></rootElement>
>
>How do i get rid of these spaces when i save it as xml file (I only want one line after the tags to indent the xml file and make it readable.). I need to re-open the xml files and save it again. Every time i reopen it and save, i would get new lines added. So, after 10times of saving and re-opening it there would be 10 blank lines in the xml file (between tag and its content) and it is not readable in text editors. Can someone tell me how do i get around this problem.
>
>In short: I want to save the Document object as xml file with indenting and new line characters. I would need to reopen the file, modify it and save it again. When I save the second time, additional new line characters are added
>Saving this xml file second time I would get
><person>
>   <name>
>     A
>   </name>
></person>
>
>After saving it second time.
><person>
>
>   <name>
>
>     A
>
>   </name>
>
></person>
>
>So, when I access name it is "\n  A  \n \n" kind. each time I save it, I get new lines added. This makes it difficult to edit it in text editors like notepad. How do i solve this problem.
>thank you very much!!
>


More information about the jdom-interest mailing list