我就想在XML里设置变量,然后在类里面获取到。方便以后项目做更改。谁能给个最简单的方法。 最好把设置变量和获取的过程都写出来。呵呵~

解决方案 »

  1.   

    呵呵,就是对xml的操作嘛……
      

  2.   

    对xml做解析就行了
    网上搜一下比较多的
    http://lavasoft.blog.51cto.com/62575/66953
      

  3.   

    propertise ,jdom ,dom4j, ....太多了
      

  4.   

    -  -0  网上dom4j的内容太多了。 我就要dom4j里的一个最简单的功能:获取一个变量而已……
      

  5.   

    Document document = new SAXReader().read(
    Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath));
    List modules = document.selectNodes("//Modules/Module;
    for (Iterator iter = modules.iterator(); iter.hasNext();) {
    Element element = (Element) iter.next();
    ......
    }
    记不清了
      

  6.   

    我只是想知道怎么用dom4j,它里面的机制我不想知道~
      

  7.   

    楼主诸位给你的就是例子,哪里有Dom4j的内部机制了?
      

  8.   

    -  -0 我完全看不懂啊~我的需求很简单,但是Dom4j那些太复杂了。看来我还是直接给出需求吧我用以下方法在xml文件中设置变量: <context-param>
    <param-name>page_size</param-name>
    <param-value>5</param-value>
    </context-param> <context-param>
    <param-name>max_index_pages</param-name>
    <param-value>5</param-value>
    </context-param>那么我想在类中读取到这两个值,该如何读取~
      

  9.   

    可以参考一下,然后自己再修改一下,你的要求是不算难,写死就好了.
    import java.io.File;
    import java.util.List;import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Node;
    import org.dom4j.io.SAXReader;public class Dom4jApp {
        public static Document parse(String fileName) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(fileName));
            return document;
        }
        
        public static void main(String[] args) throws DocumentException {
            String fileName = "resources/a.xml";
            Document doc = parse(fileName);
            
            List nodes = doc.selectNodes( "/root/context-param/param-name" );
            for (Object obj : nodes) {
                Node n = (Node) obj;
                System.out.printf("Tag: %s, Text: %s\n", n.getName(), n.getText());
            }
            
            nodes = doc.selectNodes( "/root/context-param/param-value" );
            for (Object obj : nodes) {
                Node n = (Node) obj;
                System.out.printf("Tag: %s, Text: %s\n", n.getName(), n.getText());
            }
        }
    }
      

  10.   


    谢了~不过这样还是太麻烦了,还是不用dom4j了吧,肯定还有其他方法可以轻松获取里面的值的!
      

  11.   

    用 properties 应方便。。  load , get 就行了  不要xml
      

  12.   

    这个是xml的解析,你先学一下JDOM看一下怎么解析xml和创建xml之后就都明白了
      

  13.   

    http://blog.csdn.net/kbyst/archive/2009/05/31/4228024.aspx
    以前写过的,楼主可以参考一下。
      

  14.   

    我教你一个简单的ResourceBundle
    获取key=value文件的值
      

  15.   

    不是说难学,而是我要设置的变量在程序里经常会使用到,一旦需要使用又得把那个类注入进去才能取到,整个项目显得很累赘~所以我觉得这个需求不太适合用dom4j做!
      

  16.   


    resourcebundle.getBundle()
    这个能获取xml文件吗? 那文件的路径要怎么获取呢?  我获取不到,我的xml文件路径在:
    /【项目名】/WebRoot/WEB-INF/web.xml这么写没办法获取啊,应该怎么写?
      

  17.   

    获取这种格式的文件后缀一般是xx.properties
    name=刘友
    sex=男
      

  18.   

    哈哈哈~~~这个好,还是存在properties里方便,谢啦!