乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      java讀XML文件例子

       鳳舞天煌 2007-11-15
      XML文件 book.xml 如下:

      <?xml version="1.0" encoding="gb2312"?>
      <book>
      <person>
      <first>Kiran</first>
      <last>Pai</last>
      <age>22</age>
      </person>
      <person>
      <first>Bill</first>
      <last>Gates</last>
      <age>46</age>
      </person>
      <person>
      <first>Steve</first>
      <last>Jobs</last>
      <age>40</age>
      </person>
      </book>

      程序輸出

      Root element of the doc is book
      Total no of people: 3
      First Name: Kiran
      Last Name: Pai
      Age: 22
      First Name: Bill
      Last Name: Gates
      Age: 46
      First Name: Steve
      Last Name: Jobs
      Age: 40

      程序清單:

      import java.io.File;
      import org.w3c.dom.Document;
      import org.w3c.dom.*;

      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;
      import org.xml.sax.SAXException;
      import org.xml.sax.SAXParseException;

      public class ReadAndPrintXMLFile{

          public static void main (String argv []){
          try {

                  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                  Document doc = docBuilder.parse (new File("book.xml"));

                  // normalize text representation           
                  doc.getDocumentElement ().normalize ();
                  System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


                  NodeList listOfPersons = doc.getElementsByTagName("person");
                  int totalPersons = listOfPersons.getLength();
                  System.out.println("Total no of people : " + totalPersons);

                  for(int s=0; s<listOfPersons.getLength() ; s++){


                      Node firstPersonNode = listOfPersons.item(s);
                      if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){


                          Element firstPersonElement = (Element)firstPersonNode;

                          //-------                   
                          NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                          Element firstNameElement = (Element)firstNameList.item(0);

                          NodeList textFNList = firstNameElement.getChildNodes();
                          System.out.println("First Name : " +
                                 ((Node)textFNList.item(0)).getNodeValue().trim());

                          //-------                   
                        NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                          Element lastNameElement = (Element)lastNameList.item(0);

                          NodeList textLNList = lastNameElement.getChildNodes();
                          System.out.println("Last Name : " +
                                 ((Node)textLNList.item(0)).getNodeValue().trim());

                          //----                   
                          NodeList ageList = firstPersonElement.getElementsByTagName("age");
                          Element ageElement = (Element)ageList.item(0);

                          NodeList textAgeList = ageElement.getChildNodes();
                          System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim());

                          //------

                      }//end of if clause

                  }//end of for loop with s var

              }
              catch (SAXParseException err) {
                     System.out.println ("** Parsing error" + ", line "
                                                + err.getLineNumber () + ", uri " + err.getSystemId ());
                     System.out.println(" " + err.getMessage ());

              }
              catch (SAXException e) {
                      Exception x = e.getException ();
                      ((x == null) ? e : x).printStackTrace ();

              }
              catch (Throwable t) {
                      t.printStackTrace ();
              }
              //System.exit (0);
          }//end of main

      }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀(guān)點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多