<?xml version="1.0" encoding="UTF-8"?>
<Metrics scope="somlb05p" type="Project" date="2008-05-21" xmlns="http://metrics.sourceforge.net/2003/Metrics-First-Flat">
<Metric id = "NSM" description ="Number of Static Methods">
      <Values per = "type" total = "0" avg = "0" stddev = "0" max = "0">
      <Value name="B05pBC" source ="B05pBC.java" package ="com.systex.som.somlb05p.model.business" value ="0"/>
       <Value name="B05pRpt" source ="B05pRpt.java" package="com.systex.som.somlb05p.model.business" value ="0"/>
       </Values>
   </Metric>
   <Metric id = "TLOC" description ="Total Lines of Code">
      <Value value="595"/>
   </Metric>
<Metric id = "MLOC" description ="Method Lines of Code">
      <Values per = "method" total = "326" avg = "7.951" stddev = "23.194" max = "150">
         <Value name="initComponents" source ="B05pReportPanel.java" package ="com.systex.som.somlb05p.view" value ="150"/>
         <Value name="getTransactionDate" source ="B05pRpt.java" package ="com.systex.som.somlb05p.model.business" value ="19"/>
         <Value name="toReport" source ="B05pBusiness.java" package ="com.systex.som.somlb05p.view" value ="19"/>
         <Value name="getReportInfo" source ="B05pRemote.java" package ="com.systex.som.somlb05p.model.client.impl" value ="17"/>
         <Value name="colleaguesChanged" source ="B05pBusiness.java" package ="com.systex.som.somlb05p.view" value ="17"/>
         <Value name="getJasperMap" source ="B05pRpt.java" package ="com.systex.som.somlb05p.model.business" value ="15"/>
         <Value name="getBhno" source ="B05pRpt.java" package ="com.systex.som.somlb05p.model.business" value ="13"/>
         <Value name="composeSql" source ="B05pRpt.java" package ="com.systex.som.somlb05p.model.business" value ="11"/>
         <Value name="initUI" source ="B05pReportPanel.java" package ="com.systex.som.somlb05p.view" value ="9"/>
         <Value name="constructPage" source ="B05pWS.java" package ="com.systex.som.somlb05p.view" value ="9"/>
         <Value name="getData" source ="B05pFactory.java" package ="com.systex.som.somlb05p.model.client" value ="7"/>
         <Value name="composeSql" source ="B05pRpt.java" package ="com.systex.som.somlb05p.model.business" value ="5"/>
         <Value name="checkData" source ="B05pBusiness.java" package ="com.systex.som.somlb05p.view" value ="5"/>
                           
   </Metric>
</Metrics>
我有160多个这种xml文件,想从中提取scope="somlb05p,TLOC中的value值,还有Mloc中各方法名字,以及每个的value值~
请厉害的人帮帮我吧

解决方案 »

  1.   

    我现在不需要批量读入xml文件,当然如果能这么做是最好的了~我的大致思想是从根节点下面循环判断标签的属性id是不是tloc,如果是的话,就访问他的子节点入value~但是循环判断我写不出来~
      

  2.   

    google 搜索
    xml sax 你会看到许多的例子!自己去学习。这是程序员的基本素质!
      

  3.   

    http://zhidao.baidu.com/question/30848798.html
    http://hi.baidu.com/mycollection/blog/item/fd15ac00b517af86e850cd76.html
    http://www.diybl.com/course/3_program/java/javashl/2008520/117123.html
      

  4.   

    package reader;
    import org.jdom.*;
    import org.jdom.output.*;
    import org.jdom.input.*;
    import java.io.*;
    import java.util.List;public class reading {
        public reading() 
        {
        SAXBuilder builder = new SAXBuilder();
        Document doc = null;
    try {
    doc = builder.build("somtc50v.xml");
    } catch (JDOMException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
           
        Element root = doc.getRootElement();
            List metrics = root.getChildren();
            if (metrics!=null) ;
            {
         for (int i = 0; i < metrics.size(); i++) 
                {
                    Element metric= (Element)metrics.get(i);
                    String check=metric.getAttribute("id").getValue();
                    String tloc="TLOC";
                    if (check==tloc) ;
                 {
                      Element kid = metric.getChild("value");
                      String number =kid.getAttributeValue("value");
                      System.out.println(number); 
                 }
    }
    }
    }
      public static void main(String[] args) {
       reading read=new reading ();
    }
    }能帮我看看哪错了吗?
      

  5.   

    import java.io.FileInputStream;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    public class LoadXml ...{    /** *//**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception ...{
            // TODO Auto-generated method stub
            //从 XML 文档获取 DOM 文档实例。使用此类,应用程序员可以从 XML 获取一个Document
            DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
            FileInputStream in=new FileInputStream("F:\wrokspace\learnspring\src\ch7\111.xml");
            Document document=db.parse(in);
            NodeList nl=document.getElementsByTagName("a");
            showElem(nl);        
            in.close();
        }
        /** *//**
         * 通过递归得到所有子节点
         * @param nl
         */
        public static void showElem(NodeList nl)...{
            for(int i=0;i<nl.getLength();i++)...{
                Node n=nl.item(i);
                if(n.hasChildNodes())...{
                    System.out.print("<"+n.getNodeName()+">");
                    //递归
                    showElem(n.getChildNodes());
                    System.out.print("</"+n.getNodeName()+">");
                }else...{
                    //判断是不是文本
                    if(n.getNodeType()==Node.TEXT_NODE)...{
                        System.out.print(n.getNodeValue());
                    }else...{
                        System.out.print("<"+n.getNodeName()+">");
                        System.out.print("</"+n.getNodeName()+">");
                    }
                    break;
                }
            }
        }}
      

  6.   

    import java.io.FileInputStream;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    public class LoadXml{    /** *//**
         * @param args
         * @throws Exception 
         */
        public static void main(String[] args) throws Exception{
            // TODO Auto-generated method stub
            //从 XML 文档获取 DOM 文档实例。使用此类,应用程序员可以从 XML 获取一个Document
            DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
            FileInputStream in=new FileInputStream("F:\wrokspace\learnspring\src\ch7\111.xml");
            Document document=db.parse(in);
            NodeList nl=document.getElementsByTagName("a");
            showElem(nl);        
            in.close();
        }
        /** *//**
         * 通过递归得到所有子节点
         * @param nl
         */
        public static void showElem(NodeList nl){
            for(int i=0;i<nl.getLength();i++){
                Node n=nl.item(i);
                if(n.hasChildNodes()){
                    System.out.print("<"+n.getNodeName()+">");
                    //递归
                    showElem(n.getChildNodes());
                    System.out.print("</"+n.getNodeName()+">");
                }else{
                    //判断是不是文本
                    if(n.getNodeType()==Node.TEXT_NODE){
                        System.out.print(n.getNodeValue());
                    }else{
                        System.out.print("<"+n.getNodeName()+">");
                        System.out.print("</"+n.getNodeName()+">");
                    }
                    break;
                }
            }
        }}