3

Currently I am trying something very simple. I am looking through an XML document for a certain phrase upon which I try to replace it. The problem I am having is that when I read the lines I store each line into a StringBuffer. When I write the it to a document everything is written on a single line.

Here my code:

File xmlFile = new File("abc.xml")
BufferedReader br = new BufferedReader(new FileReade(xmlFile));
String line = null;
while((line = br.readLine())!= null)
{
    if(line.indexOf("abc") != -1)
    {
        line = line.replaceAll("abc","xyz");
    }         
    sb.append(line);                
}
br.close();

BufferedWriter bw = new BufferedWriter(new FileWriter(xmlFile));
bw.write(sb.toString());
bw.close();

I am assuming I need a new line character when I prefer sb.append but unfortunately I don't know which character to use as "\n" does not work.

Thanks in advance!

P.S. I figured there must be a way to use Xalan to format the XML file after I write to it or something. Not sure how to do that though.

Bilzac
  • 555
  • 3
  • 9
  • 23

4 Answers4

7

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);
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Brel
  • 131
  • 5
  • 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 06 '10 at 16:10
1

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>abc</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.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
0

Sb would be the StringBuffer object, which has not been instantiated in this example. This can added before the while loop:

StringBuffer sb =  new StringBuffer();
rayryeng
  • 102,964
  • 22
  • 184
  • 193
Konark K
  • 147
  • 8
  • We are assuming that an instance of a `StringBuffer` has already been created. This does not help in answering the question. Suggest you read the question again. – rayryeng Jul 08 '14 at 05:31
  • @ rayryeng :- added instance of StringBuffer in connection to one answer , which itself was asking about variable "sb" – Konark K Jul 08 '14 at 10:31
  • You could leave that as a comment. It still doesn't constructively answer the question though. Either way, good of you to clear things up. – rayryeng Jul 08 '14 at 13:18
  • FWIW - That answer was removed because it was an answer that wasn't constructive. It was seeking clarification, which should be left as a comment as well. – rayryeng Jul 08 '14 at 13:57
-1
Scanner scan = new Scanner(System.in);
String filePath = scan.next();
String oldString = "old_string";
String newString = "new_string";
String oldContent = "";
BufferedReader br = null;
FileWriter writer = null;
File xmlFile = new File(filePath);
try {
    br = new BufferedReader(new FileReader(xmlFile));
    String line = br.readLine();
    while (line != null) {
        oldContent = oldContent + line + System.lineSeparator();
        line = br.readLine();
    }
    String newContent = oldContent.replaceAll(oldString, newString);
    writer = new FileWriter(xmlFile);
    writer.write(newContent);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        scan.close();
        br.close();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Clijsters
  • 4,031
  • 1
  • 27
  • 37
  • 3
    Welcome to StackOverflow. In order to provide OP a solution he can understand and work with please format your code correctly. Furthermore you have to explain your code what you did and why, so OP understands how his issue gets solved by your solution. – L. Guthardt May 22 '18 at 07:46