JAVA读取XML所有内容

所需jar包:

  dom4j-1.6.1.jar

  xercesImpl.jar

package com.word;
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

/**
 * 读取xml 所有内容
 * @author Administrator
 *
 */
public class XmlRead {

    static StringBuffer sBuilder = new StringBuffer();

    public static void main(String[] args) throws IOException {
        XmlRead  xml = new XmlRead();
        String path = "d:\temp\pzhd1389682365500.xml" ;
         String xmlstr = xml.xmlToStr(path);
         System.out.println("-----------end :");
         System.out.println(xmlstr);
    }

    /**
     * 读取xml文件格式+内容
     * @param filePath
     * @return
     */
    public String xmlToStr(String filePath){
        
      
        String path  = filePath;

        Document document = null;
        try {
            
            document = read(path);
            
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        Element root = getRootElement(document);
        if (root == null) {
            System.out.print("没有获取到root节点");
            return null;
        }
        // 获取XML文档的编码格式
        String encString = document.getXMLEncoding();
        sBuilder.append("<?xml version="1.0" encoding="" + encString
                + "">
");
         sBuilder.append(elementText(root, attText(root), 0));
          
         String ss = getIterator(root, 0) + "</" + root.getName() + ">";
         sBuilder.append(ss);
            
        return sBuilder.toString();
    }
    
    
    /**
     * 递归节点
     * 
     * @description
     * @param element
     * @param lvl
     *            层级
     * @return
     */
    private static String getIterator(Element element, int lvl) {

        lvl += 1;
      //  StringBuffer sBuilder = new StringBuffer();
        for (Iterator i = element.elementIterator(); i.hasNext();) {
            Element e = (Element) i.next();
            sBuilder.append(elementText(e, attText(e), lvl));
            getIterator(e, lvl);

            int count = e.nodeCount();

            if (count > 0) {
                for (int j = 0; j < lvl; j++) {
                    sBuilder.append("    ");
                }
            }
            sBuilder.append(element.getStringValue()); //文本值
            sBuilder.append("</" + e.getName() + ">
");
           // System.out.println("========================>"+element.getStringValue());
        }

        return sBuilder.toString();
    }

    /**
     * 获取当前节点的属性的值的字符串
     * 
     * @description
     * @param element
     *            当前节点
     * @return
     */
    private static String attText(Element element) {

        String str = " ";
        for (int i = 0; i < element.attributeCount(); i++) {
            Attribute attribute = element.attribute(i);

            str += attribute.getName() + "="" + attribute.getValue() + "" ";
           String ss =  attribute.getText();
        }
        return str;
    }

    /**
     * 获取当前Element的文本值
     * 
     * @description
     * @param element
     *            当前Element节点
     * @param text
     *            属性值
     * @param lvl
     *            层级
     * @return
     */
    private static String elementText(Element element, String text, int lvl) {
        String str = "";
        for (int i = 0; i < lvl; i++) {
            str += "    ";
        }
        str += "<" + element.getName();
        if (text != null && text != "") {
            str += text;
        }
        int count = element.nodeCount();
        if (count == 0) {
            return str += ">";
        }
        return str += ">
";
    }

    /**
     * 
     * @description 读取XML文件
     * @param file
     *            XML文件路径,包含文件名
     * @return Document 文档
     * @throws MalformedURLException
     * @throws DocumentException
     */
    public static Document read(String file) throws MalformedURLException,
            DocumentException {

        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(file));
        return document;
    }

    /**
     * 获取Document文档的root节点
     * 
     * @param document
     * @return
     */
    public static Element getRootElement(Document document) {
        return document.getRootElement();
    }

}
View Code
原文地址:https://www.cnblogs.com/summer520/p/3522251.html