java讀取XML檔案通用工具類(遞迴調用),xml工具類

來源:互聯網
上載者:User

java讀取XML檔案通用工具類(遞迴調用),xml工具類

原文:java讀取XML檔案通用工具類(遞迴調用) 原始碼:http://www.zuidaima.com/share/1550463285480448.htm

java實現讀取XML檔案並得到指定名稱節點下的所有常值內容,包括節點(逆歸)

package com.zuidaima.xml;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;/******************************************************************************* * xml通用工具類 * @author www.zuidaima.com */@SuppressWarnings("unchecked")public class XMLUtils{public static String xml = "";public static void init(){xml = "";}/*************************************************************************** * 得到指定名稱節點下的所有常值內容,包括節點(逆歸) <暫不考慮節點屬性情況> *  * @param doc *            xml文檔對象 * @param e *            要擷取的節點對象 * @param exceptTag *            要排除的節點名稱 * @return */public static String getChildAllText(Document doc, Element e){if (e != null){if (e.getChildren() != null){List<Element> list = e.getChildren();xml += "<" + e.getName() + ">";for (Element el : list){if(el.getChildren().size() > 0){getChildAllText(doc, el);}else{xml += "<" + el.getName() + ">" + el.getTextTrim() + "</"+ el.getName() + ">";}}xml += "</" + e.getName() + ">" ;}else{xml += "<" + e.getName() + ">" + e.getTextTrim() + "</"+ e.getName() + ">";}}return xml;}public static void main(String[] args) throws FileNotFoundException,JDOMException, IOException{ // 如果有任何異常則拋出SAXBuilder sb = new SAXBuilder(); // 建立立構造器Document doc = null;doc = sb.build(new FileInputStream("D:\\test.xml")); // 讀入6.xmlElement root = doc.getRootElement(); // 取得根節點// Element e = root.getChild("apptype1");// System.out.println(e);System.out.println(getChildAllText(doc, root));}}

 標籤: xml 讀取 工具 遞迴調用 java話題: 文本解析和檔案處理 指令碼和工具


java讀取xml

XStream stream = new XStream();
//<student> 根節點和類的對應關係,就是說看到student,就認為是Student這個類
stream.alias("student", Student.class);
//可以用xml檔案 filePath是xml檔案路徑
Student student = (Student) stream.fromXML(new FileInputStream(filePath));
//可以用字串(xml字串)xml是字串
Student student = (Student) stream.fromXML(xml);
這個樣檔案或字串就變成對象了,對象裡面定義的屬性和檔案裡面的一直就會自動複製
需要兩個jar包:xpp3_min-1.1.3.4.O.jar和xstream-1.3.1.jar
 
java中怎讀取xml中資料多節點的給我一個例子,

在我們的程式中,通常要有一些根據主機環境確定的變數.比如資料庫訪問使用者名稱和密碼,不同的主機可能設定不一樣.只要更改XML設定檔,就可以正常運行.

localhost

sqlname

username

password

上面這個myenv.xml設定檔一般是放在tomcat的WEB-INF/classes目錄下.

我們編製一個Java程式直接讀取,將dbhost dbuser dbpassword提取出來供其他程式訪問資料庫用.

目前使用SAX比較的多,與DOM主要區別是 SAX是一行一行讀取XML檔案進行分析,適合比較大檔案,DOM是一次性讀入記憶體,顯然不能對付大檔案.這裡我們使用SAX解析,由於SAX解析器不斷在發展,網上有不少文章是針對老版本的.假如你使用JDK1.4 ,可以參考 使用SAX處理XML文檔 一文.這裡的程式是根據其改進並且經過實踐調試得來的.

對上面myenv.xml讀取的Java程式:

import org.xml.sax.Attributes;

import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.SAXException;

import java.util.Properties;

//使用DefaultHandler的好處 是 不必陳列出所有方法,

public class ConfigParser extends DefaultHandler {

////定義一個Properties 用來存放 dbhost dbuser dbpassword的值

private Properties props;

private String currentSet;

private String currentName;

private StringBuffer currentValue = new StringBuffer();

//構建器初始化props

public ConfigParser() {

this.props = new Properties();

}

public Properties getProps() {

return this.props;

}

//定義開始解析元素的方法. 這裡是將中的名稱xxx提取出來.

public void startElement(String uri, String localName, String qName, Attributes attributes)

throws SAXException {

currentValue.delete(0, currentValue.length());

this.currentName =qName;

}

//這裡是將之間的值加入到currentValue

public void characters(char[] ch, int start, int length) throws SAXException {

currentValue.append(ch, start, length);

}

//在碰到結束後,將之前的名稱和值一一對應儲存在props中

public void endElemen......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.