jdom的使用
来源:互联网 发布:实况足球8球员数据库 编辑:程序博客网 时间:2024/05/17 01:45
多次使用jdom包,整理了一下,我用的比较简单,只是对xml文件进行读节点和写节点简单操作!
package servletTest;
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;
public class XmlTest {
/**
* @param args
* @throws IOException
* @throws JDOMException
* @author ****
* @date 2007-08-14
*/
public static void main(String[] args ){
// XmlTest.loadXml("D:/Web.net.xml");//path no question
XmlTest.writeXml("D:/temp/Web.net.xml");
}
public static String[] loadXml(String filepath){
try {
java.io.FileInputStream xmlFile = new java.io.FileInputStream(filepath);
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(xmlFile);
Element config = doc.getRootElement();
List application = (List) config.getChildren("appSettings");
Element e1 = (Element) application.get(0);
String[] array =new String[4];
array[0] = e1.getChildText("cachePath");
array[1] = e1.getChildText("LZD");
array[2] = e1.getChildText("wmsURL");
array[3] = e1.getChildText("param");
// System.out.println(e1.getName());
System.out.println("pararse web.net.xml successfully ! ");
return array;
} catch (Exception e) {
e.printStackTrace();//
return null;
}
}
public static boolean writeXml(String filepath){
try {
org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
java.io.FileInputStream fos = new java.io.FileInputStream(filepath);
org.jdom.Document doc = builder.build(fos);
org.jdom.Element root = doc.getRootElement();
Element body = new Element("body");
Element table = new Element("table");
Element row = new Element("row");
Element column = new Element("column");
Element script = new Element("script");
table.setText("table id");
row.setText("row id");
column.setText("column id");
script.setText("script code");
body.addContent(table);
body.addContent(row);
body.addContent(column);
body.addContent(script);
root.addContent(body);
java.io.FileOutputStream xmlOut = new java.io.FileOutputStream(filepath);
org.jdom.output.XMLOutputter out = new org.jdom.output.XMLOutputter();
out.output(doc, xmlOut);
out.output(doc, System.out);
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
}
按照上面操作可对下面文件读写操作:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<cachePath>D:/temp/</cachePath>
<LZD>1.125</LZD>
<wmsURL>
<![CDATA[http://192.168.0.4:8080/geoserver/wms?FORMAT=image/gif&TRANSPARENT=TRUE&HEIGHT=406&REQUEST=GetMap&WIDTH=810&STYLES=&SRS=EPSG:4326&VERSION=1.1.1]]>
</wmsURL>
<param>LAYERS</param>
</appSettings>
<body>
<table>table id</table>
<row>row id</row>
<column>column id</column>
<script>script code</script>
</body>
<body>
<table>table id</table>
<row>row id</row>
<column>column id</column>
<script>script code</script>
</body>
</configuration>
- jdom的使用
- jdom的使用(3)
- jdom的使用
- jdom的使用
- jdom的使用
- jdom的使用
- JDOM的使用
- jdom工具的使用
- JDOM的基本使用
- Jdom的安装和使用
- 使用JDOM处理XML文档的技巧
- java图片处理和jdom的使用
- JDom 常用api 及 一般的使用
- 使用Jdom进行简单的XML解析
- 使用Java的JDOM解析xml代码
- JDOM入门:使用JDOM解析XML
- JDOM入门:使用JDOM解析XML
- JDOM入门:使用JDOM解析XML
- 不是框架页,模仿框架页的左右结构
- 帮忙看看
- 技术资料 网址
- WindowsServer2003 + IIS6.0 + ASP 服务器安全设置之 -- 硬盘权限篇
- java和c#企业开发
- jdom的使用
- 正则表达式30分钟入门教程
- eclipse 设置提示信息
- 在PHP中使用与Perl兼容的正则表达式
- ASP.NET虚拟主机安全漏洞解决方案
- asp.net中生成缩略图
- CSS制作虚线效果的水平线(转贴)
- 在.NET2.0里面如何发送邮件
- 网页中的一些禁止方法