学习笔记-解析XML

来源:互联网 发布:最新的搞笑网络短剧 编辑:程序博客网 时间:2024/06/05 17:31
package java_web_programming;


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 xml_test {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("E:" + File.separator + "javaStudy"
+ File.separator + "test.xml");
NodeList nl = doc.getElementsByTagName("linkman");
if (nl.item(0) instanceof Element) {
Element el = (Element)nl.item(0);
System.out.println(el.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
} else System.out.println("非法XML");
}
}
0 0