Java如何读取xml文件

首先给大家推荐一个网站:http://semorhome.cn/tags/xml/

上面讲解比较好,例子简单实用.

 

----以下为我的代码,使用的是jdom的方式读取

package mt.project.xtgl.xmlparse;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import mt.tools.XMLProcess;

public class ParseXml {
  
 Document document;
 BzXmlDataPo bzxmldata;
 public ParseXml(){
   
  }
//返回document对象
 public Document getdocument(String path){
  
  String xmlparse=this.getClass().getResource("/").getPath();
  document=XMLProcess.xmlParse(xmlparse+path);//读取xml文件
  return document;
 }
 //ton为跟目录下节点1,childton为ton下字节点
 public List doup(String path,String ton,String childton,String attribute1,String attribute2){
  ArrayList<Object> array=new ArrayList<Object>();
  Element root=getdocument(path).getRootElement();//读取根节点
  List lista=root.getChildren(ton);
  for(int i=0;i<lista.size();i++){
   Element ela=(Element)lista.get(i);
   List listb=ela.getChildren(childton);
   
   for(int j=0;j<listb.size();j++){

  bzxmldata=new BzXmlDataPo();

    Element elb=(Element)listb.get(j);
    String stepid=elb.getAttributeValue(attribute1);
    String stepname=elb.getAttributeValue(attribute2);
    bzxmldata.setId(stepid);
    bzxmldata.setName(stepname);
    array.add(bzxmldata);
   }
   System.out.println("数组大小"+array.size());
  }
  return array;
  
 }
 
  public static void main(String[] args){
   ParseXml parsexml=new ParseXml();
   List list=parsexml.doup("bz.xml", "steps", "step","id","name");
   }
}

 

