13-day 12_XML


今日の内容
1. XML
    1.   
    2.   
    3.   
XML:
1.   :Extensible Markup Language        
    *    :        。     

    *   
        *     
            1.     
            2.       
    * xml html   
        1. xml        ,html      。
        2. xml     ,html    
        3. xml      ,html     

    * w3c:     

2.   :
    *     :
        1. xml       .xml
        2. xml            
        3. xml            
        4.          (    )   
        5.         
        6. xml         
    *     :
        
        
            
                zhangsan
                23
                male
            
        
            
                lisi

                24
                male
            
            
                  
                   
                 
            
        
        
    *     :
        1.     
            1.   :
                      :
            2.     :
                * version:   ,     
                * encoding:    。                ,   :ISO-8859-1
                * standalone:    
                    *   :
                        * yes:       
                         * no:      
                  :
        2.   (  ):  css 
            * 
        3.   :        
            *   :
                *         、          
                *                 
                *         xml(   XML、Xml   )   
                *          

        4.   :
            id     
        5.   :
            * CDATA :              
                *   :  
                :
                
                    b && a>c){}
                    ]]>
                

    *   :  xml       
        *         (   ):
            1.    xml       
            2.            
        
        *   :
            1. DTD:         
            2. Schema:         


        * DTD:
            *   dtd   xml   
                *   dtd:        xml   
                         :
                            
                            
                                    
                                    
                                    
                                    
                                    
                                    
                                    ]>
                            
                            
                                
                                    
                                    
                                    
                                
                             
                *   dtd:            dtd   
                    *   :
                            :
                              :
                               dtd  
                                    
                                    
                                    
                                    
                                    
                                    
                               xml  
                                    
                                    
                                    
                                        
                                            
                                            
                                            
                                        
                                    

                    *   :   " "dtd     URL">
                            :


        * Schema:
            *   :
                1.  xml      
                2.  xsi  .  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                3.  xsd      .  xsi:schemaLocation="http://www.itcast.cn/xml  student.xsd"
                4.    xsd        ,      xmlns="http://www.itcast.cn/xml" 

            



