JAVA读取xml文件
来源:互联网 发布:安徽幻维网络是培训吗 编辑:程序博客网 时间:2024/05/17 08:59
一.java类
- package
com.java.test; -
- import
org.w3c.dom.*; - import
javax.xml.parsers.*; - import
java.io.*; -
- public
class JavaReadXml { - //
Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对 - //
内存的操作来实现对XML的操作,首先第一步获取XML相关的Document - private
Document null;doc = -
- public
void init(String throwsxmlFile) Exception { - //
很明显该类是一个单例,先获取产生DocumentBuilder工厂 - //
的工厂,在通过这个工厂产生一个DocumentBuilder, - //
DocumentBuilder就是用来产生Document的 - DocumentBuilderFactory
dbf = DocumentBuilderFactory.newInstance(); - DocumentBuilder
db = dbf.newDocumentBuilder(); - //
这个Document就是一个XML文件在内存中的镜像 - doc
= newdb.parse( File(xmlFile)); - }
-
- //
该方法负责把XML文件的内容显示出来 - public
void viewXML(String throwsxmlFile) Exception { - this.init(xmlFile);
- //
在xml文件里,只有一个根元素,先把根元素拿出来看看 - Element
element = doc.getDocumentElement(); - System.out.println("根元素为:"
+ element.getTagName()); -
- NodeList
nodeList "person");= doc.getElementsByTagName_r( - System.out.println("book节点链的长度:"
+ nodeList.getLength()); -
- Node
fatherNode 0);= nodeList.item( - System.out.println("父节点为:"
+ fatherNode.getNodeName()); -
- //
把父节点的属性拿出来 - NamedNodeMap
attributes = fatherNode.getAttributes(); -
- for
( inti 0;= i < attributes.getLength(); i++) { - Node
attribute = attributes.item(i); - System.out.println("person的属性名为:"
+ attribute.getNodeName() - +
" 相对应的属性值为:" + attribute.getNodeValue());//原作者这块写的是book的属性名,经调试应该是person的 - }
-
- NodeList
childNodes = fatherNode.getChildNodes(); - System.out.println(childNodes.getLength());
- for
( intj 0;= j < childNodes.getLength(); j++) { - Node
childNode = childNodes.item(j); - //
如果这个节点属于Element ,再进行取值 - if
(childNode instanceofElement) { - //
System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue()); - System.out.println("子节点名为:"
+ childNode.getNodeName() - +
"相对应的值为" + childNode.getFirstChild().getNodeValue()); - }
- }
-
- }
-
- public
static void main(String[] throwsargs) Exception { - JavaReadXml
parse new= JavaReadXml(); -
- //
我的XML文件 - parse.viewXML("person.xml");
- }
- }
二.xml文件
- <?xml
version= "1.0"encoding= "UTF-8"?> - <book>
- <person>
- <first>wang</first>
- <last>laohu</last>
- <age>25</age>
- <version>中国邮电出版社</version>
- </person>
- <person>
- <first>li</first>
- <last>junjia</last>
- <age>24</age>
- <version>清华大学出版社</version>
- </person>
- </book>
三.输出结果
根元素为:book
book节点链的长度:2
父节点为:person
9
子节点名为:first相对应的值为wang
子节点名为:last相对应的值为laohu
子节点名为:age相对应的值为25
子节点名为:version相对应的值为中国邮电出版社
- <?xml
- JAVA读取XML文件
- JAVA读取XML文件
- JAVA读取XML文件
- JAVA读取XML文件
- JAVA读取xml文件
- java 读取xml文件
- java读取XML文件
- Java 读取XML文件
- java读取XML文件
- Java读取xml文件
- JAVA读取xml文件
- Java读取xml文件
- Java读取xml文件
- java 读取xml文件
- JAVA读取XML文件
- JAVA读取xml文件
- JAVA读取xml文件
- java读取xml文件
- ubuntu安装netbeans方法及解决netbeans乱码方法
- LINUX下使用移动3G上网卡攻略
- scsi命令
- .NET 面试题总结 (附有参考答案) 第3部分
- 基数排序 RadixSort 算法分析
- JAVA读取xml文件
- VC++ Tab Control控件的使用
- eclipse subversion native library not available
- Windows环境进程间通信(一):共享文件方式
- 怕忘记了。记录一下要采购的元件----1
- MySQL存储过程-插入后返回ID
- POJ 2155 二维线段树 书套树
- 关于百度蛛蛛日志分析对SEO的作用
- 软件测试方法汇总