XML文件解析

//解析xml文件
public static void analyseXmlFile(String filepath){
System.out.println("************ ANALYSE START ************");
System.out.println(filepath);
System.out.println("***************************************");

File xmlfile = new File(filepath);
if(xmlfile.exists()){
Document doc = XmlSaxReaderTest.getStringXMLDoc(xmlfile);
if(doc!=null){
Element root = doc.getDocumentElement();
NodeList list = root==null?null:root.getElementsByTagName("element");
if(list!=null){
int len = list.getLength();
if(len>0){
for(int i=0;i<len;i++){
Element e = (Element)list.item(i);
// dealWithElement(e, i);//解析并插入到数据库
printElementContent(e);//纯打印解析内容至控制台
}
System.out.println("********* "+filepath.substring(filepath.lastIndexOf('\\')+1)+" 总共完成 "+len+" 条记录*********");
} 
}
}
} else {
System.out.println("文件不存在--异常!");
}

System.out.println("************* ANALYSE END *************");
}

//打印解析内容
public static void printElementContent(Element e){
String eid = e.getAttribute("id");
Element title = (Element)e.getElementsByTagName("title").item(0);
Element subject = (Element)e.getElementsByTagName("subject").item(0);
Element content = (Element)e.getElementsByTagName("content").item(0);
System.out.println("element:id="+eid+"******************************************");
System.err.println(title.getNodeName()+":"+title.getAttribute("descripton")+":"+title.getFirstChild().getNodeValue());
System.err.println(subject.getNodeName()+":"+subject.getAttribute("descripton")+":"+subject.getFirstChild().getNodeValue());


NodeList attrs = e.getElementsByTagName("attr");
if(attrs!=null && attrs.getLength()>0){
int attr_len = attrs.getLength();
for(int j=0;j<attr_len;j++){
Element attr = (Element)attrs.item(j);
System.err.println(attr.getAttribute("name")+":"+attr.getAttribute("description")+":"+printNodeValue(attr.getFirstChild()));
}
}
System.err.println(content.getNodeName()+":"+content.getAttribute("description")+":"+printNodeValue(content.getFirstChild()));
//替换正文区的图片地址
String contentValue = printNodeValue(content.getFirstChild());
Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
Matcher m = p.matcher(contentValue.toLowerCase());
while (m.find()) {
String srcpath = m.group(1);
String forepath = srcpath.substring(0, srcpath.lastIndexOf('/')+1);
contentValue = contentValue.replace(forepath, "");
// System.out.println(m.group(1));
}

//处理正文内部文件链接
//如:<a href="/n2669700/n5326028/n5326101/n5328096/n5328138/n6046427.files/n6046428.doc" 
//name="attachment" target="_blank">B超中标.doc</a>
Pattern p1 = Pattern.compile("<a\\s.*?href=\"([^\"]+)\"[^>]*>(.*?)</a>"); 
Matcher m1 = p1.matcher(contentValue);
String attaSaveUrl = "C:/IMPORT_ATTA/" + DateTools.formatDate(new Date(), "yyyyMMdd") + "/";
System.out.println("*****正文附件链接*****");
while(m1.find()){
String srcstr = m1.group(1);
String url = "servlet/FileDownload?filepath="+attaSaveUrl+srcstr.substring(srcstr.lastIndexOf('/')+1)+"&dispname="+m1.group(2);
contentValue = contentValue.replace(srcstr, url);
System.out.println(eid+":"+srcstr+" --> "+url);
// System.out.println("("+eid+"):"+m1.group(1)+" --> "+m1.group(2));
}


System.out.println(contentValue);

//图片区
System.out.println("*****图片区*****");
Element images = (Element)e.getElementsByTagName("images").item(0);
NodeList imglist = images.getElementsByTagName("image");
if(imglist!=null && imglist.getLength()>0){
for(int j=0;j<imglist.getLength();j++){
System.out.print(j+1+":");
Element tmp = (Element)imglist.item(j);
if(tmp!=null){
System.out.print(tmp.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
}
System.out.println();
}
} else {
System.out.println("("+eid+")没有图片文件!");
}

//附件区
System.out.println("*****附件区*****");
Element attas = (Element)e.getElementsByTagName("appendixes").item(0);
NodeList attalist = attas.getElementsByTagName("appendix");
if(attalist!=null && attalist.getLength()>0){
for(int j=0;j<attalist.getLength();j++){
System.out.print(j+1+":");
Element tmp = (Element)attalist.item(j);
if(tmp!=null){
System.out.print(((Element)tmp.getElementsByTagName("filename").item(0)).getAttribute("value"));
}
System.out.println();
}
} else {
System.out.println("("+eid+")没有附件文件!");
}
}

//打印节点的内容 加入null值判断
public static String printNodeValue(Node node){
return node!=null?node.getNodeValue():"null";
}


****************************************************************************************************************

xml文件结构:

****************************************************************************************************************

<?xml version="1.0" encoding="utf-8"?>
<elements>
    <requestman><![CDATA[172.16.200.7]]></requestman>
    <element id="5715329">
        <title descripton="文章标题"><![CDATA[日报社领导信息]]></title>
        <subject descripton="栏目"><![CDATA[市政府信息公开目录 > 事业单位及办事机构 > 市日报社 > 政府信息公开目录 > 组织机构 > 领导信息]]></subject>
        <attributes description="属性区">
            <attr description="文章标题" name="title"><![CDATA[日报社领导信息]]></attr>
            <attr description="文章状态" name="status"><![CDATA[审核通过]]></attr>
            <attr description="内容重要性" name="level"><![CDATA[0]]></attr>
            <attr description="作者" name="createBy"><![CDATA[whsrbs]]></attr>
            <attr description="关键字" name="keywords"><![CDATA[]]></attr>
            <attr description="摘要" name="description"><![CDATA[]]></attr>
            <attr description="发布流水号" name="publishId"><![CDATA[5716062]]></attr>
            <attr description="评论要求" name="criticaltype"><![CDATA[0]]></attr>
            <attr description="栏目" name="parentDn"><![CDATA[dc=cms,ou=subject,cn=n2669700,cn=n4022495,cn=n4050903,cn=n4050904,cn=n4050905,cn=n4050907]]></attr>
            <attr description="栏目" name="subject"><![CDATA[市政府信息公开目录 > 事业单位及办事机构 > 市日报社 > 政府信息公开目录 > 组织机构 > 领导信息]]></attr>
            <attr description="创建时间" name="creatTime"><![CDATA[20110705213511]]></attr>
            <attr description="发布时间" name="enableTime"><![CDATA[20110705213511]]></attr>
            <attr description="修改时间" name="modifyTime"><![CDATA[20110705220622]]></attr>
            <attr description="自动停止发布时间" name="disableTime"><![CDATA[20541231010101]]></attr>
            <attr description=" 最后接触时间" name="touchTime"><![CDATA[]]></attr>
            <attr description="文章来源" name="asource"/>
            <attr description="所属部门" name="department"><![CDATA[]]></attr>
            <attr description="正文字数" name="wordNum"><![CDATA[4394]]></attr>
            <attr description="标题图片个数" name="namePicNum"><![CDATA[0]]></attr>
            <attr description="图片数" name="imageNum"><![CDATA[6]]></attr>
            <attr description="附件数" name="appendixNum"><![CDATA[0]]></attr>
            <attr description="索引号" name="索引号"><![CDATA[460240064/2011-00008]]></attr>
            <attr description="副索引号" name="副索引号"><![CDATA[KAO-501-12-0002000-2011-016]]></attr>
            <attr description="名称" name="名称"><![CDATA[日报社领导信息]]></attr>
            <attr description="生成日期" name="生成日期"><![CDATA[2011-07-05]]></attr>
            <attr description="文号" name="文号"/>
            <attr description="发布机构" name="发布机构"><![CDATA[WHS0050]]></attr>
            <attr description="主题分类" name="主题分类"><![CDATA[12C]]></attr>
            <attr description="体裁分类" name="体裁分类"><![CDATA[themecat]]></attr>
            <attr description="组配分类" name="组配分类"><![CDATA[svobjcat]]></attr>
            <attr description="信息关键词" name="信息关键词"/>
            <attr description="著录日期" name="著录日期"><![CDATA[2011-07-05]]></attr>
            <attr description="内容概述" name="内容概述"><![CDATA[日报社领导信息]]></attr>
            <attr description="生效日期" name="生效日期"><![CDATA[2011-07-05]]></attr>
            <attr description="废止日期" name="废止日期"><![CDATA[2011-07-05]]></attr>
            <attr description="相关信息" name="相关信息"/>
            <attr description="主题词" name="主题词"/>
            <attr description="语种" name="语种"/>
            <attr description="信息格式" name="信息格式"><![CDATA[HTML]]></attr>
            <attr description="在线链接地址" name="在线链接地址"/>
            <attr description="在线咨询" name="在线咨询"/>
            <attr description="状态查询" name="状态查询"/>
            <attr description="在线申报" name="在线申报"/>
        </attributes>
        <content description="正文区"><![CDATA[<p>
	 </p>
<table align="center" bgcolor="#bebebe" border="0" cellpadding="2" cellspacing="1" width="710">
	<tbody>
		<tr>
			<td align="center" bgcolor="#f0f0f0" colspan="5" height="22">
				<strong>领导简历</strong></td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" height="22" width="85">
				<strong>姓    名</strong></td>
			<td align="center" bgcolor="#ffffff" width="200">
				XXXXX</td>
			<td align="center" bgcolor="#f0f0f0" width="85">
				<strong>性    别</strong></td>
			<td align="center" bgcolor="#ffffff" width="200">
				男</td>
			<td align="center" bgcolor="#ffffff" rowspan="5" width="130">
				<p align="center">
					<img alt="" border="0" src="/n2669700/n4022495/n4050903/n4050904/n4050905/n4050907/n5715329.files/n5715941_small.jpg" /></p>
			</td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" height="22">
				<strong>出生年月</strong></td>
			<td align="center" bgcolor="#ffffff">
				1961年9月</td>
			<td align="center" bgcolor="#f0f0f0" width="85">
				<strong>民    族</strong></td>
			<td align="center" bgcolor="#ffffff">
				汉族</td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" height="22" valign="top">
				<strong>籍    贯</strong></td>
			<td align="left" bgcolor="#ffffff" colspan="3" valign="top">
				XXXXX</td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" height="22">
				<strong>职    务</strong></td>
			<td align="left" bgcolor="#ffffff" colspan="3">
				党委书记、社长</td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" height="22" valign="top">
				<strong>学历背景</strong></td>
			<td align="left" bgcolor="#ffffff" colspan="3" valign="top">
				<p>
					研究生学历,历史学学士学位</p>
			</td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" height="22" valign="top">
				<strong>工作经历</strong></td>
			<td align="left" bgcolor="#ffffff" colspan="4" valign="top">
				<p align="left">
					调入日报社任党委书记、社长。</p>
			</td>
		</tr>
		<tr>
			<td align="center" bgcolor="#f0f0f0" colspan="5" height="22" valign="top">
				<strong>工作分工</strong></td>
		</tr>
		<tr>
			<td align="left" bgcolor="#ffffff" colspan="5" height="22" valign="top">
				<table align="left" border="0" cellpadding="5" cellspacing="0" width="95%">
					<tbody>
						<tr>
							<td>
								主持日报社全面工作。</td>
						</tr>
					</tbody>
				</table>
			</td>
		</tr>
	</tbody>
</table>
		<tr>
		<td colspan="4"><!-- 红头文件结束 -->
 
		<div style="width: 100%; overflow: auto; margin: 0 auto; overflow-y: hidden;">
		<p style="margin-left: 80px">
	<a href="/n2669700/n5326028/n5326101/n5328096/n5328138/n6046427.files/n6046428.doc" name="attachment" target="_blank">B超中标.doc</a></p>
 
		</div>
		</td>
	</tr>

	</tbody>
</table>
]]></content>
        <images description="图片区">
            <image>
                <description><![CDATA[n5715941_small.jpg]]></description>
                <filename value="n5715941_small.jpg"/>
                <type value="jpg"/>
            </image>
            <image>
                <description><![CDATA[n5715970_small.jpg]]></description>
                <filename value="n5715970_small.jpg"/>
                <type value="jpg"/>
            </image>
        </images>
        <appendixes description="附件区">
			<appendix>
                <displayname><![CDATA[n5593792]]></displayname>
                <filename value="n5593792.doc"/>
                <type value="doc"/>
            </appendix>
            <appendix>
                <displayname><![CDATA[n5593793]]></displayname>
                <filename value="n5593793.xls"/>
                <type value="xls"/>
            </appendix>
   		</appendixes>
    </element>
</elements>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值