java讀取xml檔案資料
阿新 • • 發佈:2018-12-05
import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; public class ReadXml { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("C:\\Users\\20433\\Desktop\\sxfdemo.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("VALUE"); for (int i = 0; i < nl.getLength(); i++) { System.out.print("車牌號碼:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); System.out.println("車主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); } } catch (Exception e) { e.printStackTrace(); } }
}