---
title: "解析XML"
author: "bryande"
date: "2010-08-05T14:09:44Z"
tags: []
categories: ["编程语言"]
slug: "xml"
draft: false
lastmod: "2010-08-05T14:09:44Z"
id: 729772
url: "https://bryande.iteye.com/blog/729772"
---

上机考试时的一道题没答好，有点悲剧。

面试还不错，问了很多底层实现的问题。

然后死在机试上了，原因多方面的。

 

 

 

```
public static void parseXml() {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
		File f = new File("d:/jobs.xml");
		Document d = null;
		try {
			d = db.parse(f);
			System.out.println("0" + d.getNodeName());
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Element e = d.getDocumentElement();
		NodeList nodes = e.getChildNodes();

		for (int i = 0; i < nodes.getLength(); ++i) {
			Node n = nodes.item(i);
			if (n instanceof Element) {
				Element childNode = (Element) n;
				NamedNodeMap attributes = childNode.getAttributes();
				for (int j = 0; j < attributes.getLength(); ++j) {
					Node node = attributes.item(j);
					String key = node.getNodeName();
					String value = node.getNodeValue();
					System.out.println(key);
					System.out.println(value);
				}

			}

		}

	}
```