当前位置:编程学习 > JSP >>

一个可以完成读取、打印输出、保存xml等等功能的java例子(xml新手不可不看呀!)

答案:需要一些jar,自己去down吧

/*
* 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 Yusong  2001-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 St

上一个:一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare,)
下一个:充分利用网上Java资源2(转贴)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,