jdom读取XML文件

项目中实例(参照最下面实例所写):

package com.hot35.common.action;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.hot35.common.form.XMLTestForm;

public class XMLTestAction extends BasicAction{
 protected final Logger log = Logger.getLogger(getClass());
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  log.info("Begin to execute: "+this.getClass().getSimpleName()+".");
  XMLTestForm xml=(XMLTestForm )form;
  String strURL="D:/workspace0823/hot/WebContent/test.xml";
  
  Element eroot = null;

  request.getParameter("name");
  log.info(request.getParameter("name"));
  SAXBuilder builder = new SAXBuilder();
  Document doc = builder.build(new File(strURL));
  
  request.setAttribute("xml",doc );
  
  Element root = doc.getRootElement(); //根节点
   String result = root.getChildText("result"); //根节点之下的子节点class_name的内容
      String totalduration = root.getChildText("totalduration") == null ? "" : root.getChildText("totalduration");
      String totalcostcash = root.getChildText("totalcostcash") == null ? "" : root.getChildText("totalcostcash");
      String totaltimes = root.getChildText("totaltimes") == null ? "" : root.getChildText("totaltimes");
      log.info("result:"+result);
      log.info("totalduration:"+totalduration);
      log.info("totalcostcash:"+totalcostcash);
      log.info("totaltimes:"+totaltimes);
     
      java.util.List childrens = root.getChildren();  //得到所有属于根节点下的同级自节点
       for (int i = 0; i < childrens.size(); i++) {   //逐个打印 属性 信息
         org.jdom.Element ele = (org.jdom.Element) childrens.get(i);
         if (ele.getName().equals("cdrs")) {
          List li =ele.getChildren("cdr");
          for(int k=0;k            Element ele2 = (Element) li.get(k);
     String calltype = ele2.getAttributeValue("calltype");
     String caller = ele2.getAttributeValue("caller");
     String callee = ele2.getAttributeValue("callee");
     String begintime = ele2.getAttributeValue("begintime");
     String endtime = ele2.getAttributeValue("endtime");
     String duration = ele2.getAttributeValue("duration");
     String cost = ele2.getAttributeValue("cost");
     String udInfo = ele2.getAttributeValue("udInfo");
     log.info("calltype:" + calltype);
     log.info("caller:" + caller);
     log.info("callee:" + callee);
     log.info("begintime:" + begintime);
     log.info("endtime:" + endtime);
     log.info("duration:" + duration);
     log.info("cost:" + cost);
     log.info("udInfo:" + udInfo);
          }          
  
         }
       }        

         return mapping.findForward("success");
 }
}

所读

 

 

 

public class Untitled1 {

  public static void main(String[] args) {
    try {
      org.jdom.input.SAXBuilder sb = new org.jdom.input.SAXBuilder();
      org.jdom.Document doc = sb.build(new java.io.File("F:\\xsl\\x1.xml")); //改这个路径
      org.jdom.Element root = doc.getRootElement(); //根节点
      String class_name = root.getChildText("class_name"); //根节点之下的子节点class_name的内容
      String class_purview = root.getChildText("class_purview") == null ? "" : root.getChildText("class_purview");
      System.out.println(class_purview +" class " + class_name + "{"); //打印类信息
      java.util.List childrens = root.getChildren();  //得到所有属于根节点下的同级自节点
      for (int i = 0; i < childrens.size(); i++) {   //逐个打印 属性 信息
        org.jdom.Element ele = (org.jdom.Element) childrens.get(i);
        if (ele.getName().equals("property")) {
          String property_purview = ele.getChildText("property_purview")==null?"":ele.getChildText("property_purview")+" ";
          String property_name = ele.getChildText("property_name");
          String property_value = ele.getChildText("property_value")==null?"":"="+ele.getChildText("property_value");
          String property_type = ele.getChildText("property_type");
          System.out.println( property_purview + property_type +" "+ property_name + property_value +";");
        }
      }
      for (int i = 0; i < childrens.size(); i++) {  //逐个打印方法信息
        org.jdom.Element ele = (org.jdom.Element) childrens.get(i);
        if (ele.getName().equals("method")) {
          String method_name = ele.getChildText("method_name");
          String return_type = ele.getChildText("return_type");
          String method_purview = ele.getChildText("method_purview") == null ? "" : ele.getChildText("method_purview");
          java.util.List methodChildrens = ele.getChildren();
          String parameter_stat="";
          for (int j = 0; j < methodChildrens.size(); j++) {
            org.jdom.Element methodEle = (org.jdom.Element) methodChildrens.get(j);
            if (methodEle.getName().equals("parameter"))
            {
              String parameter_return_type = methodEle.getChildText("parameter_return_type");
              String parameter_name = methodEle.getChildText("parameter_name");
              parameter_stat = parameter_stat + parameter_return_type + " " +  parameter_name + ",";
           }
          }
          try {
            parameter_stat = parameter_stat.substring(0,parameter_stat.length()-1);
          }
          catch (Exception ex) {}
          System.out.println( method_purview + " "+ return_type +" "+ method_name + "("+ parameter_stat +"){");
          System.out.println("}");
        }
      }
      System.out.println("}");
    }
    catch (java.lang.Exception ex) {
      ex.printStackTrace();
    }
  }
}

 


xml文件格式:

<class></class>
    <class_purview></class_purview>public
    
    <class_name></class_name>dsjkdsa
    
    <method>
        
        <method_purview></method_purview>publie
        
        <method_name></method_name>dd
        
        <return_type></return_type>int
        
         <parameter></parameter>
            
             <parameter_name></parameter_name> aa
            
             <parameter_return_type></parameter_return_type> int
        
        
    </method>
     <property>
        
         <property_purview></property_purview> private
        
         <property_name></property_name> a
        
         <property_type></property_type> int
        
         <property_value></property_value> 9
        
    </property>
    
    <method>
        <method_purview></method_purview>public
        <method_name></method_name>getName
        <return_type></return_type>String
    </method>
    <method>
        <method_purview></method_purview>public
        <method_name></method_name>setName
        <return_type></return_type>String
         <parameter></parameter>
             <parameter_name></parameter_name> a
             <parameter_return_type></parameter_return_type> String
        
         <parameter></parameter>
             <parameter_name></parameter_name> b
             <parameter_return_type></parameter_return_type> java.lang.Object
        
         <parameter></parameter>
             <parameter_name></parameter_name> c
             <parameter_return_type></parameter_return_type> int
        
    </method>
     <property>
         <property_purview></property_purview> private
         <property_name></property_name> name
         <property_type></property_type> String
         <property_value></property_value> ""
    </property>
         <property>
         <property_name></property_name> jpane
         <property_type></property_type> javax.swing.JPanel
         <property_value></property_value> new javax.swing.JPanel()
    </property>
         <property>
         <property_name></property_name> but
         <property_type></property_type> javax.swing.JButton
    </property>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值