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官方的解析方式, ...
随机推荐
- SVM经典论文
1. P. H. Chen, C. J. Lin, and B. Schölkopf, A tutorial on ν-support vector machines, Appl. Stoch. Mo ...
- 关于phpmyadmin的小笔记
默认情况下,phpmyadmin联系的是localhost. 如果此时hostname不是localhost而是其它什么的话,在phpmyadmin是不能连接上的,虽然在命令行mysql -h loc ...
- CSS之viewport 2
在这个迷你系列的文章里边我将会解释viewport,以及许多重要元素的宽度是如何工作的,比如<html>元素,也包括窗口和屏幕. 这篇文章我们来聊聊关于移动浏览器的内容.如果你对移动开发完 ...
- spring boot + gradle[草稿]
入门文档:https://github.com/qibaoguang/Spring-Boot-Reference-Guide 安装gradle 官方下载 https://gradle.org/grad ...
- CMake undefined reference to `QTcpServer::QTcpServer(QObject*)'的解决
1. 这是因为工程link不到network的库,因此除了要包含头文件 #include 之外,还需要在.pro文件中加入: QT += network 2. 对于CMake,需添加Net ...
- Python开发【第九章】:堡垒机实例
一.堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作 模块安装 C:\Program Files\Python 3.5\Scri ...
- docker好文收藏
深入浅出Docker(一):Docker核心技术预览 2. 核心技术预览 Docker核心是一个操作系统级虚拟化方法, 理解起来可能并不像VM那样直观.我们从虚拟化方法的四个方面:隔离性.可配额/可度 ...
- gulp任务
项目使用的gulp自动化任务 //定义输出文件夹名称 var distFolderH5 = "distH5"; var distFolderMofang = "distM ...
- android开发中的问题总结(一)
1.设置sdk路径 window->preferences->android->SDK location 中进行修改,如果出现红叉,就说明不正确,选择的路径必须包含tools的那个目 ...
- Android -- 常见控件的小效果
1,EditText控件 ① 修改光标颜色 自定义drawable 创建cursor.xml文件 <?xml version="1.0" encoding="utf ...