永无止境的程序

..::[......]::..

导航

统计

公告

2005年2月2日 #

用DOM读取XML文件例子(JAVA)

        Document document = null;
        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 e) {
            e.printStackTrace();
        }
 catch (SAXException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }


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>

posted @ 2005-02-02 13:32 AlleNny 阅读(2236) 评论(0) 编辑