• 解析xml文件


    package com.ss1.xml;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    
    
    public class Dom4jTest {
        
        public static void main(String[] args) throws IOException, DocumentException {
    
            /*Document document = DocumentHelper.createDocument();
            
            Element root = DocumentHelper.createElement("student");
            
            document.setRootElement(root);
            
            root.addAttribute("name", "zhangsan");
            Element helloElement = root.addElement("hello");
            
            Element worldElement = root.addElement("world");
            helloElement.setText("hello text");
            worldElement.setText("world text");
            
            XMLWriter xmlWriter = new XMLWriter();
            xmlWriter.write(document);
            /ss1/src/students.xml
        
            OutputFormat format = new OutputFormat("    ",true);
            XMLWriter xmlWriter2 = new XMLWriter(new FileOutputStream("student.xml"),format);
            xmlWriter2.write(document);
            */
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new File("test.xml"));
            
            Element rootElement = document.getRootElement();
            System.out.println(rootElement);
            
            
        }
    
    }
    package com.ss1.xml;
    
    import java.io.File;
    import java.util.Iterator;
    
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class Dom4jXml {
    
        public static void main(String[] args) throws DocumentException {
            File file = new File("test.xml");
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(file);
            
            Element rootElement = document.getRootElement();
            for(Iterator iterator = rootElement.elementIterator("students");iterator.hasNext();){
                Element element = (Element) iterator.next();
                Attribute nameAttribute = element.attribute("name");
                String name = nameAttribute.getValue();
                Attribute idAttribute = element.attribute("id");
                String id = idAttribute.getValue();
                System.out.println(name);
                System.out.println(id);
                
            }
        }
    }
  • 相关阅读:
    idea 添加不同的模板文件比如 . vm 文件
    Spring ioc及aop思想
    idea配置非maven项目配置及热部署配置
    AES对称式加密及https加密算法中数字证书(非对称加密)
    Solr(搜索引擎)简单介绍(不一定要会用但是要知道)
    回炉重造-基础规则之初识static关键字
    回炉重造-基础规则之初识IO流
    回炉重造-基础规则之类和对象
    回炉重造-基础规则之行为方法
    回炉重造-基础规则之Java的数组
  • 原文地址:https://www.cnblogs.com/tingbogiu/p/5786254.html
Copyright © 2020-2023  润新知