09/16 JavaWeb系统学习 XML的读取及其解析

初学:

需要用到的工具:dom4j.jar

读取xml文件:(导入的都是org.w3c.dom下的包)

基于DocumentBuilderFactory

		DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
		DocumentBuilder newDocumentBuilder = newInstance.newDocumentBuilder();
		Document parse = newDocumentBuilder.parse("src/person.xml");
		NodeList elementsByTagName = parse.getElementsByTagName("name");
		for(int i=0;i

回写xml文件:

		Node item = parse.getElementsByTagName("person").item(0);
		Element createElement = parse.createElement("subject");
		Text createTextNode = parse.createTextNode("software engineering");
		createElement.appendChild(createTextNode);
		item.appendChild(createElement);
		TransformerFactory newInstance2 = TransformerFactory.newInstance();
		Transformer newTransformer = newInstance2.newTransformer();
		newTransformer.transform(new DOMSource(parse), new StreamResult("src/person.xml"));

基于TransformerFactory

你可能感兴趣的:(09/16 JavaWeb系统学习 XML的读取及其解析)