正文

在java中读取xml信息2006-01-13 00:39:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wtblog/9576.html

分享到:

JAVA文件!!!

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文件!!

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE class SYSTEM "X1.dtd">
<class>
    <class_purview>public</class_purview>
    <!--类访问权限 可选-->
    <class_name>dsjkdsa</class_name>
    <!--类名 必选-->
    <method>
        <!--类的方法 可选-->
        <method_purview>publie</method_purview>
        <!--方法的访问权限 可选-->
        <method_name>dd</method_name>
        <!--方法名 必选-->
        <return_type>int</return_type>
        <!--方法的返回类型 必选-->
        <parameter>
            <!--方法的参数 可选-->
            <parameter_name>aa</parameter_name>
            <!--参数名 必选-->
            <parameter_return_type>int</parameter_return_type>
        </parameter>
        <!--参数类型 必选-->
    </method>
    <property>
        <!--类的属性 可选-->
        <property_purview>private</property_purview>
        <!--属性的访问权限 可选-->
        <property_name>a</property_name>
        <!--属性名 必选-->
        <property_type>int</property_type>
        <!--属性类型 必选-->
        <property_value>9</property_value>
        <!--属性的默认值 可选-->
    </property>
    <!--*********************************************************-->
    <method>
        <method_purview>public</method_purview>
        <method_name>getName</method_name>
        <return_type>String</return_type>
    </method>
    <method>
        <method_purview>public</method_purview>
        <method_name>setName</method_name>
        <return_type>String</return_type>
        <parameter>
            <parameter_name>a</parameter_name>
            <parameter_return_type>String</parameter_return_type>
        </parameter>
        <parameter>
            <parameter_name>b</parameter_name>
            <parameter_return_type>java.lang.Object</parameter_return_type>
        </parameter>
        <parameter>
            <parameter_name>c</parameter_name>
            <parameter_return_type>int</parameter_return_type>
        </parameter>
    </method>
    <property>
        <property_purview>private</property_purview>
        <property_name>name</property_name>
        <property_type>String</property_type>
        <property_value>""</property_value>
    </property>
        <property>
        <property_name>jpane</property_name>
        <property_type>javax.swing.JPanel</property_type>
        <property_value>new javax.swing.JPanel()</property_value>
    </property>
        <property>
        <property_name>but</property_name>
        <property_type>javax.swing.JButton</property_type>
    </property>
</class>

阅读(2018) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册