首页 > 其他分享 >jdom读写xml

jdom读写xml

时间:2023-10-20 11:01:13浏览次数:26  
标签:xml jdom Element new import out 读写 provinceEle


要使用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

相关文章

  • Web前端使用 $.ajax 和 XMLHttpRequest 请求的区别
    1、首先是我使用XMLHttpRequest进行后台请求,我在请求前。会把按钮置为灰色。 $('button[type="generateProductBop"]').css("background-color","#d4d4d4");在最后的finally才把按钮,重新置换回来原来的颜色$('button[type="generateProductBop"]').css(......
  • pom.xml常用配置(六)
    SpringCloudSpringCloudDependencies<properties><spring-cloud.version>Hoxton.SR3</spring-cloud.version></properties><dependencyManagement><dependencies><!--添加springcloud家族依赖-->&......
  • xStream完美转换XML、JSON
    xStream框架xStream可以轻易的将Java对象和xml文档相互转换,而且可以修改某个特定的属性和节点名称,而且也支持json的转换;前面有介绍过json-lib这个框架,在线博文:以及Jackson这个框架,在线博文:它们都完美支持JSON,但是对xml的支持还不是很好。一定程度上限制了对Java对象的描述,不能让xml......
  • Eclipse plugin.xml简写command
    <?xmlversion="1.0"encoding="UTF-8"?><?eclipseversion="3.4"?><plugin><extensionpoint="org.eclipse.ui.commands"><commandname="车间质量问责分析处理报告"id="com.xpm.plm.h......
  • [spring-mvc.xml] cannot be opened because it does not exist
    IOExceptionparsingXMLdocumentfromclasspathresource[spring-mvc.xml];nestedexceptionisjava.io.FileNotFoundException:classpathresource[spring-mvc.xml]cannotbeopenedbecauseitdoesnotexist检查pom.xml文件:<packaging>war</packagin......
  • C语言 mmap完成文件读写
    #include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/mman.h>#include<fcntl.h>#include<unistd.h>intmain(){//打开文件进行读写intfd=open("test.log",O_RDWR|O_CREAT,0600);......
  • pom.xml配置文件(五)
    SPRINGBOOT相关JUnitstarter<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion>......
  • [IDEA]查看一个类是在pom.xml的哪个dependency中被引入的
    ......
  • C#上位机序列9: 批量读写+事件广播
    1.读取配置文件及创建变量信息(点位名称,地址,数据类型(bool/short/int/float/long/double))2.读任务&写任务,数据有变化时事件广播通知usingHslCommunication;usingHslCommunication.Core;usingHslCommunication.ModBus;usingPLCEvent.Util;usingSystem;usingSystem.......
  • IO流,通过字节缓冲流来提高读写效率
    BufferedInputStream和BufferedOutputStream  两个流是缓冲字节流,通过内部缓存数组来提高操作流的效率。 当我们开启了很多流时,关闭顺序为:先开的后关闭(后开的先关闭)  在这个缓冲区中,byte数组的默认长度为8192,也是2的整数幂   练习代码如下: 结果是在指定文......