JAVA读取XML文件

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Xml {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("E://新建 文本文档 (3).xml"));
Element rootElement = document.getDocumentElement();

NodeList list = rootElement.getElementsByTagName("Header");
Element element = (Element) list.item(0);
System.out.println(element.getChildNodes().item(0).getNodeValue());

} catch (Exception e) {
System.out.println("exception:" + e.getMessage());
}
}
}

你可能感兴趣的:(读取xml)