|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
唉!都是钱闹的1.Swing和.net开发比较------从市场份额看.net开发主要占据大部分的中小型和中型的的桌面开发,原因是它封装了很多工具xml|程序Java与XML(二)用java编写xml的读写程序
这是读取xml文件的java程序,我调试好的。接纳的是dom体例读取xml文件到Vector中。
packagesrc;
importjava.io.*;
importjava.util.Vector;
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
publicclassreadxml{
staticDocumentdocument;
privatebooleanvalidating;
publicreadxml(){
}
publicVectortoRead(Stringfilename){
Vectortitle=newVector();
Vectorcontent=newVector();
StringmyStr=newString();
try{
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
factory.setValidating(validating);
DocumentBuilderbuilder=factory.newDocumentBuilder();
document=builder.parse(newFile(filename));
document.getDocumentElement().normalize();
Nodenode=document.getFirstChild();
NodeListlist=node.getChildNodes();
for(inti=0;i<list.getLength();i++){
Nodenodeitm=list.item(i);
if(nodeitm.getNodeName().equals("Title")){
myStr=nodeitm.getFirstChild().getNodeValue();
title.addElement(myStr);//getFirstChild()
}
if(nodeitm.getNodeName().equals("Content")){
myStr=nodeitm.getFirstChild().getNodeValue();
content.addElement(myStr);
}
}
}catch(Exceptionexp){
exp.printStackTrace();
returnnull;
}
Vectorall=newVector();
all.add(title);
all.add(content);
returnall;
}
publicstaticvoidmain(String[]args){
VectorA;
readxmlmy=newreadxml();
A=my.toRead("f: omcat5webappsmyxmlxmldata9.xml");
for(inti=0;i<A.size();i++){
System.out.println(A.elementAt(i));
}
}
}
这是将xml写进文件。个中,transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312")干系到编码成绩,十分主要。
importorg.w3c.dom.*;
importjavax.xml.parsers.*;
importjavax.xml.transform.*;
importjavax.xml.transform.dom.DOMSource;
importjavax.xml.transform.stream.StreamResult;
importjava.io.*;
publicclasswritexml{
privateDocumentdocument;
privateStringfilename;
publicwritexml(Stringname)throwsParserConfigurationException{
filename=name;
DocumentBuilderFactoryfactory=DocumentBuilderFactory.newInstance();
DocumentBuilderbuilder=factory.newDocumentBuilder();
document=builder.newDocument();
}
publicvoidtoWrite(Stringmytitle,Stringmycontent){
Elementroot=document.createElement("WorkShop");
document.appendChild(root);
Elementtitle=document.createElement("Title");
title.appendChild(document.createTextNode(mytitle));
root.appendChild(title);
Elementcontent=document.createElement("Content");
content.appendChild(document.createTextNode(mycontent));
root.appendChild(content);
}
publicvoidtoSave(){
try{
TransformerFactorytf=TransformerFactory.newInstance();
Transformertransformer=tf.newTransformer();
DOMSourcesource=newDOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
PrintWriterpw=newPrintWriter(newFileOutputStream(filename));
StreamResultresult=newStreamResult(pw);
transformer.transform(source,result);
}
catch(TransformerExceptionmye){
mye.printStackTrace();
}
catch(IOExceptionexp){
exp.printStackTrace();
}
}
publicstaticvoidmain(Stringargs[]){
try{
writexmlmyxml=newwritexml("f: omcat5webappsmyxmlxmldata9.xml");
myxml.toWrite("中文标题","中文内容");
myxml.toSave();
System.out.print("Yourwritingissuccessful.");
}
catch(ParserConfigurationExceptionexp){
exp.printStackTrace();
System.out.print("Yourwritingisfailed.");
}
}
}
java比较简单,没有C++的烦琐,但学习时最好有C++为基础.与JSP和SQL起应用,功能强大. |
|