<?xml version="1.0" encoding="UTF-8"?>
<commands>
<comand name="select" class="function">
Regular expression for select 
</comand>
<comand name="delete" class="function2">
Regular expression for delete
</comand>
</commands>解析上面那种格式的xml配置文件,我要解析出name 和 class 的属性值 
以及 "Regular expression for select ",怎么做??

解决方案 »

  1.   

    直接用DOM解析呗
    API里面有
      

  2.   

    顶,现在有两种解析XML的方法(DOM,SAX),各有利弊吧。。
    LZ何必自己写呢
      

  3.   

     用DOM or SAX来解析  
      

  4.   

    小文件直接用sax解析调就行了
    String xmlStr = 
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    +"<commands>"
    +"<comand name=\"select\" class=\"function\">"
    +"Regular expression for select"
    +"</comand>"
    +"<comand name=\"delete\" class=\"function2\">"
    +"Regular expression for delete"
    +"</comand>" 
    +"</commands>";

    InputStream xmlIo = new ByteArrayInputStream(xmlStr.getBytes());
    SAXBuilder builder = new SAXBuilder();
    Document document = builder.build(xmlIo);
    Element root = document.getRootElement();
    List commands  = root.getChildren();
    for(int i=0;i<commands.size();i++){
    Element comand = (Element)commands.get(i);
    System.out.println(comand.getAttributeValue("class"));
    System.out.println(comand.getAttributeValue("name"));
    System.out.println(comand.getText());
    }
    需要用到jdom的jar包