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>

 

原创粉丝点击