• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Merlin

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

               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              34 隨筆 :: 0 文章 :: 40 評論 :: 0 Trackbacks

            這是讀取xml文件的java程序,我調(diào)試好的。采用的是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 A? = ?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")關(guān)系到編碼問題,非常重要。

            ?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( " 中文題目 " , " 中文內(nèi)容 " );
            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 閱讀(203) 評論(0)  編輯 收藏 引用 所屬分類: java基礎(chǔ)篇
            久久AⅤ人妻少妇嫩草影院| 亚洲一区精品伊人久久伊人| 久久久久亚洲av无码专区导航 | 久久国产精品-久久精品| 99久久人人爽亚洲精品美女| 欧美粉嫩小泬久久久久久久 | 久久亚洲AV无码精品色午夜麻豆| 伊人色综合久久天天人手人婷| 亚洲AV无一区二区三区久久| 亚洲国产精品久久| 久久久久亚洲av综合波多野结衣| 国产99精品久久| 亚洲精品国产美女久久久| 国产福利电影一区二区三区久久老子无码午夜伦不 | 国产午夜福利精品久久| 伊人色综合久久天天人守人婷| 成人综合伊人五月婷久久| 日本国产精品久久| 99久久免费国产精品| 久久久久亚洲AV片无码下载蜜桃| 久久狠狠一本精品综合网| 97久久久精品综合88久久| 精品久久久中文字幕人妻| 久久久久人妻精品一区三寸蜜桃 | 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 亚洲第一极品精品无码久久| 久久人人爽人人澡人人高潮AV | 久久久久久av无码免费看大片| 久久婷婷激情综合色综合俺也去| 欧美久久久久久精选9999| 久久精品国产精品国产精品污| 人妻精品久久无码专区精东影业 | 亚洲精品美女久久777777| 久久久久久综合网天天| 一97日本道伊人久久综合影院| 久久激情五月丁香伊人| 草草久久久无码国产专区| 国内精品免费久久影院| 久久综合成人网| 久久精品国产日本波多野结衣| 亚洲国产小视频精品久久久三级|