java 使用JDOM生成 和读取 xml

jar包

       <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1.3</version>
        </dependency>    
  /**
     * 生成xml方法
     */
    public static void createXml() {
        try {
            // 1、生成一个根节点
            Element rss = new Element("Documnet");
            // 2、为节点添加属性
            rss.setAttribute("version", "2.0");
            // 3、生成一个document对象
            Document document = new Document(rss);

            Element channel = new Element("Project");
            rss.addContent(channel);
            Element prjInfo = new Element("PrjInfo");
            channel.addContent(prjInfo);

            Element title = new Element("Item");
            title.setAttribute("name","项目类别").setAttribute("value","国内常规项目");
            title.setText("国内最新新闻");
            prjInfo.addContent(title);

            Element title1 = new Element("Item");
            title1.setAttribute("name","项目编码").setAttribute("value","60-B7771K");
            prjInfo.addContent(title1);

            Format format = Format.getCompactFormat();
            // 设置换行Tab或空格
            format.setIndent("	");
            format.setEncoding("UTF-8");

            // 4、创建XMLOutputter的对象
            XMLOutputter outputer = new XMLOutputter(format);
            // 5、利用outputer将document转换成xml文档
            File file = new File("project.xml");
            outputer.output(document, new FileOutputStream(file));

            System.out.println("生成project.xml成功");
        } catch (Exception e) {
            System.out.println("生成project.xml失败");
        }
    }
    /**
     * 读取xml方法
     * @throws Exception
     */
    public static void readXml() throws Exception {
        //1.创建SAXBuilder对象
        SAXBuilder saxBuilder = new SAXBuilder();
        //2.创建输入流
        InputStream is = new FileInputStream(new File("project.xml"));
        //3.将输入流加载到build中
        Document document = saxBuilder.build(is);
        //4.获取根节点
        Element rootElement = document.getRootElement();
        //5.获取子节点
        List<Element> children = rootElement.getChildren();
        for (Element child : children) {
            getElement(child);
        }
    }

    /**
     * 递归 读取子节点
     * @param child
     */
    public static void getElement(Element child){
        List<Element> childrenList = child.getChildren();
        if(childrenList.size()>0){
            for (Element o : childrenList) {
                getElement(o);
            }
        }
        System.out.println("通过name获取属性值:"+child.getAttribute("name"));
        List<Attribute> attributes = child.getAttributes();
        //打印属性
        for (Attribute attr : attributes) {
            System.out.println(attr.getName()+":"+attr.getValue());
        }
        for (Element o : childrenList) {
            System.out.println("节点名:"+o.getName()+"---"+"节点值:"+o.getValue());
        }
    }

参考

https://www.cnblogs.com/lin346112883/p/10212985.html

https://blog.csdn.net/m0_37499059/article/details/80505567

posted @ 2020-04-13 09:21  closeIt  阅读(288)  评论(0编辑  收藏  举报