本文主要是介绍DOM4J学习笔记 --- Java简单解析XML数据,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文本格式:
<note><to>George</to><from>John</from><heading>Reminder</heading><body>Don't forget the meeting!</body>
</note>
利用SAXReader读取,解析:
package zetyun;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import java.io.File;
import java.util.List;/*** Created by ryan on 17-7-31.*/
public class Main1 {public static void main(String[] args){load1("./xmlfiles/simple1.xml");}public static Document load1(String fileName){Document document = null;try{SAXReader saxReader = new SAXReader();document = saxReader.read(new File(fileName));Element root = document.getRootElement();List<Element> childElements = root.elements();for(Element i : childElements){System.out.println(i.getName() + " : " +i.getData());}}catch(Exception e){System.out.println("Not Found");e.printStackTrace();}return document;}
}
这篇关于DOM4J学习笔记 --- Java简单解析XML数据的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!