关于sax解析xml的问题,希望大家帮忙一下啊 回车、换行符等空白字符也会触发characters事件,加上ignorableWhitespace()处理一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我自己刚好写了一个 应该content那里只是触发1次characters事件,直接读取整个字符串的内容从int start到length怎么会是6次的呢??弄代码来看看把 private class TenorNoticeHandler extends DefaultHandler { private java.util.Stack tags = new java.util.Stack(); private TB_TenorNoticeBookVO tb_tenorNoticBookVO = new TB_TenorNoticeBookVO(); private ContentHandler parentHandler; public void setParentHandler(DefaultHandler parentHandler) { this.parentHandler = parentHandler; } public void startElement(String uri, String localName, String tag, Attributes attribs) throws SAXParseException { System.out.println(localName); tags.push(localName); } /* public void ignorableWhitespace (char ch[], int start, int length) throws SAXException{ System.out.println("dddddddddddddddddddddddddddddddddddddddd"); //characters(ch, start, length); } */ /** * * * @param buf * @param start * @param length * @throws SAXParseException */ public void characters(char[] buf, int start, int length) throws SAXParseException { String tag = ""; if (!tags.isEmpty()) tag = (String) tags.peek(); String temp = new String(buf, start, length); System.out.println("value=================" + temp + " length====="+temp.length()); if (tag.equals("ID")) tb_tenorNoticBookVO.setId(temp); else if (tag.equals("CHECKCODE")) tb_tenorNoticBookVO.setCheckcode(temp); else if (tag.equals("CONTENT")) tb_tenorNoticBookVO.setContent(temp); else if (tag.equals("ENDDATE")) { Timestamp timestamp = TimeUtil.getTimeOfTimeStr(temp); tb_tenorNoticBookVO.setEnddate(timestamp); }else if (tag.equals("SENDUNIT")) tb_tenorNoticBookVO.setSendunit(temp); else if (tag.equals("WFXW")) tb_tenorNoticBookVO.setWfxw(temp); else if (tag.equals("UNITADMIN")) tb_tenorNoticBookVO.setUnitadmin(temp); else if (tag.equals("ADMINSIGNDATE")){ Timestamp timestamp = TimeUtil.getTimeOfTimeStr(temp); tb_tenorNoticBookVO.setAdminsigndate(timestamp); } } /** * * @param uri * @param localName * @param tag * @throws SAXParseException */ public void endElement(String uri, String localName, String tag) throws SAXParseException { System.out.println(localName); startElementFlag=false; if (localName.equals("TENORNNOTICEBOOK")) { DataOperator dataOperator = DataOperatorFactory.getTenorNotictBookInstance(); try { //dataOperator.insertRecord(tb_tenorNoticBookVO); } catch (Exception e) { } parser.setContentHandler(parentHandler); } else { while (!tags.empty()) tags.pop(); } } } 我是用栈来保存元素的信息的,在通过characters方法去根据当前栈的最顶元素去设置值,最后再把数据插入到数据库中,但在解析<CONTENT>1.dddd2.bbbb3.cccc</CONTENT>里面有回车件的,在这里触发了5次characters方法,每次内容是1.dddd 回车 2.bbbb 回车 3.cccc这样在解析的时候就会出现问题了, 是啊,楼主能留下其他联系方式吗,msn [email protected]qq 7766284请联系我,谢谢 package core.xml;//我写的是这个 你可以参考一下 import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.helpers.*;import org.xml.sax.*;import java.util.*;/** * <p>Title: basic core</p> * <p>Description: zealvampire</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: zealvampire</p> * @author zeal * @version 1.0 */public class simpleSaxParser extends DefaultHandler { private String name=null; private StringBuffer value=null; private HashMap hMap=null; public simpleSaxParser() { } public void startDocument( ) throws SAXException { System.out.println("---SAX Event: START DOCUMENT---"); this.hMap=new HashMap(); this.value=new StringBuffer(); } public void endDocument( ) throws SAXException { System.out.println("---SAX Event: END DOCUMENT---"); } public void startElement( String namespaceURI,String localName,String qName,Attributes attr ) throws SAXException { System.out.println("SAX event:startElement["+localName+"]"); if(localName.equals("param")){ this.value.setLength(0); this.name = attr.getValue("name"); } } public void endElement( String namespaceURI,String localName,String qName ) throws SAXException { System.out.println("sax event:endElement["+localName+"]"); if(localName.equals("param")){ this.hMap.put(this.name,this.value.toString().trim()); this.value.setLength(0); } } public void characters( char[] ch, int start, int length ) throws SAXException{ this.value.append(ch,start,length); } private void printError(SAXParseException exception){ System.out.println("\t行:\t" + exception.getLineNumber()); System.out.println("\t列:\t" + exception.getColumnNumber()); System.out.println("\t错误信息:\t" + exception.getMessage()); System.out.println("-------------------"); } public void warning( SAXParseException exception ) { System.out.println("------WARNING------"); this.printError(exception); } public void error( SAXParseException exception ) throws SAXException{ System.out.println("------ ERROR ------"); this.printError(exception); } public void fatalError( SAXParseException exception ) throws SAXException { System.out.println("------ FATAL ERROR ------"); this.printError(exception); } public HashMap getResult(){ return this.hMap; } } <?xml version="1.0" encoding="utf-8"?><parameter><sys> <param name="INITIAL_CONTEXT_FACTORY">org.jnp.interfaces.NamingContextFactory</param> <param name="PROVIDER_URL">localhost:1099</param> <param name="URL_PKG_PREFIXES">org.jboss.naming:org.jnp.interfaces</param> <param name="SECURITY_PRINCIPAL">test_user</param> <param name="SECURITY_CREDENTIALS">test_pass</param> <param name="名字">1.ddd 2.ddd3.ccc</param></sys></parameter> 一样的,我用你的代码测了,同样是触发了好几次,都是把1.ddd 2.ddd .....分开的 swt 的图形缩放如何实现抗锯齿? 求SCJP复习资料 电子版...最好是chm,能拷贝 提问 在线等 把Connetion关闭了,在Connetion上创建的Statemt和ResulSet会自动关闭吗? 网站根目录的路径问起,高手解答....... 求书 100分求JSP+JAVABEAN+SQLSERVER的BBS!!!马上给!!! 关于多线程的简单问题!! 請幫忙看一下,把一個文件,或者文件來轉換成xxx.rar的問題 急急急 请教一个关于汉洛塔的问题。 求助java日期格式转换问题 关于Image装载问题,请高手帮忙 #求JFC 核心编程的电子版本
怎么会是6次的呢??
弄代码来看看把
private java.util.Stack tags = new java.util.Stack();
private TB_TenorNoticeBookVO tb_tenorNoticBookVO = new TB_TenorNoticeBookVO();
private ContentHandler parentHandler;
public void setParentHandler(DefaultHandler parentHandler) {
this.parentHandler = parentHandler;
} public void startElement(String uri, String localName, String tag,
Attributes attribs)
throws SAXParseException {
System.out.println(localName);
tags.push(localName);
} /* public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException{
System.out.println("dddddddddddddddddddddddddddddddddddddddd");
//characters(ch, start, length);
} */
/**
*
*
* @param buf
* @param start
* @param length
* @throws SAXParseException
*/
public void characters(char[] buf, int start, int length)
throws SAXParseException { String tag = "";
if (!tags.isEmpty())
tag = (String) tags.peek(); String temp = new String(buf, start, length);
System.out.println("value=================" + temp + " length====="+temp.length());
if (tag.equals("ID"))
tb_tenorNoticBookVO.setId(temp);
else if (tag.equals("CHECKCODE"))
tb_tenorNoticBookVO.setCheckcode(temp);
else if (tag.equals("CONTENT"))
tb_tenorNoticBookVO.setContent(temp);
else if (tag.equals("ENDDATE")) {
Timestamp timestamp = TimeUtil.getTimeOfTimeStr(temp);
tb_tenorNoticBookVO.setEnddate(timestamp);
}else if (tag.equals("SENDUNIT"))
tb_tenorNoticBookVO.setSendunit(temp);
else if (tag.equals("WFXW"))
tb_tenorNoticBookVO.setWfxw(temp);
else if (tag.equals("UNITADMIN"))
tb_tenorNoticBookVO.setUnitadmin(temp);
else if (tag.equals("ADMINSIGNDATE")){
Timestamp timestamp = TimeUtil.getTimeOfTimeStr(temp);
tb_tenorNoticBookVO.setAdminsigndate(timestamp);
}
} /**
*
* @param uri
* @param localName
* @param tag
* @throws SAXParseException
*/
public void endElement(String uri, String localName, String tag)
throws SAXParseException {
System.out.println(localName);
startElementFlag=false;
if (localName.equals("TENORNNOTICEBOOK")) {
DataOperator dataOperator = DataOperatorFactory.getTenorNotictBookInstance();
try {
//dataOperator.insertRecord(tb_tenorNoticBookVO);
} catch (Exception e) {
}
parser.setContentHandler(parentHandler);
} else {
while (!tags.empty())
tags.pop();
}
}
}
回车 2.bbbb 回车 3.cccc
这样在解析的时候就会出现问题了,
msn [email protected]
qq 7766284请联系我,谢谢
import org.xml.sax.helpers.*;
import org.xml.sax.*;
import java.util.*;
/**
* <p>Title: basic core</p>
* <p>Description: zealvampire</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: zealvampire</p>
* @author zeal
* @version 1.0
*/public class simpleSaxParser extends DefaultHandler {
private String name=null;
private StringBuffer value=null;
private HashMap hMap=null; public simpleSaxParser() {
}
public void startDocument( ) throws SAXException {
System.out.println("---SAX Event: START DOCUMENT---");
this.hMap=new HashMap();
this.value=new StringBuffer();
} public void endDocument( ) throws SAXException {
System.out.println("---SAX Event: END DOCUMENT---");
} public void startElement( String namespaceURI,String localName,String qName,Attributes attr ) throws SAXException {
System.out.println("SAX event:startElement["+localName+"]");
if(localName.equals("param")){
this.value.setLength(0);
this.name = attr.getValue("name");
}
} public void endElement( String namespaceURI,String localName,String qName ) throws SAXException {
System.out.println("sax event:endElement["+localName+"]");
if(localName.equals("param")){
this.hMap.put(this.name,this.value.toString().trim());
this.value.setLength(0);
}
} public void characters( char[] ch, int start, int length ) throws SAXException{
this.value.append(ch,start,length);
} private void printError(SAXParseException exception){
System.out.println("\t行:\t" + exception.getLineNumber());
System.out.println("\t列:\t" + exception.getColumnNumber());
System.out.println("\t错误信息:\t" + exception.getMessage());
System.out.println("-------------------");
}
public void warning( SAXParseException exception ) {
System.out.println("------WARNING------");
this.printError(exception);
} public void error( SAXParseException exception ) throws SAXException{
System.out.println("------ ERROR ------");
this.printError(exception);
} public void fatalError( SAXParseException exception ) throws SAXException {
System.out.println("------ FATAL ERROR ------");
this.printError(exception);
} public HashMap getResult(){
return this.hMap;
}
}
<parameter>
<sys>
<param name="INITIAL_CONTEXT_FACTORY">org.jnp.interfaces.NamingContextFactory</param>
<param name="PROVIDER_URL">localhost:1099</param>
<param name="URL_PKG_PREFIXES">org.jboss.naming:org.jnp.interfaces</param>
<param name="SECURITY_PRINCIPAL">test_user</param>
<param name="SECURITY_CREDENTIALS">test_pass</param>
<param name="名字">1.ddd
2.ddd3.ccc</param>
</sys>
</parameter>