Java 7 for Absolute Beginners

(nextflipdebug5) #1

CHAPTER 9 ■ WRITING AND READING XML


Writing XML with Strings


Here's the code to produce the same output by writing out a String object.

Listing 9-5. Writing XML with Strings

package com.bryantcs.examples.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteWithStrings {

public static void main(String[] args) {
String xmlContent = createXMLContent();
writeXMLToFile(xmlContent);
}

private static String createXMLContent() {
// write the first line
StringBuilder sb = new StringBuilder();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); sb.append("<poem title=\"");
sb.append(Poem.getTitle());
sb.append("\" author=\"");
sb.append(Poem.getAuthor());
sb.append("\">\n");
// write the middle lines
for (String lineIn : Poem.getLines()) {
sb.append("<line>");
sb.append(lineIn);
sb.append("</line>\n");
}
// write the last line
sb.append("</poem>");
return sb.toString();
}

private static void writeXMLToFile(String xmlContent) {
String fileName = "C:" + File.separator + "test"
+ File.separator + "domoutput.xml";
try {
File domOutput = new File(fileName);
FileOutputStream domOutputStream
= new FileOutputStream(domOutput, true);
domOutputStream.write(xmlContent.getBytes());
domOutputStream.close();
System.out.println(fileName + " was successfully created");
} catch (FileNotFoundException fnfe) {
System.out.println("Couldn't find a file called " + fileName);
Free download pdf