2012-11-28 3 views

답변

0

공공 무효 문자 (문자는 [] CH, INT의 시작은 INT 길이)의 SAXException {
을 발생하는 경우 (preTag! = NULL) {
문자열 콘텐츠 = 새로운 문자열 (CH , 시작, 길이);
if ("name". equals (preTag)) {
student.setName (content);
} else if ("age". equals (preTag)) {
student.setAge (Float.parseFloat (content)); 당신이 DOM에 대해 SAX를 묻는 것 같은
}
은}
}

+0

이 값은 신뢰할 수 없습니다. 많은 SAX 퍼서 구현은 특히 텍스트에 문자 엔티티 참조가 포함되어있는 경우 문자 데이터를 여러 이벤트로 분할합니다. 예를 들어 "a & b"는 "a & b"로 인코딩되며 대부분의 경우 세 개의 문자() 이벤트 ("a", "&", "b")로 전송됩니다. –

2

것 같습니다. DOM으로, 당신은 같은 것을 할 것 :

private String getChildNodeContent(Element parentNode, String tagName) { 
    NodeList children = parentNode.getElementsByTagName(tagName); 
    return children.length == 0 ? null : children.item(0).getTextContent(); 
} 
... 
Element student = (Element) childNodeList.item(k); 
String name = getChildNodeContent(student, "name"); 
String grade = getChildNodeContent(student, "grade"); 
String age = getChildNodeContent(student, "age"); 

는 SAX에서, 당신은)의 startElement()와하는 endElement을 (보고 당신이 걱정 시작/끝 태그 및 캡처 문자를 찾아 가야() 텍스트를 캡처하는 이벤트.

private StringBuilder buffer = new StringBuilder(); 

public void startElement(String uri, String lname, String qname, Attributes atts) { 
    buffer.setLength(0); 
} 

public void endElement(String uri, String lname, String qname) { 
    String tag = context.pop(); 
    String contents = buffer.toString(); 
    // Do something interesting with 'contents' for the given tag. 
    buffer.setLength(0); 
} 

public void characters(char[] ch, int start, int len) { 
    buffer.append(ch, start, len); 
} 
관련 문제