findbook
로직순서
findbook클래스에서 검색할 엘리먼트 및 검색어를 선택, 파싱
findbookhandler
difaulthandler 상속
생성자로 검색할 엘리먼트,검색어 입력 및 해쉬태이블생성
//검색 엘리먼트
this.condition = condition;
//검색 문자열
this.keyWord = keyWord;
//검색 내용을 저장할 Hashtable 객체
this.hash = new Hashtable();
startelement에서 검색할 엘리먼트 찾기
if(qName.equals("book")) {
isBook = true;
String isbn = attributes.getValue("ISBN");
hash.put("ISBN", isbn);
} else if(qName.equals("Title")) {
isTitle = true;
} else if(qName.equals("Author")) {
isAuthor = true;
} else if(qName.equals("FixedPrice")) {
isPrice = true;
}
characters에서 검색할 엘리먼트와 그에 해당하는 문자열 해쉬태이블에 입력
String str = new String(ch,start,length).trim();
if(isTitle) {
hash.put("Title", str);
isTitle = false;
} else if(isAuthor) {
hash.put("Author", str);
isAuthor = false;
} else if(isPrice) {
hash.put("FixedPrice", str);
isPrice = false;
}
endelement에서 검색할 엘리먼트에해당하는 문자열이 검색되면 북정보출력
if(qName.equals("book")) {
if(condition.equals("ISBN")) {
String isbn = (String) hash.get("ISBN");
if(isbn.indexOf(keyWord)!=-1) displayBookInfo();
} else if(condition.equals("Title")) {
String title = (String) hash.get("Title");
if(title.indexOf(keyWord)!=-1) displayBookInfo();
} else if(condition.equals("Author")) {
String author = (String) hash.get("Author");
if(author.indexOf(keyWord)!=-1) displayBookInfo();
}
hash.clear();
isBook = false;
}
출력~
System.out.println("Title: " + (String) hash.get("Title"));
System.out.println("Author: " + (String) hash.get("Author"));
System.out.println("Price: " + (String) hash.get("FixedPrice"));
System.out.println("----------------------");
애러처리
public void warning(SAXParseException exception) throws SAXException {
throw new SAXException("warning 이벤트 처리");
}
public void error(SAXParseException exception) throws SAXException {
System.out.println("DTD 또는 XML Schema 문서 구조에 위배됩니다.");
System.out.println("유효하지 않는 문서 입니다.");
throw new SAXException("error 이벤트 처리");
}
public void fatalError(SAXParseException exception) throw
SAXException {
System.out.println("XML 권고안의 내용을 지키지 않았습니다.");
System.out.println("잘 짜여진 XML 문서가 아닙니다.");
throw new SAXException("fatalError 이벤트 처리");
}
==================================================================================
- findByXPath
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
new InputSouce("aaa/aaa.xml");
id로 내용찾기
String title=xpath.evaluate("/aaa/aaa/aaa[../@id='aa']",inputSource);
id로 엘리먼트찾기
Node node=(Node)xpath.evaluate("/aaa/aaa/aaa[@id='aa']",inputSource,XPathConstants.NODE);
해당 엘레먼트갯수 검색
Double no = (Double)xpath.evaluate("count(/aaa/aaa/aaa)",inputSource,XPathConstants.NUMBER);
엘레먼트들 검색
NodeList nl = (NodList)xpath.evaluate("/aaa/aaa/aaa",inputSource,XPathConstants.NODESET);
/books/book[@id='1']/@kind
로직순서
findbook클래스에서 검색할 엘리먼트 및 검색어를 선택, 파싱
findbookhandler
difaulthandler 상속
생성자로 검색할 엘리먼트,검색어 입력 및 해쉬태이블생성
//검색 엘리먼트
this.condition = condition;
//검색 문자열
this.keyWord = keyWord;
//검색 내용을 저장할 Hashtable 객체
this.hash = new Hashtable();
startelement에서 검색할 엘리먼트 찾기
if(qName.equals("book")) {
isBook = true;
String isbn = attributes.getValue("ISBN");
hash.put("ISBN", isbn);
} else if(qName.equals("Title")) {
isTitle = true;
} else if(qName.equals("Author")) {
isAuthor = true;
} else if(qName.equals("FixedPrice")) {
isPrice = true;
}
characters에서 검색할 엘리먼트와 그에 해당하는 문자열 해쉬태이블에 입력
String str = new String(ch,start,length).trim();
if(isTitle) {
hash.put("Title", str);
isTitle = false;
} else if(isAuthor) {
hash.put("Author", str);
isAuthor = false;
} else if(isPrice) {
hash.put("FixedPrice", str);
isPrice = false;
}
endelement에서 검색할 엘리먼트에해당하는 문자열이 검색되면 북정보출력
if(qName.equals("book")) {
if(condition.equals("ISBN")) {
String isbn = (String) hash.get("ISBN");
if(isbn.indexOf(keyWord)!=-1) displayBookInfo();
} else if(condition.equals("Title")) {
String title = (String) hash.get("Title");
if(title.indexOf(keyWord)!=-1) displayBookInfo();
} else if(condition.equals("Author")) {
String author = (String) hash.get("Author");
if(author.indexOf(keyWord)!=-1) displayBookInfo();
}
hash.clear();
isBook = false;
}
출력~
System.out.println("Title: " + (String) hash.get("Title"));
System.out.println("Author: " + (String) hash.get("Author"));
System.out.println("Price: " + (String) hash.get("FixedPrice"));
System.out.println("----------------------");
애러처리
public void warning(SAXParseException exception) throws SAXException {
throw new SAXException("warning 이벤트 처리");
}
public void error(SAXParseException exception) throws SAXException {
System.out.println("DTD 또는 XML Schema 문서 구조에 위배됩니다.");
System.out.println("유효하지 않는 문서 입니다.");
throw new SAXException("error 이벤트 처리");
}
public void fatalError(SAXParseException exception) throw
SAXException {
System.out.println("XML 권고안의 내용을 지키지 않았습니다.");
System.out.println("잘 짜여진 XML 문서가 아닙니다.");
throw new SAXException("fatalError 이벤트 처리");
}
==================================================================================
- findByXPath
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
new InputSouce("aaa/aaa.xml");
id로 내용찾기
String title=xpath.evaluate("/aaa/aaa/aaa[../@id='aa']",inputSource);
id로 엘리먼트찾기
Node node=(Node)xpath.evaluate("/aaa/aaa/aaa[@id='aa']",inputSource,XPathConstants.NODE);
해당 엘레먼트갯수 검색
Double no = (Double)xpath.evaluate("count(/aaa/aaa/aaa)",inputSource,XPathConstants.NUMBER);
엘레먼트들 검색
NodeList nl = (NodList)xpath.evaluate("/aaa/aaa/aaa",inputSource,XPathConstants.NODESET);
/books/book[@id='1']/@kind

카테고리