|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先谈谈我对java的一些认识。我选择java,是因为他语法简单,功能强大,从web,到桌面,到嵌入式,无所不能。但当我进一步了解了java后,感叹,java原来也有许多缺点。xml此小程序用DOM来读取XML文件,用DOM4j来写XML文件。
DOM4j下载地点:http://sourceforge.net/projects/dom4j
1、Applet主程序mainFrame.java
packageframe;
/**
*@authorwls
*/
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassmainFrame
extendsApplet{
DomXmldomxml=newDomXml();
ButtonbtnExit=newButton("Exit");
ButtonbtnRun=newButton("Save");
LabellblMain=newLabel("dynamicroute.xml设置文件");
LabellblSiteinfo=newLabel("srcserverid:");
LabellblPath=newLabel("destserverid:");
LabellblFilename=newLabel("switchserver:");
LabellblSession=newLabel("metric:");
LabellblSuccess=newLabel("");
TextFieldtxtSiteinfo=newTextField(domxml.getSrcserverid(),10);
TextFieldtxtPath=newTextField(domxml.getDestserverid(),10);
TextFieldtxtFilename=newTextField(domxml.getSwitchserver(),10);
TextFieldtxtSession=newTextField(domxml.getMetric(),10);
Panelp6=newPanel();
Panelp1=newPanel();
Panelp2=newPanel();
Panelp3=newPanel();
Panelp4=newPanel();
Panelp5=newPanel();
Panelp7=newPanel();
Panelp8=newPanel();
publicvoidinit(){
resize(400,300);
p6.add(lblMain);
p1.add(lblSiteinfo);
p1.add(txtSiteinfo);
p2.add(lblPath);
p2.add(txtPath);
p3.add(lblSession);
p3.add(txtSession);
p4.add(btnRun);
p4.add(btnExit);
p5.add(lblFilename);
p5.add(txtFilename);
p8.add(lblSuccess);
setLayout(newGridLayout(4,2,1,1));
add(p6);
add(p7);
add(p1);
add(p2);
add(p5);
add(p3);
add(p4);
add(p8);
}
publicbooleanaction(Eventevt,Objectobj){
if(evt.targetinstanceofButton){
StringbtnName=(String)obj;
if(btnName.equals("Save")){
try{
domxml.setSrcserverid(txtSiteinfo.getText());
domxml.setDestserverid(txtPath.getText());
domxml.setSwitchserver(txtFilename.getText());
domxml.setMetric(txtSession.getText());
buildXmlbuildxml=newbuildXml();
if(buildxml.createXMLFile("dynamicroute.xml",domxml)==1&&
buildxml.formatXMLFile("dynamicroute.xml")==1){
lblSuccess.setText("Savesuccess!");
System.out.println("Savesuccess!");
}
}
catch(Exceptione){
e.printStackTrace();
}
}
elseif(btnName.equals("Exit")){
System.exit(0);
}
}
returnsuper.action(evt,obj);
}
publicstaticvoidmain(Stringargs[]){
newFrameDownload(newmainFrame());
}
}
classFrameDownload
extendsFrame{
publicFrameDownload(Appletapplet){
setSize(400,300);
add("Center",applet);
applet.init();
setVisible(true);
this.addWindowListener(newcloseWindows());
}
}
classcloseWindows
extendsWindowAdapter{
publicvoidwindowClosing(WindowEvente){
Framefrm=(Frame)(e.getSource());
frm.dispose();
System.exit(0);
}
}
2、读取XML类DomXml.java
packageframe;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
/*
*Createdon2004-8-19
*@authorwls
*java读取XML文档
*使用DoM来读取一个XML文档的内容
*
*/
publicclassDomXml{
privateStringsrcserverid;
privateStringdestserverid;
privateStringswitchserver;
privateStringmetric;
publicDomXml(){
try{
initXml("dynamicroute.xml");
}
catch(Exceptionex){
metric="0";
}
}
publicStringgetSrcserverid(){
returnsrcserverid;
}
publicvoidsetSrcserverid(Stringsrcserverid){
this.srcserverid=srcserverid;
}
publicStringgetDestserverid(){
returndestserverid;
}
publicvoidsetDestserverid(Stringdestserverid){
this.destserverid=destserverid;
}
publicStringgetSwitchserver(){
returnswitchserver;
}
publicvoidsetSwitchserver(Stringswitchserver){
this.switchserver=switchserver;
}
publicStringgetMetric(){
returnmetric;
}
publicvoidsetMetric(Stringmetric){
this.metric=metric;
}
publicvoidinitXml(StringfileName)throwsException{
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbulider=factory.newDocumentBuilder();
Documentdoc=bulider.parse(fileName);
NodeListnl=doc.getElementsByTagName("dynamicroute");
for(inti=0;i<nl.getLength();i++){
Elementnode=(Element)nl.item(i);
srcserverid=node.getElementsByTagName("srcserverid").item(0).
getFirstChild().getNodeValue().trim();
destserverid=node.getElementsByTagName("destserverid").item(0).
getFirstChild().getNodeValue().trim();
switchserver=node.getElementsByTagName("serverid").item(0).
getFirstChild().getNodeValue().trim();
metric=node.getElementsByTagName("metric").item(0).getFirstChild().
getNodeValue().trim();
}
}
}
3、写XML文件类buildXml.java
packageframe;
importjava.io.*;
importorg.dom4j.*;
importorg.dom4j.io.*;
/**
*@authorwls
*/
publicclassbuildXml{
publicbuildXml(){
}
/**
*创建一个XML文档,文档名由输出参数决意
*@paramfilename需创建的文件名
*@return前往操纵了局,0表失利,1表乐成
*/
publicintcreateXMLFile(Stringfilename,DomXmldomxml){
/**前往操纵了局,0表失利,1表乐成*/
intreturnValue=0;
/**创建document对象*/
Documentdocument=DocumentHelper.createDocument();
/**创建XML文档的根route*/
ElementrouteElement=document.addElement("route");
/**到场第一个dynamicroute节点*/
ElementdynamicrouteElement=routeElement.addElement("dynamicroute");
/**到场show参数内容*/
//bookElement.addAttribute("show","yes");
/**到场节点*/
ElementsrcserveridElement=dynamicrouteElement.addElement("srcserverid");
ElementdestserveridElement=dynamicrouteElement.addElement("destserverid");
ElementswitchserverElement=dynamicrouteElement.addElement("switchserver");
ElementserveridElement=switchserverElement.addElement("serverid");
ElementmetricElement=dynamicrouteElement.addElement("metric");
/**为title设置内容*/
srcserveridElement.setText(domxml.getSrcserverid());
destserveridElement.setText(domxml.getDestserverid());
serveridElement.setText(domxml.getSwitchserver());
metricElement.setText(domxml.getMetric());
try{
/**将document中的内容写进文件中*/
XMLWriterwriter=newXMLWriter(newFileWriter(newFile(filename)));
writer.write(document);
writer.close();
/**实行乐成,需前往1*/
returnValue=1;
}
catch(Exceptionex){
ex.printStackTrace();
}
returnreturnValue;
}
/**
*格局化XML文档,并办理中文成绩
*@paramfilename
*@return
*/
publicintformatXMLFile(Stringfilename){
intreturnValue=0;
try{
SAXReadersaxReader=newSAXReader();
Documentdocument=saxReader.read(newFile(filename));
XMLWriteroutput=null;
/**格局化输入,范例IE扫瞄一样*/
OutputFormatformat=OutputFormat.createPrettyPrint();
/**指定XML字符集编码*/
format.setEncoding("UTF-8");
output=newXMLWriter(newFileWriter(newFile(filename)),format);
output.write(document);
output.close();
/**实行乐成,需前往1*/
returnValue=1;
}
catch(Exceptionex){
ex.printStackTrace();
}
returnreturnValue;
}
}
但是我同意你的观点,对于大型项目来说,应该是采用框架的一部分,根据功能的不同而改进,欢迎你能再提出些宝贵意见,我会多多学习的。说到jbuilder,我可能是个人感觉,用的时候确实没有vs爽,我最喜欢的IDE是netbeans,谢谢。 |
|