本文實例講述了java實現(xiàn)簡單解析XML文件功能。分享給大家供大家參考,具體如下:
站在用戶的角度思考問題,與客戶深入溝通,找到臨潭網(wǎng)站設(shè)計與臨潭網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋臨潭地區(qū)。package demo; import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class JieXiXML { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory bdf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = bdf.newDocumentBuilder(); Document document = db.parse(new File("user.xml")); NodeList list = document.getElementsByTagName("usa"); System.out.println("創(chuàng)新互聯(lián)測試結(jié)果:"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element)list.item(i); String name = element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue(); System.out.println(name); String pass = element.getElementsByTagName("pass").item(0).getFirstChild().getNodeValue(); System.out.println(pass); System.out.println("------------------"); } } }