|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
恰恰证明了java的简单,要不怎么没有通过c/c++来搞个这种框架?xml|办理|成绩|中文------------
author:wait4friend
------------
共有三种办法,分离利用了JDK,xerces.jar或jdom.jar。
间接贴出原码:
/**
*UsethisprogramtoindicatehowtosaveaXMLfile,resolvingtheproblem
*aboutCharacterSet,ImeanGB2312herecanbedealtwithcorrectly
*
*@authorMichaelZeng
*/
packageclasses;
importjava.io.*;
publicclassDOMTest
{
privateStringinFile="E:/AboutXML/Java_XML/XmlData/mapping.xml";
privateStringoutFile="E:/AboutXML/Java_XML/XmlData/my.xml";
publicstaticvoidmain(Stringargs[])
{
newDOMTest();
}
//Approach1:onlyusetheJDK1.4
//Inthiscase,IhandletheChinesecorrectlywiththeTransFormer.setOutputProperty()
//Thesepackagesarenecessary:
//org.w3c.dom
//javax.xml.parsers
//javax.xml.transform
//javax.xml.transform.dom
//javax.xml.transform.stream
publicDOMTest()
{
try
{
//codetocreateanewDOMdocumentgoeshere...
javax.xml.parsers.DocumentBuilderbuilder=
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Documentdoc=builder.newDocument();
//Addsomeelementshere...
org.w3c.dom.Elementroot=doc.createElement("先生");
org.w3c.dom.Elementwang=doc.createElement("王");
wang.appendChild(doc.createTextNode("我是王先生"));
root.appendChild(wang);
doc.appendChild(root);
//codetosavegoeshere...
javax.xml.transform.Transformertransformer=
javax.xml.transform.TransformerFactory.newInstance().newTransformer();
//Noticethisfirstsentencebelow,whichresolvestheproblemofChinese
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,"gb2312");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,"yes");
transformer.transform(newjavax.xml.transform.dom.DOMSource(doc),
newjavax.xml.transform.stream.StreamResult(outFile));
}
catch(Exceptione)
{
System.out.println(e.getMessage());
}
}
//Approach2:useXercesadditionally.Thexerces.jarmusthavebeenin
//yourCLASSPATH
//Inthiscase,Chinesecharacterscanbehandledsuccessfully.
//Thesepackagesarenecessary:
//org.w3c.dom
//org.apache.xerces.parsers
//org.apache.xml.serialize
////publicDOMTest()
////{
////try
////{
//////codetoparseanexistedXMLfilegoeshere...
////org.apache.xerces.parsers.DOMParserparser=
////neworg.apache.xerces.parsers.DOMParser();
////parser.parse(inFile);
////org.w3c.dom.Document.doc=parser.getDocument();
////
//////codetosavegoeshere...
////FileWriterwriter=newFileWriter(outFile);
//////PayattentiontotheOutputFormatconstructor,whichsettheGB2312
////org.apache.xml.serialize.OutputFormatoutputFormat=
////neworg.apache.xml.serialize.OutputFormat(doc,"GB2312",true);
////
////org.apache.xml.serialize.XMLSerializerserializer=
////neworg.apache.xml.serialize.XMLSerializer(writer,outputFormat);
////serializer.serialize(doc);
////writer.close();
////}
////catch(Exceptione)
////{
////System.out.println(e.getMessage());
////}
////}
//Approach3:usetheJDOMpackage,andthisisthesimplestone.Additionally,
//thejdom.jarmusthavebeeninyourCLASSPATH
//Thesepackagearenecessary:
//org.jdom
//org.jdom.input
//org.jdom.output
////publicDOMTest()
////{
////try
////{
//////codetoparseanexistedXMLfilegoeshere...
////org.jdom.input.SAXBuilderbuilder=neworg.jdom.input.SAXBuilder();
////org.jdom.Documentdoc=builder.build(inFile);
////
//////codetosavegoeshere...
////FileWriterwriter=newFileWriter(outFile);
////
////org.jdom.output.XMLOutputteroutputter=
////neworg.jdom.output.XMLOutputter("",true,"GB2312");
////outputter.output(doc,writer);
////writer.close();
////}
////catch(Exceptione)
////{
////System.out.println(e.getMessage());
////}
////}
}
为什么外国人还要写那些框架进行代码封装,他们不就是为了别人使用时可以更简单么!如果要达到一个企业级项目的不用框架是很难的。小一些的项目还行,大的光是MVC模式的设计的编码量就够大的了。还有性能方面,单轮windows,这个工具是微软写的,。 |
|