Java使用DOM读写 XML

2017-01-13 19:14:22来源:CSDN作者:Al_assad人点击

使用DOM对象处理XML
XML文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <persons>
  3. <person>
  4. <name>Assad</name>
  5. <age>22</age>
  6. <home>Shanghai</home>
  7. </person>
  8. <person>
  9. <name>John</name>
  10. <age>19</age>
  11. <home>GuangZhou</home>
  12. </person>
  13. </persons>


1、读取XML文件
  1. //Java读取XML文件
  2. public static void readXML(){
  3. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  4. try{
  5. DocumentBuilder builder = factory.newDocumentBuilder();
  6. Document document = builder.parse(new File("test.xml"));
  7. Element rootElement = document.getDocumentElement(); //获取Document对象的根元素;
  8. NodeList personList = rootElement.getElementsByTagName("person"); //获取节点元素列表
  9. for(int i=0;i<personList.getLength();i++){ //循环处理person节点对象
  10. Element person = (Element)personList.item(i);
  11. for(Node node = person.getFirstChild() ; node!=null ; node = node.getNextSibling()){ //循环处理每个perosn节点中的所有子节点
  12. if(node.getNodeType() == Node.ELEMENT_NODE){
  13. String key = node.getNodeName(); //获取该节点的标签名
  14. String value = node.getFirstChild().getNodeValue(); //获取该节点的值 System.out.println(key+":"+value);
  15. }
  16. }
  17. }
  18. }catch(Exception ex){
  19. ex.printStackTrace();
  20. }
  21. }

2、修改XML文件并保存
  1. public static void updateAndSave(){
  2. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  3. try{
  4. DocumentBuilder builder = factory.newDocumentBuilder();
  5. Document document = builder.parse(new File("test.xml"));
  6. Element persons = document.getDocumentElement(); //获取根节点<persons>
  7. //在<persons>节点下增加一个新的<person>节点
  8. Element person = document.createElement("person");
  9. Element name = document.createElement("name"); //创建子节点
  10. name.setTextContent("Petter");
  11. Element age = document.createElement("age");
  12. age.setTextContent("20");
  13. Element home = document.createElement("home");
  14. home.setTextContent("ShengZhen");
  15. person.appendChild(name); //添加子节点
  16. person.appendChild(age);
  17. person.appendChild(home);
  18. persons.appendChild(person);
  19. //在<persons>节点下删除<name>="assad"的<perosn>节点;
  20. NodeList personList = persons.getElementsByTagName("person");
  21. for(int i=0;i<personList.getLength();i++){
  22. Element person_i = (Element)personList.item(i);
  23. Element name_i = (Element)person_i.getElementsByTagName("name").item(0);
  24. if(name_i.getFirstChild().getNodeValue().equals("Assad")){
  25. persons.removeChild(person_i); //通过该节点的父节点删除子节点的方式删除该子节点;
  26. break;
  27. }
  28. }
  29. //保存至文件
  30. TransformerFactory factory_t = TransformerFactory.newInstance();
  31. Transformer former = factory_t.newTransformer();
  32. former.setOutputProperty(OutputKeys.INDENT,"yes"); //设置输出时XML换行;
  33. former.transform(new DOMSource(document),new StreamResult(new File("test_update.xml")));
  34. }catch(Exception ex){
  35. ex.printStackTrace();
  36. }
  37. }

3、创建XML对象并保存
  1. public static void create(){
  2. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  3. try{
  4. DocumentBuilder builder = factory.newDocumentBuilder();
  5. Document doc = builder.newDocument();
  6. //创建节点树
  7. Element bookstore = doc.createElement("bookstore"); //创建根节点
  8. Element book = doc.createElement("book"); //创建其他节点
  9. Element title = doc.createElement("title");
  10. title.setTextContent("How to Learn Java");
  11. Element author = doc.createElement("author");
  12. author.setTextContent("Json");
  13. book.appendChild(title);
  14. book.appendChild(author);
  15. bookstore.appendChild(book);
  16. doc.appendChild(bookstore);
  17. //保存
  18. TransformerFactory factory_t = TransformerFactory.newInstance();
  19. Transformer former = factory_t.newTransformer();
  20. former.setOutputProperty(OutputKeys.INDENT,"yes");
  21. former.transform(new DOMSource(doc),new StreamResult(new File("test_create.xml")));
  22. }catch(Exception ex){
  23. ex.printStackTrace();
  24. }
  25. }



最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台