您的位置:首页 > 编程语言 > Java开发

一个可以完成读取、打印输出、保存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();

  }

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