用javaDOM解析xml文档:
package demo;
import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class DemoXml{
public static void main(String[] args){
long lasting =System.currentTimeMillis();
try{
File f = new File("shiyan.txt.xml");//文档目录
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("Event");
for (int i=0;i<nl.getLength();i++){
try { //利用 try catch 防止出现错误
//下面拿取 Event下的每一个节点的值 System.out.print(doc.getElementsByTagName("Participant")
.item(i).getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("Time").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("Location")
.item(i).getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("Denoter")
.item(i).getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("Time").item(i)
.getFirstChild().getNodeValue());
System.out.println(doc.getElementsByTagName("Object")
.item(i).getFirstChild().getNodeValue());
} catch (Exception e) {
// System.out.println("");
}
}
}catch(Exception e){
e.printStackTrace();
}
}
}
XML:
<?xml version="1.0" encoding="GB2312" ?>
<Body>
<Title>成都网友称震感强烈 女同事当即哭泣</Title>
<ReportTime type="absTime">2008年05月12日16:15</ReportTime>
<Content>
<Paragraph>
<Sentence>
<Event eid="e1"><Time type="relTime" tid="t1">5月12日14时28分</Time>,<Location lid="l1">四川</Location>发生7.8级<Denoter type="emergency" did="d1">地震</Denoter>。</Event>
</Sentence>
</Paragraph>
<Paragraph>
<Sentence>
<Event eid="e2"><Time type="absTime" tid="t2">15时50分</Time>,新民网<Participant sid="s1">记者</Participant>网上<Denoter type="action" did="d2">连线</Denoter>成都网友<Participant oid="o2">姚先生</Participant></Event>。
</Sentence>
<Sentence>
<Event eid="e3"><Participant sid="s3,s4,s6,s7,s8,s9,s10">姚先生</Participant><Time type="relTime" tid="t3">当时</Time>正在<Location lid="l3">成都市武侯区的办公室</Location><Denoter type="action" did="d3">打电话</Denoter>,</Event>
<Event eid="e4" type="thoughtevent" >突然<Denoter type="statement" did="d4">感觉</Denoter></Event>
<Event eid="e5"><Object oid="o5">电脑显示器</Object><Denoter type="action" did="d5">晃</Denoter>得很厉害</Event>,
<Event eid="e6">便<Denoter type="action" did="d6">丢下</Denoter><Object oid="o6">电话</Object></Event>、
<Event eid="e7"><Denoter type="action" did="d7">抓起</Denoter><Object oid="o7">手机</Object>和<Object oid="o7">钱包</Object></Event>
<Event eid="e8">往外<Denoter type="action" did="d8">跑</Denoter>,</Event>
<Event eid="e9">手里还<Denoter type="action" did="d9">拿</Denoter>着<Object oid="o9">无绳电话子机</Object>,</Event>
<Event type="thoughtevent" eid="e10">准备给远在江苏的<Participant type="recipient" oid="o10">家人</Participant><Denoter type="action" did="d10">打电话</Denoter>。</Event>
</Sentence>
</Paragraph>
<Paragraph>
<Sentence>
<Event eid="e11" type="thoughtevent"><Participant type="agent" sid="s11">姚先生</Participant><Denoter type="statement" did="d11">说</Denoter></Event>
<Event eid="e12">坐在他旁边的<Participant type="agent" sid="s12,s13">女同事</Participant>立刻<Denoter type="perception" did="d12">感觉</Denoter>是在地震,</Event>
<Event eid="e13">一下子<Denoter type="action" did="d13">哭</Denoter>了起来。</Event>
</Sentence>
<Sentence>
<Event eid="e14" type="thoughtevent">据<Participant sid="s14">姚先生</Participant><Denoter type="statement" did="d14">介绍</Denoter>,</Event>
<Event eid="e15">成都市很多钢结构的<Object oid="o15">房屋</Object>都有明显<Denoter type="emergency" did="d15">震感</Denoter>,</Event>
<Event eid="e16">当<Participant oid="o16">他</Participant>和<Participant>很多市民</Participant>一样<Denoter type="operation" did="d16">疏散</Denoter>到<Location type="destination" lid="l16">室外</Location>时,</Event>
<Event eid="e17" type="thoughtevent">随即<Denoter type="perception" did="d17">发现</Denoter></Event>
<Event eid="e18"><Object oid="o18">手机信号</Object>已经<Denoter type="stateChange" did="d18">中断</Denoter></Event>,据称基站坏了。
</Sentence>
<Sentence>
<Event eid="e19">疏散出来的人群中,<Participant sid="s19">大家</Participant>都在<Denoter type="action" did="d19">谈论</Denoter>唐山大地震。</Event>
</Sentence>
</Paragraph>
<Paragraph>
<Sentence>
<Event eid="e20" type="thoughtevent"><Participant sid="s20">姚先生</Participant>向<Participant oid="o20">新民网记者</Participant><Denoter type="perception" did="d20">称</Denoter></Event>,
<Event eid="e21" type="thoughtevent">据<Participant sid="s21">一些成都市民</Participant><Denoter type="perception" did="d21">说</Denoter>,</Event>
<Event eid="e22">成都的<Object oid="o22">伊藤洋华堂武侯店</Object><Denoter type="stateChange" did="d22">倒塌</Denoter>。</Event>
</Sentence>
</Paragraph>
</Content>
<eRelation thoughtcontent_eids="e5" thoughtevent_eid="e4" relType="Thoughtcontent"/>
<eRelation thoughtcontent_eids="e12-e13" thoughtevent_eid="e11" relType="Thoughtcontent"/>
<eRelation thoughtcontent_eids="e15-e16" thoughtevent_eid="e14" relType="Thoughtcontent"/>
<eRelation thoughtcontent_eids="e18" thoughtevent_eid="e17" relType="Thoughtcontent"/>
<eRelation thoughtcontent_eids="e21-e22" thoughtevent_eid="e20" relType="Thoughtcontent"/>
<eRelation thoughtcontent_eids="e22" thoughtevent_eid="e21" relType="Thoughtcontent"/>
<eRelation relType="Causal" cause_eid="e1" effect_eid="e5"/>
<eRelation relType="Causal" cause_eid="e12" effect_eid="e13"/>
<eRelation relType="Causal" cause_eid="e1" effect_eid="e15"/>
<eRelation relType="Causal" cause_eid="e1" effect_eid="e18"/>
<eRelation relType="Causal" cause_eid="e1" effect_eid="e22"/>
</Body>