java使用sax解析xml
目的:解析xml文件,并存入mysql,并且要解析的字段能一一对应.这里解析的是微博的文件,想要利用里面的article和person_id字段.
思路:
为了能得到person_id和article能一一对应.因此对两个字段分别解析,并且定义一个私有变量ct,在重载的函数startElement中自动加1.这个ct作为插入mysql中的article和person_id的主键即(ct,article)和(ct,person_id),在分别插入两张不同的表a和b之后,两个表做连接操作,实现article和person_id的一一对应(曲线救国啊!!!)
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class sax_parse_xml extends DefaultHandler {
java.util.Stack tags = new java.util.Stack();
private long ct=0;
public static boolean isLegalXMLCharacter(int ch) {
if (ch <= 0xD7FF) {
if(ch<=0x0){return false;}
if (ch >= 0x20) {
return true;
} else {
return ch == '\n' || ch == '\r' || ch == '\t';
}
}
else{
return (ch >= 0xE000 && ch <= 0xFFFD) || (ch >= 0x10000 && ch <= 0x10FFFF);
}
}
public sax_parse_xml() {
super();
}
public static void main(String args[]) {
long lasting = System.currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
sax_parse_xml reader = new sax_parse_xml();
sp.parse(new InputSource("/home/hadoop/weibo_content_corpus/nlpir_weibo_content"), reader);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println((int)'运');
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
+ "毫秒");
}
public void characters(char ch[], int start, int length)
throws SAXException {
String tag = (String) tags.peek();
String ch1 = "";
String ch2="";
//System.out.print(ch.length);
//long ct=0;
//下面的程序向文件写入解析的xml的结果
File file = new File("/home/hadoop/weibo_content_corpus", "addfile.txt");
if(!file.exists())
{
try {
file.createNewFile(); // 创建文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
File file1 = new File("/home/hadoop/weibo_content_corpus", "add_id.txt");
if(!file1.exists())
{
try {
file1.createNewFile(); // 创建文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 向文件写入内容(输出流)
String str = "java外挖出1\n";
byte bt[] = new byte[1024];
bt = str.getBytes();
/* try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter("/home/hadoop/weibo_content_corpus/addfile.txt", true);
writer.write(str);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}*/
//上面的程序向文件写入解析的xml的结果
if (tag.equals("article")) {
System.out.println("article:");
String tmpStr=new String(ch, start, length);
if(tmpStr.trim().length()>0)
{
//System.out.println(new String(ch, start, length));
ch1="insert into tb_xml_article_hd1 values ("+ct+","+"\""+tmpStr+"\""+");";//生成导入mysql的脚本
bt = ch1.getBytes();
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter("/home/hadoop/weibo_content_corpus/addfile.txt", true);
writer.write(ch1+"\n"+"commit;"+"\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(ch1);
// ct++;
//StringBuffer sb = new StringBuffer();
//sb.delete(0, sb.length());
/*
for (int i=start;i<length;i++)
{
if(Character.isDefined(ch[i]))//(isLegalXMLCharacter(ch[i]))
{
//System.out.println(ch[i]);
}
}*/
// System.out.println(start);
//System.out.println(length);
//sb.append(ch, start, length);
//System.out.println(ch1);
}
}
if (tag.equals("person_id")) {
//ch1=ct+":"+new String(ch, start, length);
String tmpStr=new String(ch, start, length);
if(tmpStr.trim().length()>0)
{
/*ch1="insert into tb_xml_person_hd values ("+(ct-4)+","+"\""+tmpStr+"\""+");";
bt = ch1.getBytes();
try {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter("/home/hadoop/weibo_content_corpus/add_id.txt", true);
writer.write(ch1+"\n"+"commit;"+"\n");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(ch1);
*/
}
//System.out.println("personid:");
//System.out.println( new String(ch, start, length));
// ch1=ch1+new String(ch, start, length);
//ch1=new String(ch, start, length);
// ch2=new String(ch, start, length);
// System.out.println(ch1);
}
if (tag.equals("time")) {
// System.out.println("time:");
// System.out.println(new String(ch, start, length));
// ch1=ch1+new String(ch, start, length);
//ch1.concat(new String(ch, start, length));
//System.out.println(ch1);
}
// System.out.println(ch1);
//ch1="";
}
public void startElement(String uri, String localName, String qName,
Attributes attrs) {
tags.push(qName);
ct=ct+1;
//System.out.println(ct);
}
}
java使用sax解析xml的更多相关文章
- Java用SAX解析XML
要解析的XML文件:myClass.xml <?xml version="1.0" encoding="utf-8"?> <class> ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- Java中Sax解析XML
SAX基于事件的解析,解析器在一次读取XML文件中根据读取的数据产生相应的事件,由应用程序实现相应的事件处理逻辑,即它是一种“推”的解析方式:这种解析方法速度快.占用内存少,但是它需要应用程序自己处理 ...
- 简单的java使用SAX解析xml
1.新建一个SAXTest类,继承import org.xml.sax.helpers.DefaultHandler类 package com.qiao.SrpingSource; import or ...
- java 使用SAX解析xml 文件
http://www.cnblogs.com/allenzheng/archive/2012/12/01/2797196.html 为了学习方便,忘博主勿究
- SAX解析XML笔记
关于基本操作,请参考:Java用SAX解析XML,这里不重复造轮子了,以下是个人笔记:
- 用SAX解析xml文件,java
(此文为(https://www.imooc.com/video/4482)之随笔) 1.用SAX解析xml文件大致分为三步 写了一个XML文件作为例子 (1)main方法代码如下: import j ...
- Android之SAX解析XML
一.SAX解析方法介绍 SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备. SAX解析器是一种基于事件的解析器,事件驱动 ...
- DOM&SAX解析XML
在上一篇随笔中分析了xml以及它的两种验证方式.我们有了xml,但是里面的内容要怎么才能得到呢?如果得不到的话,那么还是没用的,解析xml的方式主要有DOM跟SAX,其中DOM是W3C官方的解析方式, ...
随机推荐
- Ubuntu上安装Karma失败对策
在Ubuntu上安装Karma遇到超时 timeout 错误.Google了一下,国外的码农给了一个快捷的解决方案,实测可行,贴在这里: sudo apt-get install npm nodejs ...
- 使用二级域名访问本地localhost网站
将C:\Windows\System32\drivers\etc\hosts文件,修改如下.保存该文件时会提示没有权限,解决的方法是用管理员权限打开记事本. # localhost name reso ...
- 解决:tomcat部署时deploy location不能显示加载后的路径
项目总是报错,添了删,删了又添了N次以后,发现添加部署的时候,Deploy Location 没有值了,Deploy Location 没有值在自带的Tomcat上就无法用浏览器浏览(Open in ...
- PythonNote01_HTML标签
>头标签<head> >>位置 头标签要放在头部之间 >>种类 <title> : 指定整个网页的标题,在浏览器最上方显示. <meta&g ...
- 关于InvokeRequired与Invoke
from:http://www.th7.cn/Program/net/201306/140033.shtml Windows 窗体中的控件被绑定到特定的线程,不具备线程安全性.因此,如果从另一个线程调 ...
- spring的多个PropertyPlaceholderConfigurer实例装配的问题
1. 默认情况下,使用PropertyPlaceholderConfigurer多实例装配出现异常 在项目中尝试 在不同的spring的配置文件中分别引入相应的properties文件,这样会在spr ...
- GPS部标平台的架构设计(二) 可扩展性设计
在设计的前夕,设计人员喜欢把领导对未来业务的期望带入到设计目标当中,比如当前业务也不过是接入几千辆车,未来业务增长也不过几万台,但领导很多激情,强势要求二期平台的接入能力要达到20万台,这个要求带入到 ...
- win8.1 Framework3.5安装不上的问题
问题症状:安装的WIN8系统无法安装Framework,SQL等都有问题 解决误区:直接安装或者更新后在线安装(结果一样各种错误) 解决方法: 1.先gpedit.msc进入本地组策略管理,目录:计算 ...
- 创建支持eclipse的多模块maven项目
通过maven可以创建多个关联模块的项目(Multiple Module Projects).由一个总的模块,下面包含多个子模块(子模块还可以包含子模块). 这种maven功能能支持大型的项目构建,往 ...
- struts 国际化
国际化 1. 国际化与本地化 国际化 (Internationalization: I18N): 是程序在不做任何修改的情况下,就可以再不同的国家或地区和不同的语言环境下,按照当地的语言和格式习惯显示 ...