I am working on the following code:
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
public class CreatXMLFile {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter number to add elements in your XML file: ");
// String str = bf.readLine();
int no=2;
// System.out.print("Enter root: ");
String root = "SMS";
DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement(root);
document.appendChild(rootElement);
// for (int i = 1; i <= no; i++)
// System.out.print("Enter the element: ");
// String element = bf.readLine();
String element ="Number";
System.out.print("Enter the Number: ");
String data = bf.readLine();
Element em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em);
String element1 ="message";
System.out.print("Enter the SMS: ");
String data1 = bf.readLine();
Element em1 = document.createElement(element1);
em1.appendChild(document.createTextNode(data1));
rootElement.appendChild(em1);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
开发者_StackOverflow社区 StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
}
And it gives the following output:
run:
Enter the Number: 768678
Enter the SMS: ytu
<?xml version="1.0" encoding="UTF-8" standalone="no"?><SMS><Number>768678</Number><message>ytu</message></SMS>BUILD SUCCESSFUL (total time: 8 seconds)
Now I want to write the generated output (<?xml version="1.0" encoding="UTF-8" standalone="no"?><SMS><Number>768678</Number><message>ytu</message></SMS>
) to a XML file on the hard disk. How do I do it?
Use a FileOutputStream
(or a File
) instead of System.out
to construct your StreamResult
.
Here is the complete solution for this question :
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class CreatXml {
/**
* @param args
* @throws TransformerException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws TransformerException, ParserConfigurationException, IOException {
// TODO Auto-generated method stub
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// System.out.print("Enter number to add elements in your XML file: ");
// String str = bf.readLine();
int no=2;
// System.out.print("Enter root: ");
String root = "SMS";
DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement(root);
document.appendChild(rootElement);
// for (int i = 1; i <= no; i++)
// System.out.print("Enter the element: ");
// String element = bf.readLine();
String element ="Number";
System.out.print("Enter the Number: ");
String data = bf.readLine();
Element em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em);
String element1 ="message";
System.out.print("Enter the SMS: ");
String data1 = bf.readLine();
Element em1 = document.createElement(element1);
em1.appendChild(document.createTextNode(data1));
rootElement.appendChild(em1);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
//t.setParameter(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
transformer.transform(source, result);
//writing to file
FileOutputStream fop = null;
File file;
try {
file = new File("/home/zurelsoft/Desktop/newfile1.txt");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// get the content in bytes
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
byte[] contentInBytes = xmlString.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class xmlWriter {
public static void main(String[] args) throws TransformerException, ParserConfigurationException, IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int no=2;
String root = "SMS";
DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement(root);
document.appendChild(rootElement);
String element ="Number";
System.out.print("Enter the Number: ");
String data = bf.readLine();
Element em = document.createElement(element);
em.appendChild(document.createTextNode(data));
rootElement.appendChild(em);
String element1 ="message";
System.out.print("Enter the SMS: ");
String data1 = bf.readLine();
Element em1 = document.createElement(element1);
em1.appendChild(document.createTextNode(data1));
rootElement.appendChild(em1);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
//writing to file
FileOutputStream fop = null;
File file;
try {
file = new File("newfile1.xml");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
else{
}
// get the content in bytes
String xmlString = result.getWriter().toString();
System.out.println(xmlString);
byte[] contentInBytes = xmlString.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
精彩评论