读取比较简单,bz.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE workflow PUBLIC "-//OpenSymphony Group//DTD OSWorkflow 2.8//EN" "http://www.opensymphony.com/osworkflow/workflow_2_8.dtd">
<workflow>
  <meta name="lastModified">Mon Jun 23 18:15:20 CST 2008</meta>
  <meta name="created">Sat Apr 12 13:21:14 CST 2008</meta>
  <meta name="generator">OSWOrkflow Designer</meta>
  <initial-actions>
    <action id="0" name="启动工作流">
      <pre-functions>
        <function type="beanshell">
          <arg name="script"><![CDATA[
                                propertySet.setString("issmall", "true");
        propertySet.setString("ischeck", "false");
                            ]]></arg>
        </function>
      </pre-functions>
      <results>
        <unconditional-result id="2" old-status="Finished" status="Queued" step="1"/>
      </results>
    </action>
  </initial-actions>
  <steps>
    <step id="1" name="业务受理">
      <actions>
        <action id="6" name="提交申请单" view="tjsq">
          <restrict-to>
            <conditions>
              <condition name="check.status" type="class">
                <arg name="status">Queued</arg>
                <arg name="class.name">com.opensymphony.workflow.util.StatusCondition</arg>
              </condition>
            </conditions>
          </restrict-to>
          <results>
            <result id="7" old-status="Finished" status="Queued" step="5">
              <conditions>
                <condition name="bsh.condition" type="beanshell">
                  <arg name="script"><![CDATA[]]></arg>
                </condition>
              </conditions>
            </result>
            <unconditional-result id="22" old-status="Finished" status="Underway" step="5" display-name="no"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="5" name="业务审核">
      <actions>
        <action id="8" name="不通过" view="nopass">
          <results>
            <unconditional-result id="9" old-status="Finished" status="Queued" step="1"/>
          </results>
        </action>
        <action id="24" name="是否为小用户" view="issmall">
          <pre-functions>
            <function type="beanshell">
              <arg name="script"><![CDATA[
                                String issmallstate =propertySet.getString("issmall");
        if(issmallstate.equals("true"))
           propertySet.setString("issmall", "false");
         else
            propertySet.setString("issmall", "true");
                            ]]></arg>
            </function>
          </pre-functions>
          <results>
            <unconditional-result id="25" old-status="Finished" status="Underway" step="5"/>
          </results>
        </action>
        <action id="26" name="是否需要验收" view="ischeck">
          <pre-functions>
            <function type="beanshell">
              <arg name="script"><![CDATA[
                                String ischeck =propertySet.getString("ischeck");
        if(ischeck.equals("true"))
           propertySet.setString("ischeck", "false");
         else
            propertySet.setString("ischeck", "true");
                            ]]></arg>
            </function>
          </pre-functions>
          <results>
            <unconditional-result id="27" old-status="Finished" status="Underway" step="5"/>
          </results>
        </action>
        <action id="28" name="通过" view="pass">
          <pre-functions>
            <function type="beanshell">
              <arg name="script"><![CDATA[
                          System.out.println(propertySet.getString("ischeck"));
        System.out.println(propertySet.getString("issmall"));
                            ]]></arg>
            </function>
          </pre-functions>
          <results>
            <result id="29" old-status="Finished" status="Queued" step="17" display-name="通过(不为小用户,不需要验收)">
              <conditions type="AND">
                <condition name="check.status" type="class">
                  <arg name="status">Underway</arg>
                  <arg name="class.name">com.opensymphony.workflow.util.StatusCondition</arg>
                  <arg name="stepId"></arg>
                </condition>
                <condition name="bsh.condition" type="beanshell">
                  <arg name="script"><![CDATA[propertySet.getString("issmall").equals("false")&&propertySet.getString("ischeck").equals("false")]]></arg>
                </condition>
              </conditions>
            </result>
            <result id="30" old-status="Finished" status="Queued" step="16" display-name="通过(不为小用户,需要验收)">
              <conditions type="AND">
                <condition name="check.status" type="class">
                  <arg name="status">Underway</arg>
                  <arg name="class.name">com.opensymphony.workflow.util.StatusCondition</arg>
                  <arg name="stepId"></arg>
                </condition>
                <condition name="bsh.condition" type="beanshell">
                  <arg name="script"><![CDATA[propertySet.getString("issmall").equals("false")&&propertySet.getString("ischeck").equals("true")]]></arg>
                </condition>
              </conditions>
            </result>
            <unconditional-result id="31" old-status="Finished" status="Queued" step="10" display-name="通过(小用户)"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="10" name="报装资料设计审核">
      <actions>
        <action id="69" name="不通过" view="nopass">
          <results>
            <unconditional-result id="70" old-status="Finished" status="Queued" step="1"/>
          </results>
        </action>
        <action id="230" name="通过" view="pass">
          <results>
            <unconditional-result id="231" old-status="Finished" status="Queued" step="44"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="16" name="验收(验收组长)">
      <actions>
        <action id="32" name="验收合格" view="pass">
          <results>
            <unconditional-result id="33" old-status="Finished" status="Queued" step="17"/>
          </results>
        </action>
        <action id="34" name="验收不合格" view="nopass">
          <results>
            <unconditional-result id="35" old-status="Finished" status="Queued" step="1"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="17" name="报装资料设计审核(设计处科长)">
      <actions>
        <action id="42" name="不通过(资料不齐全)" view="nopass">
          <results>
            <unconditional-result id="43" old-status="Finished" status="Queued" step="1"/>
          </results>
        </action>
        <action id="64" name="报装资料设计审核(设计处科长)" view="pass">
          <results>
            <result id="65" old-status="Finished" status="Queued" step="44" display-name="通过(不转设计中心)">
              <conditions>
                <condition name="bsh.condition" type="beanshell">
                  <arg name="script"><![CDATA[propertySet.getString("isdesigncenter").equals("false")]]></arg>
                </condition>
              </conditions>
            </result>
            <unconditional-result id="66" old-status="Finished" status="Queued" step="45" display-name="通过(转设计中心)"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="44" name="设计人员勘察设计">
      <actions>
        <action id="74" name="提交" view="submit">
          <results>
            <unconditional-result id="75" old-status="Finished" status="Queued" step="71"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="45" name="设计中心勘察出图">
      <actions>
        <action id="72" name="提交" view="submit">
          <results>
            <unconditional-result id="73" old-status="Finished" status="Queued" step="71"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="71" name="设计科长审核">
      <actions>
        <action id="133" name="通过" view="pass">
          <results>
            <unconditional-result id="134" old-status="Finished" status="Queued" step="105"/>
          </results>
        </action>
        <action id="137" name="不通过" view="nopass">
          <results>
            <unconditional-result id="138" old-status="Finished" status="Queued" step="45"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="76" name="工程预算">
      <actions>
        <action id="148" name="提交" view="submit">
          <results>
            <unconditional-result id="149" old-status="Finished" status="Queued" step="80"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="79" name="用户付款">
      <actions>
        <action id="195" name="提交" view="submit">
          <results>
            <unconditional-result id="196" old-status="Finished" status="Queued" step="193"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="80" name="预算书审核">
      <actions>
        <action id="150" name="通过" view="pass">
          <results>
            <unconditional-result id="151" old-status="Finished" status="Queued" step="79"/>
          </results>
        </action>
        <action id="191" name="不通过" view="nopass">
          <results>
            <unconditional-result id="192" old-status="Finished" status="Queued" step="76"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="87" name="工程登记">
      <actions>
        <action id="199" name="提交" view="submit">
          <results>
            <unconditional-result id="200" old-status="Finished" status="Queued" step="193"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="90" name="工程施工">
      <actions>
        <action id="155" name="提交" view="submit">
          <results>
            <unconditional-result id="156" old-status="Finished" status="Queued" step="154"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="93" name="出库">
      <actions>
        <action id="169" name="提交" view="submit">
          <results>
            <unconditional-result id="170" old-status="Finished" status="Queued" step="164"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="96" name="工程配表">
      <actions>
        <action id="179" name="提交" view="submit">
          <results>
            <unconditional-result id="180" old-status="Finished" status="Queued" step="167"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="99" name="完成">
    </step>
    <step id="105" name="资料审核填写联系单">
      <actions>
        <action id="128" name="通过(需要现场勘察)" view="pass">
          <results>
            <result id="129" old-status="Finished" status="Queued" step="106">
              <conditions>
                <condition name="bsh.condition" type="beanshell">
                  <arg name="script"><![CDATA[propertySet.getString("isrecce").equals("true")]]></arg>
                </condition>
              </conditions>
            </result>
            <unconditional-result id="186" old-status="Finished" status="Queued" step="109" display-name="通过(不需要现场勘察)"/>
          </results>
        </action>
        <action id="135" name="不通过" view="nopass">
          <results>
            <unconditional-result id="136" old-status="Finished" status="Queued" step="71"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="106" name="现场勘察">
      <actions>
        <action id="187" name="提交" view="submit">
          <results>
            <unconditional-result id="188" old-status="Finished" status="Queued" step="109"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="108" name="修改图纸或重新设计">
      <actions>
        <action id="124" name="提交" view="submit">
          <results>
            <unconditional-result id="125" old-status="Finished" status="Queued" step="109"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="109" name="图纸审核">
      <actions>
        <action id="126" name="不通过" view="nopass">
          <results>
            <unconditional-result id="127" old-status="Finished" status="Queued" step="108"/>
          </results>
        </action>
        <action id="189" name="通过" view="pass">
          <results>
            <unconditional-result id="190" old-status="Finished" status="Queued" step="76"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="154" name="工程验收">
      <actions>
        <action id="158" name="不通过" view="nopass">
          <results>
            <unconditional-result id="159" old-status="Finished" status="Queued" step="90"/>
          </results>
        </action>
        <action id="208" name="通过" view="pass">
          <results>
            <unconditional-result id="209" old-status="Finished" status="Queued" step="205"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="157" name="工程决算">
      <actions>
        <action id="221" name="提交" view="submit">
          <results>
            <unconditional-result id="222" old-status="Finished" status="Queued" step="218"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="164" name="工程部装表">
      <actions>
        <action id="171" name="提交" view="submit">
          <results>
            <unconditional-result id="172" old-status="Finished" status="Queued" step="165"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="165" name="装表现场审核">
      <actions>
        <action id="175" name="不通过" view="nopass">
          <results>
            <unconditional-result id="176" old-status="Finished" status="Queued" step="164"/>
          </results>
        </action>
        <action id="177" name="通过" view="pass">
          <results>
            <unconditional-result id="178" old-status="Finished" status="Queued" step="96"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="167" name="水表立户">
      <actions>
        <action id="181" name="提交" view="submit">
          <results>
            <unconditional-result id="182" old-status="Finished" status="Queued" step="168"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="168" name="所有资料归档">
      <actions>
        <action id="228" name="提交" view="submit">
          <results>
            <unconditional-result id="229" old-status="Finished" status="Queued" step="225"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="193" name="工程部制定任务单">
      <actions>
        <action id="197" name="提交" view="submit">
          <results>
            <unconditional-result id="198" old-status="Finished" status="Queued" step="194"/>
          </results>
        </action>
        <action id="201" name="提交" view="submit">
          <results>
            <unconditional-result id="202" old-status="Finished" status="Queued" step="194"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="194" name="项目部制定施工单">
      <actions>
        <action id="203" name="提交" view="submit">
          <results>
            <unconditional-result id="204" old-status="Finished" status="Queued" step="90"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="205" name="资料上交">
      <actions>
        <action id="210" name="提交" view="submit">
          <results>
            <unconditional-result id="211" old-status="Finished" status="Queued" step="206"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="206" name="工程部审核">
      <actions>
        <action id="212" name="通过" view="pass">
          <results>
            <unconditional-result id="213" old-status="Finished" status="Queued" step="207"/>
          </results>
        </action>
        <action id="214" name="不通过" view="nopass">
          <results>
            <unconditional-result id="215" old-status="Finished" status="Queued" step="205"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="207" name="财务部财务统计">
      <actions>
        <action id="216" name="提交" view="submit">
          <results>
            <unconditional-result id="217" old-status="Finished" status="Queued" step="157"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="218" name="决算审核">
      <actions>
        <action id="223" name="不通过" view="nopass">
          <results>
            <unconditional-result id="224" old-status="Finished" status="Queued" step="157"/>
          </results>
        </action>
        <action id="233" name="通过" view="pass">
          <results>
            <unconditional-result id="234" old-status="Finished" status="Queued" step="232"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="225" name="资料移交">
      <actions>
        <action id="226" name="完成" view="complete">
          <results>
            <unconditional-result id="227" old-status="Finished" status="Queued" step="99"/>
          </results>
        </action>
      </actions>
    </step>
    <step id="232" name="通知用户结算">
      <actions>
        <action id="237" name="提交" view="submit">
          <results>
            <unconditional-result id="238" old-status="Finished" status="Queued" step="93"/>
          </results>
        </action>
      </actions>
    </step>
  </steps>
</workflow>

 

 

你可能感兴趣的:(java,xml,workflow,J#)