<%@ page contentType="text/html; charset=gb2312"%> 一个可以完成读取、打印输出、保存xml等等功能的java例子(xml新手不可不看呀!)
网站公告:   ◆北天JAVA技术网热情为java爱好者服务,本网内容包括JAVA(JSP、servlet、EJB、webservice、j2ee、javabean、应用服务器、JavaScript),数据库(MYSQL、SQL Server、Sybase、Oracle、DB2、数据库综合知识),设计研究(设计模式、Struts、Spring、Hibernate、设计框架、设计综合知识),WEB2.0新技术(主要介绍AJAX),以及各种技术的入门、实例、例子等等,欢迎各位多来坐坐!◆  诚邀各位JAVA爱好者加盟!◆  本网站内容丰富,更新快,保证每周20篇以上!  
加入收藏
设为首页
联系站长
承接项目
  相关资源:网站首页 | 免费培训学院 | 技术论坛 | JAVA聊天室 | 作家专栏 | 开发工具 | 认证考试 | 会员俱乐部
  JAVA技术初学者园地 | jsp与servlet | javascript | Java源代码 | EJB | web service | 应用服务器 | JAVA综合知识
  设计研究设计模式 | 设计框架 | Struts | Spring | Hibernate | 开源项目 | 面向对象设计 | 设计综合知识
  数 据 库MYSQL | SQL Server | Sybase | Oracle | DB2 | Informix | Access | 数据库综合知识
  其他资源:AJAX新技术 | 网站开发 | ERP软件 | OA办公软件 | 商业智能BI | 开发综合知识 | 承接项目 | 项目试用

 
 
一个可以完成读取、打印输出、保存xml等等功能的java例子(xml新手不可不看呀!)
     发布者: 发布时间:2006-07-25
需要一些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 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 are  same 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();
        }
    }
}
(转载文章请保留出处:北天JAVA技术网(www.java114.com))
 
更多精彩文章:
一个jdbc的例子(包含sql语句的批处理,事务处理,数据绑定prepare,)
Java 程序的测试工具
JSP定位特定的日期
在Linux上构建Jsp环境
一个用JAVA写的测算服务器响应速度的程序
设计模式之Command
 
最近评论:
        
回复:一个可以完成读取、打印输出、保存xml等等功能的java例子(xml新手不可不看呀!)
到底是哪些JAR 啊
        
标 题:   
内 容:   
 
                                  
 
免责声明:该文章由网友发表,如果对您造成侵权,请联系站长

首页 - 承接项目 - 网站地图 - 联系我们 -
版权所有北天JAVA技术工作室 ICP证号:粤ICP备06079815号