胖娃娃

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            4 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

            今天學(xué)習(xí)了Java讀取XML的簡單方法,主要用到了javax.xml.pasers.*中的一些對象(注:可能會用到的包,xercesImpl.jar、xml-apis.jar
          jdom.jar
          ),首先構(gòu)造一個簡單的操作XML的工具類(Simple Control XML)SCXMLUtils,具體代碼如下(SCXMLUtils.java):

          package ?SCXML;

          import ?java.io.FileWriter;

          import ?javax.xml.parsers.DocumentBuilder;
          import ?javax.xml.parsers.DocumentBuilderFactory;

          import ?org.w3c.dom.Document;
          import ?org.w3c.dom.Node;
          import ?org.w3c.dom.NodeList;

          public ? class ?SCXMLUtils? {

          ????
          private ?SCXMLUtils()? {
          ????}


          ????
          public ? static ?Document?getXMLDoc(String?xmlfile)? {
          ????????
          try ? {

          ????????????DocumentBuilderFactory?factory?
          = ?DocumentBuilderFactory
          ????????????????????.newInstance();
          ????????????DocumentBuilder?builder?
          = ?factory.newDocumentBuilder();
          ????????????Document?doc?
          = ?builder.parse(xmlfile);
          ????????????
          return ?doc;

          ????????}
          ? catch ?(Exception?e)? {

          ????????????System.out.println(
          " an?error?occurred?in?SCXMLUtils: " ? + ?e);
          ????????????
          return ? null ;

          ????????}


          ????}


          ????
          public ? static ?Node?getChild(Node?parent,?String?name)? {

          ????????NodeList?children?
          = ?parent.getChildNodes();
          ???????
          int ?len? = ?children.getLength();
          ????????Node?node?
          = ? null ;
          ????????
          for ?( int ?i? = ? 0 ;?i? < ?len;?i ++ )? {

          ????????????node?
          = ?children.item(i);
          ????????????String?thisname?
          = ?node.getNodeName();
          ????????????
          if ?(name.equals(thisname))? {

          ????????????????
          return ?node;

          ????????????}


          ????????}


          ????????
          return ? null ;
          ????}


          ????
          public ? static ?String?getTextValue(Node?node)? {

          ????????
          if ?(???(node.getNodeType()? == ?Node.TEXT_NODE)
          ????????????
          || ?(node.getNodeType()? == ?Node.CDATA_SECTION_NODE))? {

          ????????????
          return ?node.getNodeValue();

          ????????}
          ? else ? {

          ????????????
          return ? null ;

          ????????}


          ????}


          }

          說明:其中g(shù)etXMLDoc用于返回一個指定xml文件的Document對象,getChild返回一個指定節(jié)點的下一層中某一個指定名稱的節(jié)點對象,
             getTextValue用于返回一個指定節(jié)點內(nèi)的文本內(nèi)容。

          用來測試這個工具類的TestSCXML.java:

          import ?org.w3c.dom.Document;
          import ?org.w3c.dom.Element;
          import ?org.w3c.dom.Node;
          import ?SCXML. * ;

          public ? class ?TestSCXML? {

          ????
          public ? static ? void ?main(String[]?args)? {

          ????????String?nodevalue?
          = ? null ;
          ????????Document?xmldoc?
          = ?SCXMLUtils.getXMLDoc( " test.xml " );
          ????????
          if ?(xmldoc? == ? null )? {

          ????????????System.out.println(
          " Create?XML?Document?failed! " );
          ????????????
          return ;

          ????????}

          ????????Element?root?
          = ?xmldoc.getDocumentElement();
          ????????
          if ?(root? == ? null )? {

          ????????????System.out.println(
          " Get?root?of?XML?Document?failed! " );
          ????????????
          return ;

          ????????}

          ????????Node?node1?
          = ?SCXMLUtils.getChild(root,? " Master " );
          ????????
          if ?(node1? == ? null )? {

          ????????????System.out.println(
          " Get?node?failed! " );
          ????????????
          return ;

          ????????}

          ????????nodevalue?
          = ?SCXMLUtils.getTextValue(node1.getFirstChild());
          ????????
          if ?(nodevalue? == ? null )? {

          ????????????System.out.println(
          " Get?value?of?node?failed! " );
          ????????????
          return ;

          ????????}

          ????????System.out.println(
          " The?Master?is? " ? + ?nodevalue.trim());

          ????}


          }


          說明:首先調(diào)用getXMLDoc獲得test.xml的Document對象,然后調(diào)用getDocumentElement()函數(shù)獲得這個xml文件的根節(jié)點(即Home節(jié)點),然后
             執(zhí)行g(shù)etChild(root, "Master")返回根節(jié)點下一層中,名為Master的子節(jié)點對象node1,然后的執(zhí)行g(shù)etTextValue(node1.getFirstChild())獲得Master
             節(jié)點中的文本信息,這里特別說明一下,為什么是getTextValue(node1.getFirstChild())而不是getTextValue(node1),因為<Master>...</Master>
             之間的內(nèi)容也被認為是一個節(jié)點(也可以說成是一個text對象),并且這個節(jié)點是Master節(jié)點下的第一個子節(jié)點,所以用getFirstChild()
             來獲得Master節(jié)點中的文本內(nèi)容。

          測試用的test.xml:
          <?xml version="1.0" encoding="GBK"?>
          <Home>
          ??? <Master>fatcatman</Master>
          </Home>

          運行結(jié)果:
          The Master is fatcatman

          今天學(xué)習(xí)了如何Java讀取XML某個節(jié)點中的內(nèi)容,明天繼續(xù)學(xué)習(xí)修改XML的方法。
          posted on 2006-06-18 18:52 胖娃娃 閱讀(635) 評論(0)  編輯  收藏 所屬分類: XML

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 抚顺市| 陆良县| 军事| 安岳县| 朝阳区| 衢州市| 河曲县| 孙吴县| 新绛县| 乐都县| 习水县| 庆元县| 玛多县| 大新县| 阳曲县| 云阳县| 陵水| 明星| 永城市| 仲巴县| 大石桥市| 恩平市| 通海县| 米泉市| 清苑县| 高青县| 舞阳县| 信宜市| 卫辉市| 政和县| 晋州市| 龙胜| 临潭县| 仁怀市| 淳安县| 都兰县| 婺源县| 屯留县| 西平县| 库车县| 望奎县|