- package com.xml.test;
- /**
- * <p>
- * Title: DomXMLParser.class
- * </p>
- * <p>
- * Description: DOM解析XML文档
- * </p>
- *
- * @author apple90
- * @version 1.0
- * @since Sep 11, 2008
- */
- import java.io.File;
- import java.io.FileOutputStream;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.NodeList;
- public class DomXMLParser {
- public static void main(String arge[]) throws Exception {
- try {
- File xmlFile = new File("src/com/xml/test/employee.xml");
- File txtFile = new File("g://test.txt");
- FileOutputStream fos = new FileOutputStream(txtFile);
- DocumentBuilderFactory factory = DocumentBuilderFactory
- .newInstance();
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document document = builder.parse(xmlFile);
- NodeList nodeList = document.getElementsByTagName("employee");
- for (int i = 0; i < nodeList.getLength(); i++) {
- byte[] buf = ("员工姓名:"
- + document.getElementsByTagName("name").item(i)
- .getFirstChild().getNodeValue() + "/r/n")
- .getBytes();
- fos.write(buf);
- buf = (" 员工所在部门:"
- + document.getElementsByTagName("department").item(i)
- .getFirstChild().getNodeValue() + "/r/n")
- .getBytes();
- fos.write(buf);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }

被折叠的 条评论
为什么被折叠?



