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官方的解析方式, ...
随机推荐
- JMeter学习-029-JMeter配置文件propertie配置项读取及应用实例
在上文中提到通过读取配置文件中的数据,以此定制JMeter Slave的脚本分发路径(默认脚本路径,即参数文件相对路径父目录). 此文,就以此为例进行实例说明. 通过阅读JMeter源码 core/s ...
- orange pi pc 体验(二)远程登录服务器
1.本人的板子是orangepi pc,安装的debian系统 2.启动完成后,默认可以用xshell登录板子的,使用nano更改/etc/apt/source.list root@OrangePI ...
- LeetCode Remove Element
原题链接在这里:https://leetcode.com/problems/remove-element/ 题目: Given an array and a value, remove all ins ...
- ca证书校验用户证书
openssl verify -CAfile ca.cer server.crt 现在很多网站和服务都使用了HTTPS进行链路加密.防止信息在传输中间节点被窃听和篡改.HTTPS的启用都需要一个CA证 ...
- multiwii 2.4配置页面中文注释
...
- Bringing the activity to foreground 将activity切换到前台
今天遇到这个问题,找了很久,网上一些解决方法不够完全.特做此记录: 经测试以下方法不能将在后台运行的activity切换到前台运行! Intent i = new Intent(); i.setCla ...
- struts---JSP界面验证码生成与验证
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- C++之路进阶——HDU1880(魔咒词典)
---恢复内容开始--- New~ 欢迎参加2016多校联合训练的同学们~ 魔咒词典 Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 3 ...
- 关于nodejs能同时接受多少个请求的问题?////zzz
关于nodejs能同时接受多少个请求的问题? 最近学习node,看了很多教程,都在赞扬nodejs的异步I/O,异步I/O的特点就是,每接收一个请求,使用异步调用处理请求,不用等待结果,可以继续运行其 ...
- operator->和operator->*
->和->*都是C++中定义的可重载的运算符,其中:->称为成员选择符(member selection),而->*称为成员指针选择符(pointer-to-member se ...