JAVA读取XML配置文件
来源:互联网 发布:linux 音乐服务器 编辑:程序博客网 时间:2024/06/11 20:06
package com;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Parse{
//Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
//内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
private Document doc=null;
public void init(String xmlFile) throws Exception{
//很明显该类是一个单例,先获取产生DocumentBuilder工厂
//的工厂,在通过这个工厂产生一个DocumentBuilder,
//DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
doc=db.parse(new File(xmlFile));
}
//该方法负责把XML文件的内容显示出来
public void viewXML(String xmlFile) throws Exception{
this.init(xmlFile);
//在xml文件里,只有一个根元素,先把根元素拿出来看看
Element element=doc.getDocumentElement();
System.out.println("根元素为:"+element.getTagName());
NodeList nodeList=doc.getElementsByTagName("dbstore");
System.out.println("dbstore节点链的长度:"+nodeList.getLength());
Node fatherNode=nodeList.item(0);
System.out.println("父节点为:"+fatherNode.getNodeName());
//把父节点的属性拿出来
NamedNodeMap attributes=fatherNode.getAttributes();
for(int i=0;i<attributes.getLength();i++){
Node attribute=attributes.item(i);
System.out.println("dbstore的属性名为:"+attribute.getNodeName()+" 相对应的属性值为:"+attribute.getNodeValue());
}
NodeList childNodes = fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for(int j=0;j<childNodes.getLength();j++){
Node childNode=childNodes.item(j);
//如果这个节点属于Element ,再进行取值
if(childNode instanceof Element){
//System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
}
}
}
public static void main(String[] args)throws Exception{
Parse parse=new Parse();
//我的XML文件
parse.viewXML("netct.xml");
}
}
- java读取xml配置文件
- java读取xml配置文件
- JAVA读取XML配置文件
- java读取xml配置文件
- java读取XML配置文件
- java读取xml配置文件
- java读取xml配置文件(sax)
- java读取xml配置文件(sax)
- java xml作为配置文件,读取
- java读取xml配置文件(小结)
- java读取xml配置文件(小结)
- java读取xml/.properties配置文件
- JAVA技术:XML配置文件的读取处理
- JAVA技术:XML配置文件的读取处理
- JAVA解析XML配置文件的读取操作
- xml相关概念及java读取配置文件
- 利用java反射读取xml配置文件
- Java中读取配置文件(properties、xml)
- android 权限大全
- PL/SQL单行字符串函数
- eclipse/myeclipse高亮显示 处理。
- wordpress中的Feeds
- verilog HDL 的阻塞和非阻塞语句在FPGA各种设计中的分析
- JAVA读取XML配置文件
- Ubuntu安装启动SSH服务器
- lm3s811 学习笔记(二)【自己建工程】
- Linux教程:修改文件权限命令chmod的用法
- Android扫盲教程大全经典教程全分享
- Style of WebService: REST vs. SOAP
- 正则表达式入门经典(学习笔记七)——正则表达式的灵敏度和特殊性
- 新手如何安装 Orchard
- 2011/07/29, SpriteTest看完后的问题