Jdom 读取 xml 文件例子
Jdom 读取 xml 文件例子
<!-- [if !supportLists]-->1.????????? <!-- [endif]-->用 JDOM 读取 XML 文件需先用 org.jdom.input.SAXBuilder 对象的 build() 方法创建 Document 对象 , 然后用 Document 类、 Element 类等的方法读取
?
Sample1.java
?
?
?
?
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Sample1 {
? public static void main(String[] args) throws Exception{
??? SAXBuilder sb= new SAXBuilder();
??? Document doc =sb.build(Sample1.class.getClassLoader().getResource("test.xml")); //构造文档对象 象
??? Element root=doc .getRootElement(); // 获取根元素
??? List list=root.getChildren( "disk" ); // 取名字为 disk 的所有元素
??? for ( int i=0;i<list.size();i++){
?????? Element element=(Element)list.get(i);
?????? String name=element.getAttributeValue( "name" );
?????? String capacity=element.getChildText( "capacity" ); // 取 disk 子元素 capacity 的内容
?????? String directories=element.getChildText( "directories" );
?????? String files =element.getChildText( "files" );
?????? System. out .println( " 磁盘信息 :" );
?????? System. out .println( " 分区盘符 :" +name);
?????? System. out .println( " 分区容量 :" +capacity);
?????? System. out .println( " 目录数 :" +directories);
?????? System. out .println( " 文件数 :" +files);
?????? System. out .println( "-----------------------------------" );
??? }?
? }
}
?
?
?
test.xml
?
?
?
<? xml version = "1.0" encoding = "UTF-8" ?>
< HD >
? < disk name = "C" >
??? < capacity > 8G </ capacity >
??? < directories > 200 </ directories >
??? < files > 1580 </ files >
? </ disk >
?
? < disk name = "D" >
??? < capacity > 10G </ capacity >
??? < directories > 500 </ directories >
??? < files > 3000 </ files >
? </ disk >
</ HD >
?
<? xml version = "1.0" encoding = "UTF-8" ?>
< HD >
? < disk name = "C" >
??? < capacity > 8G </ capacity >
??? < directories > 200 </ directories >
??? < files > 1580 </ files >
? </ disk >
?
? < disk name = "D" >
??? < capacity > 10G </ capacity >
??? < directories > 500 </ directories >
??? < files > 3000 </ files >
? </ disk >
?
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Sample1 {
? public static void main(String[] args) throws Exception{
??? SAXBuilder sb= new SAXBuilder();
??? Document doc =sb.build(Sample1.class.getClassLoader().getResource("test.xml")); //构造文档对象 象
??? Element root=doc .getRootElement(); // 获取根元素
??? List list=root.getChildren( "disk" ); // 取名字为 disk 的所有元素
??? for ( int i=0;i<list.size();i++){
?????? Element element=(Element)list.get(i);
?????? String name=element.getAttributeValue( "name" );
?????? String capacity=element.getChildText( "capacity" ); // 取 disk 子元素 capacity 的内容
?????? String directories=element.getChildText( "directories" );
?????? String files =element.getChildText( "files" );
?????? System. out .println( " 磁盘信息 :" );
?????? System. out .println( " 分区盘符 :" +name);
?????? System. out .println( " 分区容量 :" +capacity);
?????? System. out .println( " 目录数 :" +directories);
?????? System. out .println( " 文件数 :" +files);
?????? System. out .println( "-----------------------------------" );
??? }?
? }
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Sample1 {
? public static void main(String[] args) throws Exception{
??? SAXBuilder sb= new SAXBuilder();
??? Document doc =sb.build(Sample1.class.getClassLoader().getResource("test.xml")); //构造文档对象 象
??? Element root=doc .getRootElement(); // 获取根元素
??? List list=root.getChildren( "disk" ); // 取名字为 disk 的所有元素
??? for ( int i=0;i<list.size();i++){
?????? Element element=(Element)list.get(i);
?????? String name=element.getAttributeValue( "name" );
?????? String capacity=element.getChildText( "capacity" ); // 取 disk 子元素 capacity 的内容
?????? String directories=element.getChildText( "directories" );
?????? String files =element.getChildText( "files" );
?????? System. out .println( " 磁盘信息 :" );
?????? System. out .println( " 分区盘符 :" +name);
?????? System. out .println( " 分区容量 :" +capacity);
?????? System. out .println( " 目录数 :" +directories);
?????? System. out .println( " 文件数 :" +files);
?????? System. out .println( "-----------------------------------" );
??? }?
? }
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Sample1 {
? public static void main(String[] args) throws Exception{
??? SAXBuilder sb= new SAXBuilder();
??? Document doc =sb.build(Sample1.class.getClassLoader().getResource("test.xml")); //构造文档对象 象
??? Element root=doc .getRootElement(); // 获取根元素
??? List list=root.getChildren( "disk" ); // 取名字为 disk 的所有元素
??? for ( int i=0;i<list.size();i++){
?????? Element element=(Element)list.get(i);
?????? String name=element.getAttributeValue( "name" );
?????? String capacity=element.getChildText( "capacity" ); // 取 disk 子元素 capacity 的内容
?????? String directories=element.getChildText( "directories" );
?????? String files =element.getChildText( "files" );
?????? System. out .println( " 磁盘信息 :" );
?????? System. out .println( " 分区盘符 :" +name);
?????? System. out .println( " 分区容量 :" +capacity);
?????? System. out .println( " 目录数 :" +directories);
?????? System. out .println( " 文件数 :" +files);
?????? System. out .println( "-----------------------------------" );
??? }?
? }
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Sample1 {
? public static void main(String[] args) throws Exception{
??? SAXBuilder sb= new SAXBuilder();
??? Document doc =sb.build(Sample1.class.getClassLoader().getResource("test.xml")); //构造文档对象 象
??? Element root=doc .getRootElement(); // 获取根元素
??? List list=root.getChildren( "disk" ); // 取名字为 disk 的所有元素
??? for ( int i=0;i<list.size();i++){
?????? Element element=(Element)list.get(i);
?????? String name=element.getAttributeValue( "name" );
?????? String capacity=element.getChildText( "capacity" ); // 取 disk 子元素 capacity 的内容
?????? String directories=element.getChildText( "directories" );
?????? String files =element.getChildText( "files" );
?????? System. out .println( " 磁盘信息 :" );
?????? System. out .println( " 分区盘符 :" +name);
?????? System. out .println( " 分区容量 :" +capacity);
?????? System. out .println( " 目录数 :" +directories);
?????? System. out .println( " 文件数 :" +files);
?????? System. out .println( "-----------------------------------" );
??? }?
? }
import java.util.*;
import org.jdom.*;
import org.jdom.input.SAXBuilder;
public class Sample1 {
? public static void main(String[] args) throws Exception{
??? SAXBuilder sb= new SAXBuilder();
??? Document doc =sb.build(Sample1.class.getClassLoader().getResource("test.xml")); //构造文档对象 象
??? Element root=doc .getRootElement(); // 获取根元素
text-alig