Merlin

Life was like a box of chocolates. You never know what you're gonna get.

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  34 随笔 :: 0 文章 :: 40 评论 :: 0 Trackbacks

这是读取xml文件的java程序,我调试好的。采用的是dom方式读取xml文件到Vector中。

 1 package  src;
 2 import  java.io. * ;
 3 import  java.util.Vector;
 4 import  javax.xml.parsers. * ;
 5 import  org.w3c.dom. * ;
 6 public   class  readxml  {
 7 static  Document document;
 8 private   boolean  validating;
 9 public  readxml()  {
10 }

11 public  Vector toRead(String filename)  {
12 Vector title = new  Vector();
13 Vector content = new  Vector();
14 String myStr = new  String();
15 try   {
16 DocumentBuilderFactory factory  =  DocumentBuilderFactory.newInstance();
17 factory.setValidating(validating);
18 DocumentBuilder builder  =  factory.newDocumentBuilder();
19 document  =  builder.parse( new  File(filename));
20 document.getDocumentElement().normalize();
21 Node node  =  document.getFirstChild();
22 NodeList list  =  node.getChildNodes();
23 for  ( int  i  =   0 ; i  <  list.getLength(); i ++ {
24 Node nodeitm  =  list.item(i);
25 if  (nodeitm.getNodeName().equals( " Title " ))  {
26 myStr = nodeitm.getFirstChild().getNodeValue();
27 title.addElement(myStr); // getFirstChild()
28 }

29 if  (nodeitm.getNodeName().equals( " Content " ))  {
30 myStr = nodeitm.getFirstChild().getNodeValue();
31 content.addElement(myStr);
32 }

33 }

34 }
  catch  (Exception exp)  {
35 exp.printStackTrace();
36 return   null ;
37 }

38 Vector all = new  Vector();
39 all.add(title);
40 all.add(content); 
41 return  all;
42 }

43
44 public   static   void  main(String[] args)  {
45 Vector A;
46 readxml my  =   new  readxml();
47 =  my.toRead( " file.xml " );
48 for  ( int  i  =   0 ; i  <  A.size(); i ++ {
49 System.out.println(A.elementAt(i));
50 }

51 }

52 }

53

这是将xml写入文件。其中,transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312")关系到编码问题,非常重要。

 1 import  org.w3c.dom. * ;
 2 import  javax.xml.parsers. * ;
 3 import  javax.xml.transform. * ;
 4 import  javax.xml.transform.dom.DOMSource;
 5 import  javax.xml.transform.stream.StreamResult;
 6 import  java.io. * ;
 7 public   class  writexml  {
 8 private  Document document;
 9 private  String filename;
10
11 public  writexml(String name)  throws  ParserConfigurationException {
12 filename = name;
13 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
14 DocumentBuilder builder = factory.newDocumentBuilder();
15 document = builder.newDocument();
16 }

17 public   void  toWrite(String mytitle,String mycontent) {
18 Element root = document.createElement( " WorkShop " );
19 document.appendChild(root);
20 Element title = document.createElement( " Title " );
21 title.appendChild(document.createTextNode(mytitle));
22 root.appendChild(title);
23 Element content = document.createElement( " Content " );
24 content.appendChild(document.createTextNode(mycontent));
25 root.appendChild(content);
26 }

27 public   void  toSave() {
28 try {
29 TransformerFactory tf = TransformerFactory.newInstance();
30 Transformer transformer = tf.newTransformer();
31 DOMSource source = new  DOMSource(document);
32 transformer.setOutputProperty(OutputKeys.ENCODING, " GB2312 " );
33 transformer.setOutputProperty(OutputKeys.INDENT, " yes " );
34 PrintWriter pw = new  PrintWriter( new  FileOutputStream(filename));
35 StreamResult result = new  StreamResult(pw);
36 transformer.transform(source,result);
37 }

38 catch (TransformerException mye) {
39 mye.printStackTrace();
40 }

41 catch (IOException exp) {
42 exp.printStackTrace();
43 }

44 }

45 public   static   void  main(String args[]) {
46 try {
47 writexml myxml = new  writexml( " file.xml " );
48 myxml.toWrite( " 中文题目 " , " 中文内容 " );
49 myxml.toSave();
50 System.out.print( " Your writing is successful. " );
51 }

52 catch (ParserConfigurationException exp) {
53 exp.printStackTrace();
54 System.out.print( " Your writing is failed. " );
55 }
 
56 }

57 }

58

 

 

posted on 2006-07-10 18:16 Merlin 阅读(177) 评论(0)  编辑 收藏 引用 所属分类: java基础篇

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理