3.   :  xml  ,             
    *   xml  
        1.   (  ):             
        2.   :          xml   。      

    *   xml   :
        1. DOM:               ,        dom 
            *   :    ,       CRUD     
            *   :   
        2. SAX:    ,       。
            *   :    。
            *   :    ,     


    
    * xml      :
        1. JAXP:sun        ,  dom sax    
        2. DOM4J:          
        3. Jsoup:jsoup    Java  HTML   ,       URL  、HTML    。           API,   DOM,CSS     jQuery             。
        4. PULL:Android          ,sax   。


    * Jsoup:jsoup    Java  HTML   ,       URL  、HTML    。           API,   DOM,CSS     jQuery             。
        *     :
            *   :
                1.   jar jsoup-1.11.2.jar
                2.   Document  
                3.        Element  
                4.     

        *   :
            public class JsoupDemo1 {
                public static void main(String[] args) throws IOException {            
                     //2.1  student.xml path
                    String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
                    //2.2  xml  ,       ,  dom --->Document
                    Document document = Jsoup.parse(new File(path), "utf-8");
                    //3.       Element
                    Elements elements = document.getElementsByTag("name");
            
                    System.out.println(elements.size());
                    //3.1     name Element  
                    Element element = elements.get(0);
                    //3.2    
                    String name = element.text();
                    System.out.println(name);
                }
            }
               
                xml    
                    
                     
                         
                             tom
                             18
                             male
                         
                        
                            jack
                            19
                            male
                        
                     
    *      :
        1. Jsoup:   ,    html xml  ,  Document
            * parse:  html xml  ,  Document
                * parse(File in, String charsetName):  xml html   。
                * parse(String html):  xml html   
                * parse(URL url, int timeoutMillis):           html xml     
                     :
                        /**
                         * Jsoup    
                         */
                        public class JsoupDemo2 {
                            public static void main(String[] args) throws IOException {
                                //2.1  student.xml path
                                String path=JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath();
                        
                               /* //    Xml      1
                                // parse(File in, String charsetName):  xml html   。
                                //2.2  xml  ,       ,  dom ---->Document
                                Document document=Jsoup.parse(new File(path),"utf-8");
                                System.out.println(document);
                                */
                        
                               /*
                                //    Xml      2
                               //parse(String html):  xml html   
                                String str="
" + "
" + " \t
" + " \t\ttom
" + " \t\t18
" + " \t\tmale
" + " \t

" + "\t
" + "\t\tjack
" + "\t\t19
" + "\t\tmale
" + "\t

" + "
"; Document document=Jsoup.parse(str); System.out.println(document); */ /* // Xml 3 //parse(URL url, int timeoutMillis): // html xml */ URL url=new URL("http://www.walmw.club");// Document document=Jsoup.parse(url,10000); System.out.print(document); } } xml tom 18 male jack 19 male 2. Document: 。 dom * Element * getElementById(String id): id element * getElementsByTag(String tagName): * getElementsByAttribute(String key): * getElementsByAttributeValue(String key, String value): 3. Elements: Element 。 ArrayList : /** * Document/Element */ public class JsoupDemo3 { public static void main(String[] args) throws IOException { //1 student.xml path String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); //2. Document Document document=Jsoup.parse(new File(path),"utf-8"); //3. //3.1 student Elements elements=document.getElementsByTag("student"); System.out.println(elements); System.out.println("--------------------"); //3.2 id Elements elements1=document.getElementsByAttribute("id"); System.out.println(elements1); System.out.println("--------------------"); //3.3 number heima_0001 Elements elements2=document.getElementsByAttributeValue("number","heima_0001"); System.out.println(elements2); } } : "E:\IntelliJ IDEA 2019.2.4\jbr\bin\java.exe" "-javaagent:E:\IntelliJ IDEA 2019.2.4\lib\idea_rt.jar=8528:E:\IntelliJ IDEA 2019.2.4\bin" -Dfile.encoding=UTF-8 -classpath F:\JAVA_HeiMa_xuexi_lianxi\day12_xml\out\production\day12_xml;F:\JAVA_HeiMa_xuexi_lianxi\day12_xml\libs\jsoup-1.11.2.jar cn.itcast.jsoup.JsoupDemo3 tom 18 male jack 19 male -------------------- tom -------------------- tom 18 male Process finished with exit code 0 4. Element: 1. * getElementById(String id): id element * getElementsByTag(String tagName): * getElementsByAttribute(String key): * getElementsByAttributeValue(String key, String value): 2. * String attr(String key): 3. * String text(): * String html(): ( ) : /** * Document */ public class JsoupDemo4 { public static void main(String[] args) throws IOException { //1 student.xml path String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); //2. Document Document document = Jsoup.parse(new File(path), "utf-8"); //3. document name , name , Elements elements=document.getElementsByTag("name"); System.out.println(elements.size()); System.out.println("------------------"); //4. Element Element element_student=document.getElementsByTag("student").get(0); Elements ele_name=element_student.getElementsByTag("name"); System.out.println(ele_name.size()); System.out.println("------------------"); //5. student String number=element_student.attr("number"); System.out.println(number); System.out.println("------------------"); //6. String text=ele_name.text(); String html=ele_name.html(); System.out.println(text); System.out.println(html); } XML : 18 male jack 19 male : 2 ------------------ 1 ------------------ heima_0001 ------------------ Process finished with exit code 0 5. Node: * Document Element * : 1. selector: * :Elements select(String cssQuery) * : Selector : /** * selector: */ public class JsoupDemo5 { public static void main(String[] args) throws IOException { //1 student.xml path String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); //2. Document Document document = Jsoup.parse(new File(path), "utf-8"); //3 name Elements elements=document.select("name"); System.out.println(elements); System.out.println("--------------"); //4 id itcast Elements elements1=document.select("#itcast"); System.out.println(elements1); System.out.println("--------------"); //5. student number heima_0001 age //5.1 student number heima_0001 Elements elements2=document.select("student[number=\"heima_0001\"]"); System.out.println(elements2); System.out.println("------------"); //5.2 student number heima_0001 age Elements elements3=document.select("student[number=\"heima_0001\"] > age"); System.out.println(elements3); } } xml : 18 male jack 19 male : jack -------------- -------------- 18 male ------------ 18 Process finished with exit code 0 2. XPath:XPath XML , XML( ) * Jsoup Xpath jar 。JsoupXpath-0.3.2.jar * w3cshool , xpath * : /** * XPath */ public class JsoupDemo6 { public static void main(String[] args) throws IOException, XpathSyntaxErrorException { //1 student.xml path String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); //2. Document Document document = Jsoup.parse(new File(path), "utf-8"); //3. document , JXdocument(document); JXDocument jxDocument=new JXDocument(document); //4. xpath //4.1 student List jxNodes=jxDocument.selN("//student"); // for(JXNode jxNode:jxNodes){ System.out.println(jxNode); System.out.println("-----------"); } System.out.println("^^^^^^^^^^^"); //4.2 student name List jxNodes2=jxDocument.selN("//student/name"); // for(JXNode jxNode:jxNodes2){ System.out.println(jxNode); System.out.println("*********"); } System.out.println("^^^^^^^^^^^"); //4.3 student id name List jxNodes3=jxDocument.selN("//student/name[@id]"); // for(JXNode jxNode:jxNodes3){ System.out.println(jxNode); System.out.println("&&&&&&&&&&"); } System.out.println("^^^^^^^^^^^"); //4.4 student id name itcast List jxNodes4=jxDocument.selN("//student/name[@id='itcast']"); // for(JXNode jxNode:jxNodes4){ System.out.println(jxNode); System.out.println("###########"); } } } xml : 18 male jack 19 male : 18 male ----------- jack 19 male ----------- ^^^^^^^^^^^ ********* jack ********* ^^^^^^^^^^^ &&&&&&&&&& ^^^^^^^^^^^ ########### Process finished with exit code 0