<bookpic>images\da0817.jpg</bookpic>
和
<bookpdfpath>book\da0817.pdf</bookpdfpath>
应该是一样的
没道理会有错看看解析的程序吧
和
<bookpdfpath>book\da0817.pdf</bookpdfpath>
应该是一样的
没道理会有错看看解析的程序吧
解决方案 »
- hibernate使用save 后rollback不起作用
- struts2下载文件,文件名出现乱码问题!
- MS sqlserver的分页技术怎么实现
- 急!!!!!!!!!!!!Linux下部署DNS和sendmail的问题??????????
- 关于jfreechart和struts2本人有些问题,求大牛
- struts+spring+hibernate的架沟,如果业务逻辑分析需要使用工厂方法模式,在SPRING的SERVICE里如何实现这个模式
- 我的eclipse3.1.1如何与WEBLOGIC结合运行,好难用
- 请给一段JTable里放置JCheckBox和JComboBox的源码,自定义的tableModel.谢谢。
- 请大家推荐几本好点的JSP的入门书籍吧
- 求助:那个经典的HelloWorld的应用程序(含全部代码)怎么部署?
- 急问:用Applet如何才能访问MS SQLServer2000,并从中读取数据到Applet页面?
- 全国范围Delphi程序员的现状和薪资调查?
查xml文件的生成部分
是一个问题对应几个答案或者资源。xml 结构:
<?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE questions
PUBLIC "-//Amid co, Ltd.//DTD TK Application 1.0//CH"
"http://211.94.206.59/dtd/tk.dtd">
<questions>
<question difficult="1" correctSeq="2">
<question-content>循环使用???</question-content>
<question-Resource seq="1" resType="A" mediaType="1" content="生的费用由该号码承"/>
<question-Resource seq="2" resType="A" mediaType="1" content="2:按条计费"/>
<question-Resource seq="3" resType="A" mediaType="1" content="承担"/>
</question>
</questions>
DefaultHandler progress:
package com.unicom.race.util.xml;import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;import com.unicom.race.admin.dao.Answer;
import com.unicom.race.admin.dao.Question;
import com.unicom.race.admin.dao.QuestionDAO;
import com.unicom.race.util.*;import java.util.Vector;
import java.text.SimpleDateFormat;
import java.sql.Timestamp;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author rex
* @version 1.0
*/public class ParserProcess extends DefaultHandler { private String currentName;
private StringBuffer currentValue = new StringBuffer();
private StringBuffer buf = new StringBuffer();
Question question = null;
Vector vet = null;
int group_id ;
int cpid ;
Timestamp createTime ;
String uni_id ;
int processID ;
int questionId; public ParserProcess() {
} public ParserProcess(int cpId, int groupId) { this.group_id = groupId;
this.cpid = cpId; long mills = System.currentTimeMillis();
createTime = new Timestamp(mills); uni_id = "";
processID = 0;
questionId = 0;
}
/* 指定名称空间
<surveys xmlns="http://www.nicholaschase.com/surveys/"
xmlns:rating="http://www.nicholaschase.com/surveys/revised/">
*/
//定义开始解析元素的方法. 这里是将<xxx>中的名称xxx提取出来. public void startElement(String namespaceURI, String localName,
String qName, Attributes attributes)
throws SAXException { //System.out.println("qName:"+qName);
currentValue.delete(0, currentValue.length());
if(qName != null && qName.equals("question")){
question = new Question();
vet = new Vector();
//本次序号
this.processID = this.processID + 1; String cpidStr = UtilTools.expandStringEnd(Integer.toString(cpid),"0",11);
SimpleDateFormat f1 = new SimpleDateFormat("yyMMddHHmmss");
String strdate = f1.format(createTime);
String processIDStr = UtilTools.expandStringEnd(Integer.toString(this.processID),"0",5); //System.out.println("question:"+qName+"processID"+processIDStr);
this.uni_id = processIDStr + strdate + cpidStr; question.setCpId(this.cpid);
question.setGroupId(this.group_id);
question.setUniID(this.uni_id);
question.setDiffLevel( Integer.parseInt(attributes.getValue("difficult")));
question.setSeq(attributes.getValue("correctSeq"));
question.setCreateTime(this.createTime); } if(qName != null && qName.equals("question-Resource")){
//System.out.println("question-Resource:"+qName);
Answer answer = new Answer();
answer.setSeq(attributes.getValue("seq"));
answer.setMediaType(Integer.parseInt(attributes.getValue("mediaType")));
answer.setFlag(attributes.getValue("resType"));
try{
answer.setContent( new String(attributes.getValue("content").getBytes("GB2312") ));
}catch(Exception e){
System.out.println(e.toString());
}
vet.add(answer);
}
this.currentName =qName;
//检查名称空间
//属性也可以属于特定的名称空
//Attributes 列表有一些允许您确定属性的名称空间的方法。
//使用这些 getURI() 和 getQName 方法非常类似于元素本身的 qname 和 localName 方法。 /*
if (namespaceURI == "http://www.nicholaschase.com/surveys/") {
if (localName == "question") {
this.Question = attributes.getValue("subject");
}
}
*/ } //这里是将<xxx></xxx>之间的值加入到currentValue public void characters(char[] ch, int start, int length)
throws SAXException { currentValue.append(ch, start, length); if(this.currentName != null && this.currentName.equals("question-content")){
try{
question.setContent( new String(currentValue.toString().getBytes("GB2312")));
}catch(Exception e){
System.out.println(e.toString());
}
}
//System.out.println(currentValue.toString());
} //在遇到</xxx>结束后,将之前的名称和值一一对应保存在props中 public void endElement(String uri, String localName, String qName)
throws SAXException { //System.out.println("qName:"+qName);
currentValue.delete(0, currentValue.length());
if(qName != null && qName.equals("question")){
if(question != null){
//建立问题
try{ question.create();
this.questionId = QuestionDAO.getQuestionId(this.uni_id);
System.out.println(" questionId:"+this.questionId
+" MediaType:"+question.getMediaType() + " created!");
if(vet != null && vet.size() > 0){
for(int i=0;i<vet.size();i++){
Answer answerTemp = (Answer)vet.elementAt(i);
answerTemp.setQuestionId(this.questionId);
answerTemp.create();
}
}
question = null;
if( vet != null ){
vet.removeAllElements();
vet.trimToSize();
vet = null;
}
}catch(Exception e){
if(e instanceof SAXException){
throw (SAXException)e;
}
System.out.println("error in create Questions & answers:"+e.toString());
Logger.log("error in create Questions & answers by XML file:\n"+e.toString());
} }
}
} public void startDocument()
throws SAXException {
//System.out.println("start parsering XML document...\n");
}
public void endDocument() {
//System.out.println("end parsering XML document...\n");
} /**
其它应用程序可以以类似的方式来获取信息。
<?xml version="1.0" encoding="UTF-8"?>
<?SurveyReader factor="2" ?>
<surveys>
...
processingInstruction() 事件将会拾取它,该事件将它划分成 target 和 data。
*/
public void processingInstruction(String target, String data)
throws SAXException {
System.out.println("Target = ("+target+")");
System.out.println("Data = ("+data+")");
}}