dom4J 의 증가,삭제,수정,검사 조작

8033 단어 dom4j
     ,         ,               。
        Dom4j。Dom4j   ?             XML Java  。       :dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP.
 ( Dom4j      ,     ,    XML、XPath XSLT。   Java  ,  Java    ,    DOM、SAX JAXP) 

         Dom4j     ,     Dom4j          ,        Jar        classPath 。              ,        ,        API     。
                 “ ”、“ ”、“ ”、“ ”    (           ?   、 、       ,        ),       。        XML               。           ,        、    、     ,           XML            ,      ,      。
 
    Dom4j    XML  : 


01. publicvoidcreateXml() throwsException { 

02.      Document document = DocumentHelper.createDocument(); 

03.       //          base 

04.      Element root = document.addElement( "base" ); 

05.      //        base    games    

06.      Element gamesElm = root.addElement( "games" ); 

07.      //  games         

08.      gamesElm.addComment( "      " ); 

09.      //  games   ,   game    

10.      Element gameElm = gamesElm.addElement( "game" ); 

11.      //  game        :    、     

12.      gameElm.addAttribute( "name" , "    " ); 

13.      gameElm.addAttribute( "company" , "UBISOFT" ); 

14.      

15.      //               

16.      gameElm = gamesElm.addElement( "game" ); 

17.      gameElm.addAttribute( "name" , "    " ); 

18.      gameElm.addAttribute( "company" , "KONAMI" ); 

19.   

20.      gameElm = gamesElm.addElement( "game" ); 

21.      gameElm.addAttribute( "name" , "    " ); 

22.      gameElm.addAttribute( "company" , "Activision" ); 

23.   

24.      //      author       

25.      Element author = gamesElm.addElement( "author" ); 

26.      //         “     ” 

27.      author.setText( "     " ); 

28.   

29.      //           

30.      XMLWriter writer = newXMLWriter( newFileOutputStream( newFile( 

31.          "G:/demo.xml" ))); 

32.      writer.write(document); 

33.      writer.close(); 

34. } 

    G       demo.xml  ,      :
 


01. <? xmlversion = "1.0"encoding = "UTF-8" ?> 

02. < base > 

03.      < games > 

04.          <!--      --> 

05.          < gamename = "    "company = "UBISOFT" /> 

06.          < gamename = "    "company = "KONAMI" /> 

07.          < gamename = "    "company = "Activision" /> 

08.          < author >     </ author > 

09.      </ games > 

10. </ base > 


 
1.             (         game   name    ,             )
 


01. publicvoidfindAttribute() throwsException { 

02.      /* 

03.      * SAX Simple API for XML   。  DOM 

04.      * (Document Object Model)  ,     XML     。 

05.      * Dom4j   SAXReader DOMReader       Document。 

06.      *   DOM SAX      ? 

07.      * SAX      API,DOM     API。 

08.      * DOM XML           ,       ; 

09.      * SAX        XML  ,                   。 

10.      */ 

11.      SAXReader reader = newSAXReader(); 

12.      /* SAXReader  XML          Document */ 

13.      Document document = reader.read( newFile( "G:/demo.xml" )); 

14.      /*     Document     games   game    name  ,    List */ 

15.      List list = document.selectNodes( "//games/game/@name" ); 

16.      Iterator iter = list.iterator(); 

17.      while(iter.hasNext()) { 

18.          /*   name  ,            */ 

19.          Attribute attribute = (Attribute) iter.next(); 

20.          System.out.println(attribute.getValue()); 

21.      } 

22. } 

2.     XML       (        author  ,           )
 


01. publicvoidfindNode() throwsException { 

02.      SAXReader reader = newSAXReader(); 

03.      Document document = reader.read( newFile( "G:/demo.xml" )); 

04.      List list = document.selectNodes( "//games/author" ); 

05.      Iterator iter = list.iterator(); 

06.      while(iter.hasNext()) { 

07.          /*   author  ,            */ 

08.          Element element = (Element) iter.next(); 

09.          System.out.println(element.getText()); 

10.      } 

11. } 

 :                 ,   (selectNodes)             (//games/game/@name、//games/author)。        XPath   。     XML           ,   XML              。
 //games/game/@name     :      games   game    name  。
 //gmaes/author     :      games   author   。 


 
           (    ,          DOTA)
 

01. publicvoidadd() throwsException { 

02.      SAXReader reader = newSAXReader(); 

03.      Document document = reader.read( newFile( "G:/demo.xml" )); 

04.      /*   games  ,            game    */ 

05.      List list = document.selectNodes( "//games" ); 

06.      Iterator iter = list.iterator(); 

07.      while(iter.hasNext()) { 

08.          Element gamesElm = (Element) iter.next(); 

09.          Element gameElm = gamesElm.addElement( "game" ); 

10.          gameElm.addAttribute( "name" , "  " ); 

11.          gameElm.addAttribute( "company" , "    " ); 

12.      } 

13.      XMLWriter writer = newXMLWriter( newFileOutputStream( newFile( 

14.              "G:/demo.xml" ))); 

15.      writer.write(document); 

16.      writer.close(); 

17. } 


 
           (     )
 

01. publicvoidremove() throwsException { 

02.      SAXReader reader = newSAXReader(); 

03.      Document document = reader.read( newFile( "G:/demo.xml" )); 

04.      /*   games   */ 

05.      List list = document.selectNodes( "//games" ); 

06.      Iterator iter = list.iterator(); 

07.      while(iter.hasNext()) { 

08.          Element gamesElm = (Element) iter.next(); 

09.          /*   games      game    */ 

10.          Iterator gameElms = gamesElm.elementIterator( "game" ); 

11.          while(gameElms.hasNext()) { 

12.              Element gameElm = (Element)gameElms.next(); 

13.              /*     game    name    “  ”,         */ 

14.              if( "  " .equalsIgnoreCase(gameElm.attributeValue( "name" ))) { 

15.              gamesElm.remove(gameElm); 

16.          } 

17.      } 

18. } 

19. XMLWriter writer = newXMLWriter( newFileOutputStream( newFile( 

20. "G:/demo.xml" ))); 

21. writer.write(document); 

22. writer.close(); 

23.   

24. } 



    UBISOFT     –     
 

01. publicvoidtest2() throwsException { 

02.      SAXReader reader = newSAXReader(); 

03.      Document document = reader.read( newFile( "G:/demo.xml" )); 

04.      /*      company   */ 

05.      List list = document.selectNodes( "//games/game/@company" ); 

06.      Iterator iter = list.iterator(); 

07.      while(iter.hasNext()) { 

08.          Attribute attribute = (Attribute) iter.next(); 

09.          /*   company     “UBISOFT”,            “    ” */ 

10.          if( "UBISOFT" .equalsIgnoreCase(attribute.getValue())) { 

11.              attribute.setValue( "    " ); 

12.          } 

13.      } 

14.   

15.      XMLWriter writer = newXMLWriter( newFileOutputStream( newFile( 

16.              "G:/demo.xml" ))); 

17.      writer.write(document); 

18.      writer.close(); 

19. } 

          ,     XPath    。      XPath       “//”,      ,     。           ,  document.getRootElement()   。
      XML         。  :XML     – document.getXMLEncoding()、XML    – document.asXML()、       – document.getPath()。
 
             ,         ,      

좋은 웹페이지 즐겨찾기