`
yshlin
  • 浏览: 63484 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Read And Edit Xml File

    博客分类:
  • xml
阅读更多
public class ReadAndEditXmlFile {
	public static String queryXmlElementValue(String path, String name){
		String res = "";
		try {
			String filepath = path;
			File f = new File(filepath);
			SAXReader saxReader = new SAXReader(); 
			Document document = saxReader.read(f);
			Element root = document.getRootElement();
			Iterator itElem = root.elementIterator();
			while(itElem.hasNext()){
				Element sonElem = (Element) itElem.next();
				if (sonElem.getName().equals(name)) {
					res = sonElem.getText();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return res;
	}
	public static boolean editXmlElementValue(String path, String name, String value){
		boolean b = false;
		try {
			String filepath = path;
			File f = new File(filepath); 
			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(f);
			Writer w = new FileWriter(f);
			OutputFormat opf = OutputFormat.createPrettyPrint();
			opf.setEncoding("UTF-8");
	        XMLWriter xw = new XMLWriter(w, opf);
			Element root = document.getRootElement();
			Iterator itElem = root.elementIterator();
			while(itElem.hasNext()){
				Element sonElem = (Element) itElem.next();
				if (sonElem.getName().equals(name)) {
					sonElem.setText(value);
				}
			}
			xw.write(document);   
	        xw.close();
	        return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return b;
	}
}

 

分享到:
评论

相关推荐

    UE(官方下载)

    Steps to record and edit powerful macros to quickly and efficiently edit files Using "copied" and "selected" variables for dynamic macros Use copied and selected text in macros to dramatically ...

    Firebird Maestro 17.1 带破解

    With Firebird Maestro you can view and edit table data (including BLOB fields), export and import data to/from MS Excel, MS Access, DBF, XML, TXT, CSV and other formats, get SQL data dump and more....

    Bayesian Network(贝叶斯网络) Python Program

    The SIGMAEditor program can read this type of file. Here are some limitations of the toolkit as of 23 February 2009: 1. Users cannot yet edit Bayes nets directly in the SIGMAEditor. Code has to be ...

    eac3to V3.17

    * fixed: source file detection read 300MB from every source file v3.06 * added MKV reading/parsing support * added demux support for MKV (E-)AC3, DTS(-HD), AAC, MPx, FLAC and WAV tracks * added demux...

    jdk-9.0.1_doc-all 最新版

    Defines tools for transforming a JAR file into a compressed pack200 file and transforming a packed file into a JAR file, including the pack200 and unpack200 tools. jdk.packager.services Defines the ...

    apktool documentation

    apks are nothing more than a zip file containing resources and compiled java. If you were to simply unzip an apk like so, you would be left with files such as classes.dex and resources.arsc. $ unzip...

    完整R语言实战前三章图形初阶运行代码.docx

    xmlData ("myxmlfile.xml")`。 - **网页数据**:`readLines()`下载网页内容,`grep()`和`gsub()`进行文本搜索和替换。`RCurl`和`XML`包可以用于更复杂的网页抓取。 - **其他数据格式**:`foreign`包支持SPSS(`...

    实例解析PHP结合AJAX开发

    AJAX(Asynchronous JavaScript and XML)是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。它并非一种全新的技术,而是利用JavaScript、XML、CSS和DOM等多种现有技术的组合创新。AJAX的核心是...

    cuteEditor6.0

    The default mappings: <br/>admin设置 admin.config default设置 default.config guest设置 guest.config You can customize and extend each policy file by editing the XML security ...

    OutlookAttachView v2.73

    * NK2Edit - Edit, merge and fix the AutoComplete files (.NK2) of Microsoft Outlook. Description =========== OutlookAttachView scans all messages stored in your Outlook, and displays the list of ...

    vs 2005 10款插件

    Visual Studio 2005 includes a number of debugger visualizers by default, most notably the DataSet visualizer, which provides a tabular interface to view and edit the data inside a DataSet. While the ...

    VB编程资源大全(英文源码 控件)

    xml2word.zip A ActiveX-dll for a mailmerge system based on an XML-file and Word 97. <END><br>42,ToolbarSrc.zip This is a complete commericial quality toolbar control for use with VB6 (VB5 users ...

    WPTools.v6.29.1.Pro

    * IMPORTANT: Please edit file WPINC.INC to * activate WPREPORTER, WPSPELL, wPDF, TBX * and GraphicEx / PNGImage support! * Delphi 2009 and later as inbuilt PNGImage *********************************...

    gerrit-3.0.3.war

    SVG is added as an alias to xml.js and highlighted as XML. JavaScript supports big number syntax now. cpp syntax highlighting improved. JSON gains support for highlighting comments. C++ gains more...

    完整R语言实战前三章图形初阶运行代码.pdf

    mydata <- read.table(file="data.txt", header=TRUE, sep="\t") ``` 对于Excel文件,可以使用`xlsx`包的`read.xlsx()`函数。XML数据的导入需要`XML`包的支持。从网页抓取数据可能涉及`readLines()`, `grep()`, `...

    Delphi7.1 Update

    and supersedes information contained in otherdocuments, including previously installed release notes.Borland recommends that you read this file in its entirety.NOTE: If you are updating a localized ...

    VB编程资源大全(英文源码 其它)

    6 , chunks.zip This will open a file and read it in "Chunks" of a selected file.<END><br>7 , logging.zip This is a bas that will log installation procedures so the file can be removed later....

    android 中的数据存储

    Log.d("FileRead", line); } } catch (IOException e) { e.printStackTrace(); } ``` 3. **SQLite数据库** SQLite是一个嵌入式的关系型数据库,适用于存储大量结构化的数据。在Android中,可以通过...

    EurekaLog_7.5.0.0_Enterprise

    4)....Added "--el_injectjcl", "--el_createjcl", and "--el_createdbg" command-line options for ecc32/emake to inject JEDI/JCL debug info, create .jdbg file, and create .dbg file (Microsoft debug format...

Global site tag (gtag.js) - Google Analytics