posts - 21,  comments - 6,  trackbacks - 0
          ??1 package ?dom;
          ??2
          ??3 import ?java.io.File;
          ??4 import ?java.io.FileWriter;
          ??5
          ??6 import ?org.dom4j.Document;
          ??7 import ?org.dom4j.DocumentHelper;
          ??8 import ?org.dom4j.Element;
          ??9 import ?org.dom4j.Node;
          ?10 import ?org.dom4j.io.SAXReader;
          ?11 import ?org.dom4j.io.XMLWriter;
          ?12
          ?13 /**
          ?14 ?*? @author ??~@_@
          ?15 ? */

          ?16 public ? class ?Dom4jDemo? {
          ?17 ???? /** ?XML文件路徑? */
          ?18 ???? private ?String?XMLPath? = ? null ;
          ?19
          ?20 ???? /** ?XML文檔? */
          ?21 ???? private ?Document?document? = ? null ;
          ?22
          ?23 ???? public ?Dom4jDemo()? {
          ?24 ????}

          ?25
          ?26 ???? /**
          ?27 ?????*??初始化xml文件
          ?28 ?????*?
          ?29 ?????*? @param ?XMLPath?文件路徑
          ?30 ????? */

          ?31 ???? public ?Dom4jDemo(String?XMLPath)? {
          ?32 ???????? this .XMLPath? = ?XMLPath;
          ?33 ????}

          ?34
          ?35 ???? /** ?*?打開文檔? */
          ?36 ???? public ? void ?openXML()? {
          ?37 ???????? try ? {
          ?38 ????????????SAXReader?reader? = ? new ?SAXReader();
          ?39 ???????????? this .document? = ?reader.read( this .XMLPath);
          ?40 ????????????System.out.println( " openXML()?successful? " );
          ?41 ????????}
          ? catch ?(Exception?e)? {
          ?42 ????????????System.out.println( " openXML()?Exception: " ? + ?e.getMessage());
          ?43 ????????}

          ?44 ????}

          ?45
          ?46 ???? /**
          ?47 ?????*??創建文檔
          ?48 ?????*?
          ?49 ?????*? @param ?rootName?根節點名稱
          ?50 ????? */

          ?51 ???? public ? void ?createXML(String?rootName)? {
          ?52 ???????? try ? {
          ?53 ???????????? this .document? = ?DocumentHelper.createDocument();
          ?54 ????????????Element?root? = ?document.addElement(rootName);
          ?55 ????????????System.out.println( " createXML()?successful " );
          ?56 ????????}
          ? catch ?(Exception?e)? {
          ?57 ????????????System.out.println( " createXML()?Exception: " ? + ?e.getMessage());
          ?58 ????????}

          ?59 ????}

          ?60
          ?61 ???? /**
          ?62 ?????*??添加根節點的child
          ?63 ?????*?
          ?64 ?????*? @param ?nodeName?節點名
          ?65 ?????*? @param ?nodeValue?節點值
          ?66 ????? */

          ?67 ???? public ? void ?addNodeFromRoot(String?nodeName,?String?nodeValue)? {
          ?68 ????????Element?root? = ? this .document.getRootElement();
          ?69 ????????Element?level1? = ?root.addElement(nodeName);
          ?70 ????????level1.addText(nodeValue);
          ?71 ????}

          ?72
          ?73 ???? /**
          ?74 ?????*??打開文檔
          ?75 ?????*?
          ?76 ?????*? @param ?filePath?文檔路徑
          ?77 ????? */

          ?78 ???? public ? void ?openXML(String?filePath)? {
          ?79 ???????? try ? {
          ?80 ????????????SAXReader?saxReader? = ? new ?SAXReader();
          ?81 ???????????? this .document? = ?saxReader.read(filePath);
          ?82 ????????????System.out.println( " openXML(String?filePath)?successful? " );
          ?83 ????????}
          ? catch ?(Exception?e)? {
          ?84 ????????????System.out.println( " openXML()?Exception: " ? + ?e.getMessage());
          ?85 ????????}

          ?86 ????}

          ?87
          ?88 ???? /** ?保存文檔? */
          ?89 ???? public ? void ?saveXML()? {
          ?90 ???????? try ? {
          ?91 ????????????XMLWriter?output? = ? new ?XMLWriter( new ?FileWriter( new ?File(
          ?92 ???????????????????? this .XMLPath)));
          ?93 ????????????output.write(document);
          ?94 ????????????output.close();
          ?95 ????????????System.out.println( " saveXML()?successful? " );
          ?96 ????????}
          ? catch ?(Exception?e1)? {
          ?97 ????????????System.out.println( " saveXML()?Exception: " ? + ?e1.getMessage());
          ?98 ????????}

          ?99 ????}

          100
          101 ???? /**
          102 ?????*??保存文檔
          103 ?????*?
          104 ?????*? @param ?toFilePath?保存路徑
          105 ????? */

          106 ???? public ? void ?saveXML(String?toFilePath)? {
          107 ???????? try ? {
          108 ????????????XMLWriter?output? = ? new ?XMLWriter( new ?FileWriter(
          109 ???????????????????? new ?File(toFilePath)));
          110 ????????????output.write(document);
          111 ????????????output.close();
          112 ????????}
          ? catch ?(Exception?e1)? {
          113 ????????????System.out.println( " saveXML()?Exception: " ? + ?e1.getMessage());
          114 ????????}

          115 ????}

          116
          117 ???? /**
          118 ?????*??獲得某個節點的值
          119 ?????*?
          120 ?????*? @param ?nodeName?節點名稱
          121 ????? */

          122 ???? public ?String?getElementValue(String?nodeName)? {
          123 ???????? try ? {
          124 ????????????Node?node? = ?document.selectSingleNode( " // " ? + ?nodeName);
          125 ???????????? return ?node.getText();
          126 ????????}
          ? catch ?(Exception?e1)? {
          127 ????????????System.out
          128 ????????????????????.println( " getElementValue()?Exception: " ? + ?e1.getMessage());
          129 ???????????? return ? null ;
          130 ????????}

          131 ????}

          132
          133 ???? /**
          134 ?????*??獲得某個節點的子節點的值
          135 ?????*?
          136 ?????*? @param ?nodeName
          137 ?????*? @param ?childNodeName
          138 ?????*? @return
          139 ????? */

          140 ???? public ?String?getElementValue(String?nodeName,?String?childNodeName)? {
          141 ???????? try ? {
          142 ????????????Node?node? = ? this .document.selectSingleNode( " // " ? + ?nodeName? + ? " / "
          143 ???????????????????? + ?childNodeName);
          144 ???????????? return ?node.getText();
          145 ????????}
          ? catch ?(Exception?e1)? {
          146 ????????????System.out
          147 ????????????????????.println( " getElementValue()?Exception: " ? + ?e1.getMessage());
          148 ???????????? return ? null ;
          149 ????????}

          150 ????}

          151
          152 ???? /**
          153 ?????*??設置一個節點的text
          154 ?????*?
          155 ?????*? @param ?nodeName?節點名
          156 ?????*? @param ?nodeValue?節點值
          157 ????? */

          158 ???? public ? void ?setElementValue(String?nodeName,?String?nodeValue)? {
          159 ???????? try ? {
          160 ????????????Node?node? = ? this .document.selectSingleNode( " // " ? + ?nodeName);
          161 ????????????node.setText(nodeValue);
          162 ????????}
          ? catch ?(Exception?e1)? {
          163 ????????????System.out
          164 ????????????????????.println( " setElementValue()?Exception: " ? + ?e1.getMessage());
          165 ????????}

          166 ????}

          167
          168 ???? /**
          169 ?????*??設置一個節點值
          170 ?????*?
          171 ?????*? @param ?nodeName?父節點名
          172 ?????*? @param ?childNodeName?節點名
          173 ?????*? @param ?nodeValue?節點值
          174 ????? */

          175 ???? public ? void ?setElementValue(String?nodeName,?String?childNodeName,
          176 ????????????String?nodeValue)? {
          177 ???????? try ? {
          178 ????????????Node?node? = ? this .document.selectSingleNode( " // " ? + ?nodeName? + ? " / "
          179 ???????????????????? + ?childNodeName);
          180 ????????????node.setText(nodeValue);
          181 ????????}
          ? catch ?(Exception?e1)? {
          182 ????????????System.out
          183 ????????????????????.println( " setElementValue()?Exception: " ? + ?e1.getMessage());
          184 ????????}

          185 ????}

          186 }
          posted on 2006-09-20 15:45 Warren.Wu 閱讀(201) 評論(0)  編輯  收藏 所屬分類: DOM4J

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 东阳市| 昌邑市| 保定市| 松江区| 正镶白旗| 太仆寺旗| 马龙县| 鄱阳县| 醴陵市| 肇州县| 兖州市| 中江县| 马龙县| 开江县| 普安县| 乌恰县| 堆龙德庆县| 长乐市| 鸡西市| 大名县| 乌鲁木齐县| 敖汉旗| 北辰区| 英吉沙县| 黔江区| 聊城市| 左云县| 舞钢市| 和平区| 天津市| 莱芜市| 巴楚县| 饶阳县| 商水县| 湾仔区| 邢台市| 二连浩特市| 南木林县| 毕节市| 万山特区| 丹凤县|