胖娃娃

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            4 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks

            今天學習了Java讀取XML的簡單方法,主要用到了javax.xml.pasers.*中的一些對象(注:可能會用到的包,xercesImpl.jar、xml-apis.jar
          jdom.jar
          ),首先構造一個簡單的操作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 ;

          ????????}


          ????}


          }

          說明:其中getXMLDoc用于返回一個指定xml文件的Document對象,getChild返回一個指定節點的下一層中某一個指定名稱的節點對象,
             getTextValue用于返回一個指定節點內的文本內容。

          用來測試這個工具類的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());

          ????}


          }


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

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

          運行結果:
          The Master is fatcatman

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

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


          網站導航:
           
          主站蜘蛛池模板: 老河口市| 安岳县| 孟连| 德阳市| 玉树县| 云和县| 洪雅县| 潜山县| 济南市| 德阳市| 和政县| 磐石市| 木里| 朝阳区| 长阳| 报价| 敦煌市| 勐海县| 横峰县| 顺昌县| 饶河县| 洛浦县| 垫江县| 张家口市| 瑞丽市| 黑河市| 凤冈县| 右玉县| 渝北区| 寿阳县| 滕州市| 平塘县| 宁安市| 岳西县| 丰都县| 海南省| 维西| 双江| 麦盖提县| 南靖县| 平舆县|