ITeye博客

解析XML

bryande · 2010-08-05 14:09 · 编程语言 · Markdown

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

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

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

 

 

 

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);
				}

			}

		}

	}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。