Java調用XML的方法

來源:互聯網
上載者:User
XML格式如下1:<?xml version="1.0" encoding="utf-8" ?>
<!--CyPLATE Simulator Configuration-->
<CyPLATE>  
      <BackgroundChar>◇</BackgroundChar>  
      <ForegroundChar>◆</ForegroundChar>  
      <TemplateWidth>170</TemplateWidth>  
      <TemplateHeigth>72</TemplateHeigth>  
      <WaitTimes>2000</WaitTimes>  
      <IntervalTimes>1000</IntervalTimes>  
      <FontName>default</FontName>  
      <FontSize>8</FontSize>
</CyPLATE>實現方法:DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            File xmlFile = new File("/home/creat/SYS/CYPLATE/CONFIG"); 
            Document doc = builder.parse(xmlFile);
            Element root = doc.getDocumentElement();
            BackgroundChar = root.getElementsByTagName("BackgroundChar").item(0).getFirstChild().getNodeValue();// BackgroundChar
            ForegroundChar =root.getElementsByTagName("ForegroundChar").item(0).getFirstChild().getNodeValue();// ForegroundChar
            TemplateWidth =Integer.valueOf(root.getElementsByTagName("TemplateWidth").item(0).getFirstChild().getNodeValue());// TemplateWidth
            TemplateHeigth =Integer.valueOf(root.getElementsByTagName("TemplateHeigth").item(0).getFirstChild().getNodeValue());// TemplateHeigth
            WaitTimes = Integer.valueOf(root.getElementsByTagName("WaitTimes").item(0).getFirstChild().getNodeValue());// WaitTimes
            IntervalTimes = Integer.valueOf(root.getElementsByTagName("IntervalTimes").item(0).getFirstChild().getNodeValue());// IntervalTimes
            FontName= root.getElementsByTagName("FontName").item(0).getFirstChild().getNodeValue();//FontName     
            FontSize= Integer.valueOf(root.getElementsByTagName("FontSize").item(0).getFirstChild().getNodeValue());//FontSize
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }XML格式如下2:<?xml version="1.0" encoding="GB2312" standalone="no"?>   
<books>   
    <book email="zhoujunhui">   
        <name>rjzjh</name>   
        <price>jjjjjj</price>   
    </book>   
</books>   DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();    
        try {    
            DocumentBuilder domBuilder = domfac.newDocumentBuilder();    
            InputStream is = new FileInputStream(new File("D:/test1.xml"));    
            Document doc = domBuilder.parse(is);    
            Element root = doc.getDocumentElement();    
            NodeList books = root.getChildNodes();    
            if(books!=null){    
                for (int i = 0; i < books.getLength(); i++) {    
                    Node book = books.item(i);    
                     if(book.getNodeType()==Node.ELEMENT_NODE) {    
                            //(7)取得節點的屬性值    
                            String email=book.getAttributes().getNamedItem("email").getNodeValue();    
                            System.out.println(email);    
                            //注意,節點的屬性也是它的子節點。它的節點類型也是Node.ELEMENT_NODE    
                            //(8)輪循子節點    
                            for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()) {    
                                if(node.getNodeType()==Node.ELEMENT_NODE) {    
                                    if(node.getNodeName().equals("name")) {    
                                        String name=node.getNodeValue();    
                                        String name1=node.getFirstChild().getNodeValue();    
                                        System.out.println(name);    
                                        System.out.println(name1);    
                                    }    
                                    if(node.getNodeName().equals("price")) {    
                                        String price=node.getFirstChild().getNodeValue();    
                                        System.out.println(price);    
                                    }    
                                }    
                            }    
                     }    
                }    
            }    
        } catch (Exception e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        }

 

1)得到 DOM 解析器的工廠執行個體      
DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance(); 

2)然後從 DOM 工廠獲得 DOM 解析器 
DocumentBuilder dombuilder=domfac.newDocumentBuilder(); 

3 )把要解析的 XML 文檔轉化為輸入資料流,以便 DOM 解析器解析它 
InputStream is= new  FileInputStream("test1.xml");         

( 4 )解析 XML 文檔的輸入資料流,得到一個 Document 

 Document doc=dombuilder.parse(is); 

( 5 )得到 XML 文檔的根節點 

Element root=doc.getDocumentElement(); 

( 6 )得到節點的子節點 

  NodeList books=root.getChildNodes(); 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.