一个可以完成读取、打印输出、保存xml等等功能的java例子
2010-12-11 15:19
543 查看
/*
* Created by IntelliJ IDEA.
* User: administrator
* Date: Mar 26, 2002
* Time: 1:24:56 PM
* To change template for new class use
* Code Style | Class Templates options (Tools | IDE Options).
*/
/*****readXml.java**********************
*This is a javabean.
*This bean read a xml file from a URL,and return a xmlDom
*
***** Created by Xiao Yusong2001-11-30 ****
*/
package com.chinacountry.util;
import java.util.*;
import java.net.URL;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.SerializerFactory;
import org.apache.xml.serialize.XMLSerializer;
import org.xml.sax.InputSource;
public class xmlUtil implements java.io.Serializable {
public xmlUtil()
{
}
public static DocumentBuilder getBuilder()
throws ParserConfigurationException
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
return builder;
}
// get a document from given file
public static Document getDocument(String path) throws Exception
{
// BufferedReader fileIn=new BufferedReader(new FileReader(path));
File f = new File(path);
DocumentBuilder builder = getBuilder();
Document doc = builder.parse(f);
return doc;
}
// get a document from InputStream
public static Document getDocument(InputStream in) throws Exception
{
DocumentBuilder builder = getBuilder();
Document doc = builder.parse(in);
return doc;
}
// create a empty document
public static Document getNewDoc() throws Exception
{
DocumentBuilder builder = getBuilder();
Document doc = builder.newDocument();
return doc;
}
// create a document from given string
public static Document getNewDoc(String xmlStr)
{
Document doc = null;
try
{
StringReader sr = new StringReader(xmlStr);
InputSource iSrc = new InputSource(sr);
DocumentBuilder builder = getBuilder();
doc = builder.parse(iSrc);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return doc;
}
// save a document as a file at the given file path
public static void save(Document doc, String filePath)
{
try
{
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize(doc.getDocumentElement());
String STRXML = stringOut.toString(); // Spit out DOM as a String
String path = filePath;
writeXml(STRXML, path);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// save a string(xml) in the given file path
public static void writeXml(String STRXML, String path)
{
try
{
File f = new File(path);
PrintWriter out = new PrintWriter(new FileWriter(f));
out.print(STRXML + "/n");
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// format a document to string
public static String toString(Document doc)
{
String STRXML = null;
try
{
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize(doc.getDocumentElement());
STRXML = stringOut.toString(); // Spit out DOM as a String
}
catch (Exception e)
{
e.printStackTrace();
}
return STRXML;
}
// format a node to string
public static String toString(Node node, Document doc)
{
String STRXML = null;
try
{
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize((Element) node);
STRXML = stringOut.toString(); // Spit out DOM as a String
}
catch (Exception e)
{
e.printStackTrace();
}
return STRXML;
}
public static void main(String[] args) throws Exception
{
String pathRoot = "NeTrees.xml";
Document doc, doc1;
try
{
doc = xmlUtil.getDocument(pathRoot);
doc1 = xmlUtil.getDocument(pathRoot);
if (doc == doc1)
{
System.out.println("They aresame objects!");
}
else
{
System.out.println("They are different!");
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize(doc.getDocumentElement());
String STRXML = stringOut.toString(); // Spit out DOM as a
// String
System.out.println(STRXML);
}
}
catch (Exception ex)
{
System.out.print("Reading file/"" + pathRoot + "/" error!");
ex.printStackTrace();
}
}
}
* Created by IntelliJ IDEA.
* User: administrator
* Date: Mar 26, 2002
* Time: 1:24:56 PM
* To change template for new class use
* Code Style | Class Templates options (Tools | IDE Options).
*/
/*****readXml.java**********************
*This is a javabean.
*This bean read a xml file from a URL,and return a xmlDom
*
***** Created by Xiao Yusong2001-11-30 ****
*/
package com.chinacountry.util;
import java.util.*;
import java.net.URL;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml.serialize.SerializerFactory;
import org.apache.xml.serialize.XMLSerializer;
import org.xml.sax.InputSource;
public class xmlUtil implements java.io.Serializable {
public xmlUtil()
{
}
public static DocumentBuilder getBuilder()
throws ParserConfigurationException
{
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
return builder;
}
// get a document from given file
public static Document getDocument(String path) throws Exception
{
// BufferedReader fileIn=new BufferedReader(new FileReader(path));
File f = new File(path);
DocumentBuilder builder = getBuilder();
Document doc = builder.parse(f);
return doc;
}
// get a document from InputStream
public static Document getDocument(InputStream in) throws Exception
{
DocumentBuilder builder = getBuilder();
Document doc = builder.parse(in);
return doc;
}
// create a empty document
public static Document getNewDoc() throws Exception
{
DocumentBuilder builder = getBuilder();
Document doc = builder.newDocument();
return doc;
}
// create a document from given string
public static Document getNewDoc(String xmlStr)
{
Document doc = null;
try
{
StringReader sr = new StringReader(xmlStr);
InputSource iSrc = new InputSource(sr);
DocumentBuilder builder = getBuilder();
doc = builder.parse(iSrc);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return doc;
}
// save a document as a file at the given file path
public static void save(Document doc, String filePath)
{
try
{
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize(doc.getDocumentElement());
String STRXML = stringOut.toString(); // Spit out DOM as a String
String path = filePath;
writeXml(STRXML, path);
}
catch (Exception e)
{
e.printStackTrace();
}
}
// save a string(xml) in the given file path
public static void writeXml(String STRXML, String path)
{
try
{
File f = new File(path);
PrintWriter out = new PrintWriter(new FileWriter(f));
out.print(STRXML + "/n");
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
// format a document to string
public static String toString(Document doc)
{
String STRXML = null;
try
{
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize(doc.getDocumentElement());
STRXML = stringOut.toString(); // Spit out DOM as a String
}
catch (Exception e)
{
e.printStackTrace();
}
return STRXML;
}
// format a node to string
public static String toString(Node node, Document doc)
{
String STRXML = null;
try
{
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize((Element) node);
STRXML = stringOut.toString(); // Spit out DOM as a String
}
catch (Exception e)
{
e.printStackTrace();
}
return STRXML;
}
public static void main(String[] args) throws Exception
{
String pathRoot = "NeTrees.xml";
Document doc, doc1;
try
{
doc = xmlUtil.getDocument(pathRoot);
doc1 = xmlUtil.getDocument(pathRoot);
if (doc == doc1)
{
System.out.println("They aresame objects!");
}
else
{
System.out.println("They are different!");
OutputFormat format = new OutputFormat(doc);// Serialize DOM
format.setEncoding("GB2312");
StringWriter stringOut = new StringWriter();// Writer will be a
// String
XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();// As a DOM Serializer
serial.serialize(doc.getDocumentElement());
String STRXML = stringOut.toString(); // Spit out DOM as a
// String
System.out.println(STRXML);
}
}
catch (Exception ex)
{
System.out.print("Reading file/"" + pathRoot + "/" error!");
ex.printStackTrace();
}
}
}
相关文章推荐
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- php输出标准xml,php访问mysql数据库简单例子(可以被asp等程序读取)
- 使用java.nio及其子包中的类SocketChanel完成如下功能:读取www.baidu.com等网页的内容,打印到控制台,避免中文乱码问题。
- 一个java读取word文档并将其写入xml文档的例子
- XMLReader—一个可以读取XML文件的java类
- XML中对于一个books.xml的详情显示,删除按钮,修改并保存按钮 和 添加按钮。完成这些按钮所对应的功能(XmlDocument)。
- 写了一个将VxWorks的shell打印输出到指定内存中的接口,可广泛用于CLI下的调试函数显示以及故障自动捕获等功能,稍作修改可以适合其它嵌入式OS
- 【Log】一个功能强大的Log封装库包括控制日志输出,保存Log到文件,过滤输出等级。。
- 编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt
- 使用JSP + JAVABEAN + XML 开发的一个例子