JAVA 解析 XML
XML文件时很多项目经常使用的配置或者其他用途的文件格式,解析xml文件格式的开发包也很多,本文主要使用java自带的DOM解析包。本文以一个实际例子来讲述解析xml文件的过程。
目标:解析这样一个XML文件
employees.xml
=======内容开始========
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<Name>Seagull</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Robin</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Crow</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
=======内容结束========
目标是:输出如下内容
Employee Details - Name:Seagull, Type:permanent, Id:3674, Age:34.
Employee Details - Name:Robin, Type:contract, Id:3675, Age:25.
Employee Details - Name:Crow, Type:permanent, Id:3676, Age:28.
主要步骤
1,
Get a document builder using document builder factory and parse the xml
file to create a DOM object
Get a list of employee elements from the DOM
For each employee element get the id, name, age and type. Create an
employee value object and add it to the list.
At the end iterate through the list and print the employees to verify we
parsed it right.
a) 获取document builder
public void parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBu
相关文档:
字符集与编码方法
字符集
字符编码
对应语言
ASCII
ASCII
英语
ISO8859-1
ISO8859-1
拉丁字母
GB2312
GB2312
简体中文
GBK
GBK
中文
GB18030
GB18030
简体中文
Big5
Big5
繁体中文
Unicode
UTF-8
多国语言
&nbs ......
相对路径的获得
说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目)
String
relativelyPath=System.getProperty("user.dir");
上述相对路径中,java项目中的文件是相对于项目的根目录
web项目中的文件路径视不同的web服务器不同而不同(tomcat是 ......
26.移动一个文件夹下所有文件到另一个目录
//import java.io.*;
File movefile=new File(%%1);
File[] movefiles=movefile.listFiles();
for(int i=0;i<movefiles.length;i++){
if(movefiles[i].isFile()){
int bytesum = 0;
int byteread  ......
34.读取ini文件属性
//import java.io.*;
//import java.util.*;
//import java.util.regex.*;
//private HashMap configMap=null;
private Map<String, Serializable> configMap=null;
String %%2=null;
if (configMap == null)&n ......