A. http://www.java2s.com/Code/Java/XML/NewDocumentFromInputStream.htm
New Document from InputStream
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.StringReader;
Import Java.io.StringWriter;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.Iterator;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import javax.xml.parsers.ParserConfigurationException;
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;
Import Org.w3c.dom.Node;
Import org.w3c.dom.NodeList;
Import Org.xml.sax.InputSource;
Import org.xml.sax.SAXException;
/******************************************************************************* * Copyright (C) 2007 Google Inc. * Licensed under the Apache License, Version 2.0 (the "License")); The May isn't * use this file except in compliance with the License. You may obtain a copy of * the License in * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by app
Licable or agreed to in writing, software * Distributed under the License be distributed on ' as is ' basis, without * Warranties or CONDITIONS of any KIND, either express OR implied.
The * License for the specific language governing permissions and limitations under.
//** * Various XML utilities. * * @author simonjsmith, Ksim * @version 1.1-ksim-march 6th, 2007-added functions regarding streaming * @version 1.2-ksim-march 10th, 2007-added functions regarding DOM * manipulation/public class Utils {public
Static Document Newdocumentfrominputstream (InputStream in) {documentbuilderfactory factory = null;
Documentbuilder builder = null; Document RET = null;
try {factory = Documentbuilderfactory.newinstance ();
Builder = Factory.newdocumentbuilder ();
catch (Parserconfigurationexception e) {e.printstacktrace ();
try {ret = Builder.parse (new InputSource (in));
catch (Saxexception e) {e.printstacktrace ();
catch (IOException e) {e.printstacktrace ();
return ret;
}
}
B. http://www.zuidaima.com/share/1758442014903296.htm
Java reads and writes file operation instance code through InputStream
1. File to InputStream
1 |
File File = new file ("File.xml"); |
2 |
InputStream InputStream = new FileInputStream (file); |
2.InputStream to File
01 |
InputStream InputStream = new FileInputStream ("File.xml"); |
03 |
OutputStream outputstream = new FileOutputStream ("File-new.xml"); |