java本身就有读取配置文件和XML的类
给你个小例子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");
}
}
给你个小例子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");
}
}
解决方案 »
- 如何在Action处理的前后加上Log?
- hibernate开启二级缓存,如果主键生成方式使用native的话,则session.save()是不会讲记录放入二级缓存的,这是为什么?
- CSS js 高手来
- 关于socket RMI EJB的共同点与不同应用场景的讨论
- myfaces中的DataTable怎么自动翻页.就是每隔一定时间,比如10秒种翻一页?
- velocity的取值问题,求解答。。
- 急,急,急,为什么这段代码通不过编译呀?
- 简单的JAVA问题,谢谢!!
- ************什么时候要用EJB,什么时候要用JAVA BEAN?
- maven打包时,如果项目代码发生变动,打包时版本号加1,代码没有发生变动,打包时版本号维持不变,应该怎样做?求大神告知
- 向高手求助SSH遇到的问题
- 用ECLIPSE开发web service插件在哪下?
好像有些配置文件在程序中有方法直接可以修改读取的,但是自己另外加的一些配置文件可能就不能用同样的方式读取。我看到xmlbean貌似有点点符合要求。还请大家多多给点建议!!!!!