package naixi;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Demo2 {
public static void main(String[] args) {
Demo2 d = new Demo2();
try {
Document document = d.getDocument("src/naixi/dom.xml");
//d.testDocument(document);
//d.show(document);
d.save(document, "src/naixi/dom1.xml");
d.add(document, "src/naixi/dom2.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Document getDocument(String path) throws Exception {
//获取 获取 稳当对象解析器的工厂类
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//得到解析器
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
//通过解析器获取文档对象模型
return documentBuilder.parse(path);
}
//解析xml的测试方法1
/* public void testDocument(Document document) {
//获取PhoneInfo
NodeList childNodes = document.getChildNodes();
// System.out.println(childNodes.getLength());
//得到下一个节点
Node phoneInfo = childNodes.item(0);//获取集合中的元素
//获取元素节点的名称
System.out.println(phoneInfo.getNodeName());
NodeList nodeList = phoneInfo.getChildNodes();
// System.out.println(nodeList.getLength());
//去除文本节点
for (int i = 0; i < nodeList.getLength(); i++) {
Node item = nodeList.item(i);
if(item.getNodeType()==Node.ELEMENT_NODE) {
//证明是元素节点
Element ele =(Element)item;
System.out.println(ele.getAttribute("name"));
NodeList childNodes2 = ele.getChildNodes();
for (int j = 0; j <childNodes2.getLength(); j++) {
Node item2 = childNodes2.item(j);
if(item2.getNodeType()==Node.ELEMENT_NODE) {
Element ele2 =(Element)item2;
System.out.println(ele2.getAttribute("name"));
}
}
}
}
}*/
//解析xml的测试方法2
public void show(Document document) {
NodeList bandNodeList = document.getElementsByTagName("Band");
for (int i = 0; i < bandNodeList.getLength(); i++) {
Element ele = (Element)bandNodeList.item(i);
System.out.println(ele.getAttribute("name"));
NodeList childNodes = ele.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node item = childNodes.item(j);
if(item.getNodeType()==Node.ELEMENT_NODE) {
Element ele2 = (Element)item;
System.out.println(ele2.getAttribute("name"));
}
}
}
}
/**
* 将内存中的document对象 保存至文件
* @throws Exception
*/
public void save(Document document,String path) throws Exception {
//得到转换工厂
TransformerFactory factory = TransformerFactory.newInstance();
//得到转换器
Transformer transformer = factory.newTransformer();
//设置字符集 必选
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
//设置是否换行 可选
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//创建Dom源码
DOMSource domSource = new DOMSource(document);
//创建流
StreamResult streamResult = new StreamResult(new File(path));
//转换成文件
transformer.transform(domSource, streamResult);
}
/**
* 添加一个节点并保存
* @param document
* @param path
* @throws Exception
*/
public void add(Document document,String path) throws Exception {
//创建元素节点band
Element band = document.createElement("Band");
band.setAttribute("name", "三星");
//创建元素节点note
Element type = document.createElement("type");
type.setAttribute("name", "note");
//关联band和note
band.appendChild(type);
//获取根节点
NodeList eleList = document.getElementsByTagName("PhoneInfo");
Node item = eleList.item(0);//根节点
//关联根节点和band节点
item.appendChild(band);
save(document,path);
}
}
DOM用法
最新推荐文章于 2022-06-15 15:17:17 发布