《dom对xml文件内容的增删改查.docx》由会员分享,可在线阅读,更多相关《dom对xml文件内容的增删改查.docx(5页珍藏版)》请在三一办公上搜索。
1、dom对xml文件内容的增删改查dom解析xml文件内容,不说了,直接上代码. package cn.xmk.xml1; import java.io.File; import java.io.FileOutputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import
2、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.NodeList; public class DomTest2 public static void main(String args) throws Exception DomTest2 dt = new DomTest2; / dt.create; /创建 / dt.add;
3、/增加内容 / dt.modify; /修改内容 dt.remove; /移除内容 /删除xml private void remove try DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance; DocumentBuilder builder = dbFactory.newDocumentBuilder; Document doc = builder.parse(persons.xml); NodeList nlist = doc.getElementsByTagName(person); for (
4、int i = 0; i nlist.getLength; i+) Element e = (Element) nlist.item(i); String str = e.getElementsByTagName(name).item(0) .getTextContent; if (赵六.equals(str.trim) e.getParentNode.removeChild(e); result(doc); catch (Exception e) e.printStackTrace; /修改xml内容 private void modify try DocumentBuilderFactor
5、y dbFactory = DocumentBuilderFactory .newInstance; DocumentBuilder builder = dbFactory.newDocumentBuilder; Document doc = builder.parse(persons.xml); NodeList nlist = doc.getElementsByTagName(person); for (int i = 0; i nlist.getLength; i+) Element e = (Element) nlist.item(i); String str = e.getEleme
6、ntsByTagName(name).item(0) .getTextContent; if (赵六.equals(str.trim) e.getElementsByTagName(phone).item(0) .setTextContent(987654321); result(doc); catch (Exception e) e.printStackTrace; /创建xml public void create try DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance; DocumentBuil
7、der builder = dbFactory.newDocumentBuilder; / 创建xml对象 Document doc = builder.newDocument; Element linkman = doc.createElement(linkman); Element person = doc.createElement(person); Element name = doc.createElement(name); Element phone = doc.createElement(phone); name.setTextContent(赵六); phone.setText
8、Content(87654321); person.appendChild(name); person.appendChild(phone); linkman.appendChild(person); doc.appendChild(linkman); result(doc); catch (Exception e) e.printStackTrace; /增加一条内容 public void add try DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance; DocumentBuilder build
9、er = dbFactory.newDocumentBuilder; Document doc = builder.parse(persons.xml); NodeList nlist = doc.getElementsByTagName(linkman); Element e = (Element) nlist.item(0); Element person = doc.createElement(person); Element name = doc.createElement(name); Element phone = doc.createElement(phone); name.se
10、tTextContent(其他); phone.setTextContent(234324324324); person.appendChild(name); person.appendChild(phone); e.appendChild(person); result(doc); catch (Exception e) e.printStackTrace; / 进行输出 public void result(Document doc) try / 开始输出 TransformerFactory tFactory = TransformerFactory.newInstance; Transformer former = tFactory.newTransformer; DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(new FileOutputStream( new File(persons.xml); former.transform(domSource, result); catch (Exception e) e.printStackTrace; Xml文件内容如下: 其他234324324324其他234324324324