Read XML, Replace Text and Write to same XML file via Java?

The readline reads everything between the newline characters so when you write back out, obviously the newline characters are missing. These characters depend on the OS: windows uses two characters to do a newline, unix uses one for example. To be OS agnostic, retrieve the system property "line.

Separator": String newline = System. GetProperty("line. Separator").

The readline reads everything between the newline characters so when you write back out, obviously the newline characters are missing. These characters depend on the OS: windows uses two characters to do a newline, unix uses one for example. To be OS agnostic, retrieve the system property "line.

Separator": String newline = System. GetProperty("line. Separator"); and append it to your stringbuffer: sb.

Append(line). Append(newline).

2 Another possibility is to use a PrintWriter and its println method, which will automagically append the system-correct line terminator to any string output. In fact, I'd say this is the "standard" way to write lines to an output file. – Carl Smotricz Jul 6 '10 at 16:10 Awesome worked!

– Bilzac Jul 6 '10 at 16:30.

Modified as suggested by Brel, your text-substituting approach should work, and it will work well enough for simple applications. If things start to get a little hairier, and you end up wanting to select elements based on their position in the XML structure, and if you need to be sure to change element text but not tag text (think abc), then you'll want to call in in the cavalry and process the XML with an XML parser. Essentially you read in a Document using a DocuemntBuilder, you hop around the document's nodes doing whatever you need to, and then ask the Document to write itself back to file.

Or do you ask the parser? Anyway, most XML parsers have a handful of options that let you format the XML output: You can specify indentation (or not) and maybe newlines for every opening tag, that kinda thing, to make your XML look pretty.

Thanks for the reply! – Bilzac Jul 6 '10 at 17:37.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions