• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

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

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 9次 | 进入软件测试论坛讨论

领测软件测试网
需要一些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();
        }
    }
}

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网