<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(197) 評論(0)  編輯  收藏 所屬分類: DOM4J

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


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 99久热只有精品视频免费看| 亚洲av无码一区二区三区网站| 无遮挡呻吟娇喘视频免费播放| 四虎国产精品永久免费网址| 亚洲国产美女精品久久久久| 免费大片黄手机在线观看| 亚洲免费观看网站| 免费**毛片在线播放直播| 少妇人妻偷人精品免费视频| 亚洲午夜无码久久久久小说| **一级毛片免费完整视| 亚洲AV成人精品日韩一区| 精品久久香蕉国产线看观看亚洲| 亚洲乱码一二三四五六区| 免费久久精品国产片香蕉| 99re免费99re在线视频手机版| 久久人午夜亚洲精品无码区| 亚洲视频中文字幕| 亚洲一区视频在线播放| 国产乱码免费卡1卡二卡3卡| 成人毛片100免费观看| 久久久久亚洲精品天堂久久久久久| 亚洲AV永久无码天堂影院| 亚洲精品无码午夜福利中文字幕| 大地资源在线观看免费高清| a级毛片100部免费观看| 亚洲无线观看国产精品| 久久这里只精品热免费99| 精品亚洲成a人在线观看| 亚洲综合婷婷久久| 伊人久久大香线蕉亚洲| 久久久久久免费一区二区三区 | 337p日本欧洲亚洲大胆精品555588| 国产美女被遭强高潮免费网站 | 三级网站在线免费观看| 亚洲AV成人无码网天堂| 亚洲国产成人久久综合一区| 亚洲精品无码Av人在线观看国产 | 精品亚洲成a人片在线观看| 亚洲午夜精品久久久久久浪潮| 成人免费视频试看120秒|