Purpose: Parse the XML file and save it to MySQL, and the fields to be parsed can correspond to each other. Here is the microblog file that you want to use inside the article and person_id fields.
Ideas:
In order to get person_id and article can correspond. Therefore, the two fields are parsed separately, and a private variable CT is defined, which automatically adds 1 to the overloaded function startelement. This CT is inserted into MySQL as article and Person_ The primary key of the ID is (ct,article) and (ct,person_id), after inserting two different tables A and B, two tables do the connection operation, achieve the article and person_id one by one correspondence (curve salvation AH!!!)
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_content3"), reader);
} catch (Exception e) {
E.printstacktrace ();
}
SYSTEM.OUT.PRINTLN (int) ' OP ');
System.out.println ("Run Time:" + (System.currenttimemillis ()-lasting)
+ "milliseconds");
}
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;
The following program writes the result of parsing XML to a file
File File = new file ("/home/hadoop/weibo_content_corpus", "addfile.txt");
if (!file.exists ())
{
try {
File.createnewfile (); Create a file
} 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 (); Create a file
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Writing content to a file (output stream)
String str = "Dug 1\n out of Java";
byte bt[] = new byte[1024];
BT = Str.getbytes ();
/* Try {
Open a write file, the second parameter in the constructor true indicates that the file is written in append form
FileWriter writer = new FileWriter ("/home/hadoop/weibo_content_corpus/addfile.txt", true);
Writer.write (str);
Writer.close ();
} catch (IOException e) {
E.printstacktrace ();
}*/
The above program writes the result of parsing XML to a file
if (Tag.equals ("article")) {
System.out.println ("article:");
System.out.println (New String (CH, start, length));
ch1= "INSERT INTO db (" +ct+ "," + "\" "" +new String (CH, start, length) + "\" "+"); "; /Generate scripts to import MySQL
BT = Ch1.getbytes ();
try {
Open a write file, the second parameter in the constructor true indicates that the file is written in append form
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);
BT = Ch1.getbytes ();
try {
Open a write file, the second parameter in the constructor true indicates that the file is written in append form
FileWriter writer = new FileWriter ("/home/hadoop/weibo_content_corpus/add_id.txt", true);
Writer.write (CH1);
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 parsing XML using sax