반응형
* JTidy이용하여 텍스트 추출
http://tidy.sourceforge.net 에서 개발이 진행되고 있다.
JTidy는 앤디 퀵의 Tidy를 자바로 포팅한 것이다.
public String[] getJTidyHTMLDocumentString(InputStream is){
String[] returnValue=new String[5];
//개체 생성후 설정..
Tidy tidy=new Tidy();
tidy.setQuiet(true);
//no 'Parsing X', guessed DTD or summary
tidy.setShowWarnings(false);
//ShowWarnings - however errors are always shown
tidy.setRawOut(true);
//RawOut - avoid mapping values > 127 to entities
org.w3c.dom.Document root=tidy.parseDOM(is,null);
//파팅
Element rawDoc=root.getDocumentElement();
//루트 Element를 얻어
NodeList child=rawDoc.getElementsByTagName("title");
//타이틀의 노드를 가져다가
if(child.getLength()>0){
Element titleElement=((Element)child.item(0));
Text text=(Text)titleElement.getFirstChild();
if(text!=null){
returnValue[0]=text.getData();
//Text를 얻는다
try {
returnValue[0]=new String(returnValue[0].getBytes("8859_1"),"EUC-KR");
//EUC-KR로 변환
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("HTML Title:"+returnValue[0]);
}
}
//BODY를 얻는다
child=rawDoc.getElementsByTagName("body");
if(child.getLength()>0){
returnValue[1]=getText(child.item(0));
//BODY의 모든 엘리먼트에 대해서 TEXT를 가져옮
try {
returnValue[1]=new String(returnValue[1].getBytes("8859_1"),"EUC-KR");
//EUC-KR로 변환
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("HTML Body:"+returnValue[1]);
}
return returnValue;
}
protected String getText(Node node){
NodeList children = node.getChildNodes();
//자식 노드들을 추출하여
StringBuffer sb=new StringBuffer();
//각 자식 노드들에 대해서
for(int i=0;i<children.getLength();i++){
Node child=children.item(i);
switch (child.getNodeType()){
case Node.ELEMENT_NODE:
//엘리먼트 노드일 경우 재귀적으로 getText 호출하여 TEXT 추출
sb.append(getText(child));
sb.append(" ");
break;
case Node.TEXT_NODE:
//TEXT 노드이면 String Buffer에 추가
sb.append(((Text)child).getData());
break;
}
}
return sb.toString();
}
http://tidy.sourceforge.net 에서 개발이 진행되고 있다.
JTidy는 앤디 퀵의 Tidy를 자바로 포팅한 것이다.
public String[] getJTidyHTMLDocumentString(InputStream is){
String[] returnValue=new String[5];
//개체 생성후 설정..
Tidy tidy=new Tidy();
tidy.setQuiet(true);
//no 'Parsing X', guessed DTD or summary
tidy.setShowWarnings(false);
//ShowWarnings - however errors are always shown
tidy.setRawOut(true);
//RawOut - avoid mapping values > 127 to entities
org.w3c.dom.Document root=tidy.parseDOM(is,null);
//파팅
Element rawDoc=root.getDocumentElement();
//루트 Element를 얻어
NodeList child=rawDoc.getElementsByTagName("title");
//타이틀의 노드를 가져다가
if(child.getLength()>0){
Element titleElement=((Element)child.item(0));
Text text=(Text)titleElement.getFirstChild();
if(text!=null){
returnValue[0]=text.getData();
//Text를 얻는다
try {
returnValue[0]=new String(returnValue[0].getBytes("8859_1"),"EUC-KR");
//EUC-KR로 변환
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("HTML Title:"+returnValue[0]);
}
}
//BODY를 얻는다
child=rawDoc.getElementsByTagName("body");
if(child.getLength()>0){
returnValue[1]=getText(child.item(0));
//BODY의 모든 엘리먼트에 대해서 TEXT를 가져옮
try {
returnValue[1]=new String(returnValue[1].getBytes("8859_1"),"EUC-KR");
//EUC-KR로 변환
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("HTML Body:"+returnValue[1]);
}
return returnValue;
}
protected String getText(Node node){
NodeList children = node.getChildNodes();
//자식 노드들을 추출하여
StringBuffer sb=new StringBuffer();
//각 자식 노드들에 대해서
for(int i=0;i<children.getLength();i++){
Node child=children.item(i);
switch (child.getNodeType()){
case Node.ELEMENT_NODE:
//엘리먼트 노드일 경우 재귀적으로 getText 호출하여 TEXT 추출
sb.append(getText(child));
sb.append(" ");
break;
case Node.TEXT_NODE:
//TEXT 노드이면 String Buffer에 추가
sb.append(((Text)child).getData());
break;
}
}
return sb.toString();
}
반응형