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需要怎样改。谢谢!
解决方案 »
- SSH 添加操作时,外键ID存储为空。
- url-pattern看不明白
- 请高手解决点问题!
- 如果数据库中没有你要查询的编号就要弹出一个对话框提示用户? 这该怎么弄? 请各位高手指教指教,谢谢了!
- 求救啊!!struts+spring+hibernate初学者的一个问题
- Hibernate 按外键查询问题:我有两个表,一个主表Dynasty,一个副表Emperor.(急,请HQL高手进来看看.多谢了)
- 大家帮我看看这样的查询用hibernate该怎么做?
- ActionContext.getContext().getParameters().put("action","add"),在resin没有报错,怎么weblogic就报错。
- 关于ActrionForward的问题
- 我在学习struts中碰到的问题
- 我的一个报表项目在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"));
}
}