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需要怎样改。谢谢!

解决方案 »

  1.   

    取出来也只是string,自己再进行替换。
      

  2.   


    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;
    }经过测试
      

  3.   


    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"));
    }
    }给你发个完整版的
      

  4.   

    感谢ZiSheng 。
    你是否也有替换属性的代码?:)
      

  5.   

    呵呵,可以找找,既然有getNodeValue()当然也有setNodeValue();
      

  6.   


    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"));
        }
    }