JDOM对xml文件的读写

1. 读取例子

 

  xml文件代码

<?xml version="1.0" encoding="UTF-8"?>
<sys-configure>
	<jdbc-info>
		<dirver-class-name>oracle.jdbc.driver.OracleDriver</dirver-class-name>
		<url>jdbc:oracle:thin:@127.0.0.1:1521:sxt</url>
		<user-name>username</user-name>
		<password>password</password>
	</jdbc-info>
	<beans>
		<bean id = "id1" class = "class1"/>
		<bean id = "id2" class = "class2"/>
	</beans>
</sys-configure>

 jdom 读取代码

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Text;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;


public class XMLReader {
	//单例
	private static XMLReader  instance = new XMLReader();
	
	private static final String CONFIG_NAME = "src/test/jdom/config.xml";
	
	private Element elt;
	
	private XMLReader(){
		SAXBuilder sb = new SAXBuilder();
		try {
//			先获取文件对象
			Document doc = sb.build(new File(CONFIG_NAME));
			this.elt = doc.getRootElement();
			initJdbc();
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static XMLReader getInstance(){
		return instance;
	}
	
	private  void initJdbc(){
		try {
			Element element = (Element)XPath.selectSingleNode(elt, "//sys-configure/jdbc-info/dirver-class-name");
			System.out.println(element.getText());
		} catch (JDOMException e) {
			e.printStackTrace();
		}
		
	}
	public static void main(String [] args){
		XMLReader.getInstance();
		SAXBuilder builder = new SAXBuilder();
        try {
			Document doc = builder.build(new File(CONFIG_NAME));
			XPath rolePath = XPath.newInstance("//jdbc-info/password/text()");
			List list = rolePath.selectNodes(doc);
			Iterator iter = list.iterator();
			while(iter.hasNext()){
				System.out.println((Text)iter.next());
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

2. jdom写xml文件的例子

 

 代码如下

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;

/**
 * 	<selects>
 * 		<select>
 * 			<id>1</id>
 * 			<name>吉林</name>
 * 		</select>
 *  </selects> 
 *
 */
public class XMLWriter {
	
	public static void main(String [] args){
		//生成元素
		Element rootElt = new Element("selects");
		Element selcetElt = new Element("select");
		Element idElt = new Element("id");
		idElt.setText("1");
		
		Element nameElt = new Element("name");
		nameElt.setText("吉林省");
		
		//添加关系
		selcetElt.addContent(idElt);
		selcetElt.addContent(nameElt);
		
		rootElt.addContent(selcetElt);
		
		//输出xml
		
		Document document = new Document(rootElt);
		XMLOutputter out = new  XMLOutputter();
		//默认的encoding="UTF-8",可以修改如下
		//out.setFormat(Format.getCompactFormat().setEncoding("gbk"));
		
		System.out.println(out.outputString(document));
		
		try {
			//写到指定的文件
			out.output(document, new FileOutputStream("c:\\xml.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

 

你可能感兴趣的:(java,oracle,xml,bean,jdbc)