xml文件中用到了属性的值,java如何读取?
跟build.xml类似。xml文件如:
<project>
……
<property name="decoder.lib" value="ext"/>
……
<path id="decoder.classpath">
<pathelement location="${decoder.lib}/jasper-runtime.jar" />
……
</path>
</project>我如何在java中方便的获取到location的值?
或者我的xml需要怎样改。谢谢!
跟build.xml类似。xml文件如:
<project>
……
<property name="decoder.lib" value="ext"/>
……
<path id="decoder.classpath">
<pathelement location="${decoder.lib}/jasper-runtime.jar" />
……
</path>
</project>我如何在java中方便的获取到location的值?
或者我的xml需要怎样改。谢谢!
解决方案 »
- 刚学struts2,出现错误,搜索后依然不会解决,麻烦大家帮忙看下
- 去购物了
- 求不依赖客户端倒计时程序
- Struts问题!
- 初学者请教,myeclipse怎么开发ejb?
- tomcat关于路径问题
- 在WIN2000中,数据库用SQLServer 2000,怎样配置weblogic6.1sp1?急!!!!!!!!!!
- 有人知道吗?在线等?关于findByPrimaryKey()
- cmp2.0中可以实现对日期的操作吗?
- 请问:容器管理的实体Bean是如何实现连接数据库的?
- 我的一个报表项目在tomcat能够部署运行,当我将项目发布到weblogic的时候报500 instal server error这个问题怎么解决?
- 想做过学习网站。不知道怎么设计。请大家给点意见
public String getElementContent(String elementName){
String strReturn="";
try{
NodeList nodeList=doc.getElementsByTagName(elementName);
Node node0=nodeList.item(0);
NamedNodeMap m1=node0.getAttributes();
Node nodeAttri=m1.item(0);//获得第一个属性的值
System.out.println(nodeAttri.getNodeValue());
}catch(Exception e){
strReturn=e.toString().replaceAll("\n", "");
}
return strReturn;
}经过测试
public class XmlProcessor {
private Document doc=null;
/**
* param1 xmlPath 指定XML文件的本地路径
* @param xmlPath
*/
public XmlProcessor(String xmlPath){
//很明显该类是一个单例,先获取产生DocumentBuilder工厂
//的工厂,在通过这个工厂产生一个DocumentBuilder,
//DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
doc=db.parse(new File(xmlPath));
}catch(Exception e){
System.out.println("获取文档对象失败");
}
}
public String getElementContent(String elementName){
String strReturn="";
try{
NodeList nodeList=doc.getElementsByTagName(elementName);
Node node0=nodeList.item(0);
NamedNodeMap m1=node0.getAttributes();
Node nodeAttri=m1.item(0);//获得第一个属性的值
System.out.println(nodeAttri.getNodeValue());
}catch(Exception e){
strReturn=e.toString().replaceAll("\n", "");
}
return strReturn;
}
public static void main(String[] args){
XmlProcessor xml=new XmlProcessor("D:\\struts2\\form1\\src\\common\\dbcfg.xml");
System.out.println(xml.getElementContent("driver"));
}
}给你发个完整版的
你是否也有替换属性的代码?:)
public class XmlProcessor {
private Document doc=null;
/**
* param1 xmlPath 指定XML文件的本地路径
* @param xmlPath
*/
public XmlProcessor(String xmlPath){
//很明显该类是一个单例,先获取产生DocumentBuilder工厂
//的工厂,在通过这个工厂产生一个DocumentBuilder,
//DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
doc=db.parse(new File(xmlPath));
}catch(Exception e){
System.out.println("获取文档对象失败");
}
} public String getElementContent(String elementName){
String strReturn="";
try{
NodeList nodeList=doc.getElementsByTagName(elementName);
Node node0=nodeList.item(0);
System.out.println(node0.getNodeName());
NamedNodeMap m1=node0.getAttributes();
Node nodeAttri=m1.item(0);
System.out.println("修改前"+nodeAttri.getNodeValue());
nodeAttri.setNodeValue("cccc");
System.out.println("修改后"+nodeAttri.getNodeValue());
strReturn=node0.getFirstChild().getNodeValue();
}catch(Exception e){
strReturn=e.toString().replaceAll("\n", "");
}
return strReturn;
}
public static void main(String[] args){
XmlProcessor xml=new XmlProcessor("D:\\struts2\\form1\\src\\common\\dbcfg.xml");
System.out.println(xml.getElementContent("driver"));
}
}