How to read properties from xml file with java?

后端 未结 7 1324
独厮守ぢ
独厮守ぢ 2021-01-08 00:51

I have the following xml file:


    
        
        

        
相关标签:
7条回答
  • 2021-01-08 01:52

    There are many ways. One is to do JDOM and xpath. Something like this (from this article: http://onjava.com/onjava/2005/01/12/xpath.html):

    SAXBuilder saxBuilder = 
        new SAXBuilder("org.apache.xerces.parsers.SAXParser");
    org.jdom.Document jdomDocument =
        saxBuilder.build(new File("somefile");
    org.jdom.Attribute levelNode = 
        (org.jdom.Attribute)(XPath.selectSingleNode(
            jdomDocument,
            "/resources/resource[@id='res003']/property[@name='propF']/@value"));
    System.out.println(levelNode.getValue());
    

    Did not test it, but should work. For xpath tutorial see http://zvon.org/xxl/XPathTutorial/General/examples.html . Its the best and fastest tutorial.

    Take care about the saxbuilder lifecycle, if it is called often.

    0 讨论(0)
提交回复
热议问题