zoukankan      html  css  js  c++  java
  • JavaEE XML的读写(利用JDom对XML文件进行读写)

    1.有关XML的写

    利用JDom2包,JDom2这个包中,至少引入org.jdom2.*;如果要进行XML文件的写出,则要进行导入org.jdom2.output.*;

    package com.littlepage.test1;
    
    import java.io.*;
    import org.jdom2.*;
    import org.jdom2.output.*;
    
    public class Test3 {
        public static void main(String[] args) {
            try {
                Element rootElement = new Element("rootElement");
                Document document = new Document(rootElement);
                rootElement.addContent(new Element("student1").addContent(new Element("name").addContent("Jack")).setAttribute("type","transfer student"));
                rootElement.addContent(new Element("student1").addContent(new Element("name").addContent("Nancy")));
                rootElement.addContent(new Element("student1").addContent(new Element("name").addContent("Lucy")));
                XMLOutputter xop = new XMLOutputter();
                //设置间距
                XMLOutputter out=new XMLOutputter();
                Format format=out.getFormat();
                format.setEncoding("GB2312");
                format.setIndent("
    	");
                out.setFormat(format);
                xop.output(document, new FileWriter("student.xml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    XML文件写入结果

    <?xml version="1.0" encoding="UTF-8"?>
    <rootElement>
        <student1 type="transfer student">
            <name>Jack</name>
        </student1>
        <student1>
            <name>Nancy</name>
        </student1>
        <student1>
            <name>Lucy</name>
        </student1>
    </rootElement>

    2.有关XML文件的读

    利用SAXBuilder,SAXBuilder在org.xml.input.*;中存在方法SAXBuilder,SAXBuilder是进行XML文件读入的一个类

    //递归打印XML的document
    public class Test4 {
        public static void main(String[] args) {
            try{
                Document document=new SAXBuilder().build("MyXML.xml");
                Element rootElement=document.getRootElement();
                recursionXML(rootElement);
            }catch(IOException|JDOMException e){
                e.printStackTrace();
            }
            
        }
        /**
         * recursion XML,for print the root element
         * @param element
         */
        public static void recursionXML(Element element){
            System.out.println(element.getName()+":"+element.getText());
            if(!element.getChildren().isEmpty()){
                List<Element> li=element.getChildren();
                for (Element element2 : li) {
                    recursionXML(element2);
                }
            }
        }
       /
    }
  • 相关阅读:
    <a href=”#”>与 <a href=”javascript:void(0)” 的区别
    win7与ubuntu11.04双系统
    winform中为控件设置鼠标样式
    如何得到格林威治标准时间(C#,SQL SERVER)
    winform中将光标定位到多行文本框的最下处
    VS2010 MV3开发时让jQuery支持智能感知
    转载:SQL Server 2005无日志文件附加数据库
    PowerPoint无法显示文件中某些幻灯片中的文字、图像或对象
    转载:版本简介
    使用 ADO.NET连接SQL Azure
  • 原文地址:https://www.cnblogs.com/littlepage/p/10492388.html
Copyright © 2011-2022 走看看