ITeye博客

用DOM读取XML文件的案例

tobemoved · 2008-07-09 09:16 · java代码 · Markdown

//用DOM读取XML
public class A {
        Document document = null;
    public void t() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(configPath);
            Node rootNode = document.getFirstChild();
            System.out.println(rootNode.toString());
            NodeList logFileList = rootNode.getChildNodes();
            for (int i = 1; i < logFileList.getLength(); i += 2) {
                Node curNode = logFileList.item(i);
                System.out.println("Log file" + i + " : " + curNode.toString());
                NamedNodeMap map = curNode.getAttributes();
                for (int j = 0; j < map.getLength(); j++) {
                    System.out.println("Log file" + i + " :key: " + map.item(j).getNodeName());
                    System.out.println("Log file" + i + " :value: " + map.item(j).getNodeValue());
                }
                NodeList columns = curNode.getChildNodes();
                for (int j = 1; j < columns.getLength(); j += 2) {
                    System.out.println("column" + j + " : " + columns.item(j).toString());
                }
            }
        } catch (ParserConfigurationException pce) {pce.printStackTrace(); }
          catch (SAXException f) {
            f.printStackTrace();
        } catch (IOException c) {
            c.printStackTrace();
        }
    }
    public static void main(String[] args){
        A a =new A();
        a.t();
    }
}

 

//XML文件
<?xml version="1.0" encoding="UTF-8"?> 
<LOG_MAPPING> 
    <LOG_FILE fileNamePattern="ad.+\.log" class="com.nec.jp.bigm.log.ADLogCounter"> 
        <column name="date"/> 
        <column name="carrier"/> 
        <column name=""/> 
    </LOG_FILE> 
    <LOG_FILE fileNamePattern="access.+\.log" class="com.nec.jp.bigm.log.ADLogCounter"> 
        <column name=""/> 
        <column name=""/> 
        <column name=""/> 
    </LOG_FILE> 
</LOG_MAPPING> 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。