要使用jdom解析xml文件,需要下载jdom的包,我使用的是jdom-1.1,附件中有。
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<sys-config>
<jdbc-info>
<driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name>
<url>jdbc:oracle:thin:@localhost:1521:database</url>
<user-name>why</user-name>
<password>why</password>
</jdbc-info>
<provinces-info>
<province id="hlj" name="黑龙江">
<city id="harb">哈尔滨</city>
<city id="nj">嫩江</city>
</province>
<province id="jl" name="吉林"></province>
</provinces-info>
</sys-config>
读xml文件:文章中采用用了Xpath定位,以前我不知道。
package com.why.jdom;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
public class ReadXML {
/**
* @param args
*/
public static void main(String[] args) {
SAXBuilder sax = new SAXBuilder();
try {
Document doc = sax.build("src/config.xml");
Element rootEle = doc.getRootElement();
Element driverClassNameElement = (Element)XPath.selectSingleNode(rootEle, "//sys-config/jdbc-info/driver-class-name");
String driverClassName = driverClassNameElement.getText();
System.out.println("driverClassName = " + driverClassName);
List provinceList = XPath.selectNodes(rootEle, "//sys-config/provinces-info/province");
for(Iterator it = provinceList.iterator();it.hasNext();){
Element provinceEle = (Element)it.next();
String proId = provinceEle.getAttributeValue("id");
String proName = provinceEle.getAttributeValue("name");
System.out.println("provinceId = " + proId + " provinceName = " + proName);
List cityEleList = (List)provinceEle.getChildren("city");
for(Iterator cityIt = cityEleList.iterator();cityIt.hasNext();){
Element cityEle = (Element)cityIt.next();
String cityId = cityEle.getAttributeValue("id");
String cityName = cityEle.getText();
System.out.println(" cityId = " + cityId + " cityName = " + cityName);
}
}
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
写xml文件:
package com.why.jdom;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class WriteXML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Element rootEle = new Element("sys-config");
Element provincesEle = new Element("provinces-info");
Element provinceEle = new Element("province");
provinceEle.setAttribute("id","hlj");
provinceEle.setAttribute("name","黑龙江省");
Element cityEle1 = new Element("city");
cityEle1.setAttribute("id","harb");
cityEle1.addContent("哈尔滨");
Element cityEle2 = new Element("city");
cityEle2.setAttribute("id","nj");
cityEle2.addContent("嫩江");
provinceEle.addContent(cityEle1);
provinceEle.addContent(cityEle2);
provincesEle.addContent(provinceEle);
rootEle.addContent(provincesEle);
Document doc = new Document(rootEle);
XMLOutputter out = new XMLOutputter();
// out.setFormat(Format.getCompactFormat().setEncoding("GBK"));//设置文件编码,默认为UTF-8
String xmlStr = out.outputString(doc);
System.out.println(xmlStr);
try {
out.output(doc, new FileOutputStream("c:/test.xml"));
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
标签:xml,jdom,Element,new,import,out,读写,provinceEle From: https://blog.51cto.com/u_16298170/7947928