dom4j的解析实例
- <books>
- <book>
- <author>Thomas</author>
- <title>Java从入门到放弃</title>
- <publisher>UCCU</publisher>
- </book>
- <book>
- <author>小白</author>
- <title>MySQL从删库到跑路</title>
- <publisher>Go Die</publisher>
- </book>
- <book>
- <author>PHPer</author>
- <title>Best PHP</title>
- <publisher>PHPchurch</publisher>
- </book>
- </books>
- package com;
- import org.dom4j.Document;
- import org.dom4j.Element;
- import org.dom4j.io.SAXReader;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import java.util.List;
- public class SAXReaderXML {
- public static void main(String[] args) throws Exception {
- SAXReader reader = new SAXReader();
- File xmlfile = new File("D:/books.xml");
- String xml = "<books><book><author>Thomas</author><title>Java从入门到放弃</title><publisher>UCCU</publisher>" +
- "</book><book><author>小白</author><title>MySQL从删库到跑路</title><publisher>GoDie</publisher></book>" +
- "<book><author>PHPer</author><title>BestPHP</title><publisher>PHPchurch</publisher></book></books>";
- Document fileDocument = reader.read(xmlfile);//从xml文件获取数据
- Document document = reader.read(new ByteArrayInputStream(xml.getBytes("utf-8")));//读取xml字符串,注意这里要转成输入流
- Element root = document.getRootElement();//获取根元素
- List<Element> childElements = root.elements();//获取当前元素下的全部子元素
- for (Element child : childElements) {//循环输出全部book的相关信息
- List<Element> books = child.elements();
- for (Element book : books) {
- String name = book.getName();//获取当前元素名
- String text = book.getText();//获取当前元素值
- System.out.println(name + ":" + text);
- }
- }
- //获取第二条书籍的信息
- Element book2 = childElements.get(1);
- Element author = book2.element("author");//根据元素名获取子元素
- Element title = book2.element("title");
- Element publisher = book2.element("publisher");
- System.out.println("作者:" + author.getText());//获取元素值
- System.out.println("书名:" + title.getText());
- System.out.println("出版社:"+publisher.getText());
- }
- }
1、读取xml数据
2、解析xml数据
3、获取子元素数据
4、输出元素信息
dom4j的解析实例的更多相关文章
- [工作代码]dom4j解析实例
工作中,我需要和另一个公司(A公司)共同开发一个模块,我写一个servlet接口,A公司携带xml格式的报文来访问.我采用流的形式读取,在处理后以流的形式写入,在返回(相应)给A公司. demo: p ...
- JSON解析实例——使用Json-lib
JSON解析实例——使用Json-lib Json-lib下载及使用 本文介绍用一个类库进行JSON解析. 工具下载地址:http://sourceforge.net/projects/json-li ...
- $使用dom4j可解析 返回&#x等字样的 html转义字符
如果以GET或POST请求某个系统返回,带有 $#x 那很有可能是axis服务器返回的. <?xml version="1.0" encoding="UTF-8&q ...
- $使用dom4j可解析 返回&#x等字样的 html转义字符【转】
如果以GET或POST请求某个系统返回,带有 $#x 那很有可能是axis服务器返回的. <?xml version="1.0" encoding="UTF-8&q ...
- JavaScript中hoisting(悬置/置顶解析/预解析) 实例解释,全局对象,隐含的全局概念
JavaScript中hoisting(悬置/置顶解析/预解析) 实例解释,全局对象,隐含的全局概念 <html> <body> <script type="t ...
- pcapng文件的python解析实例以及抓包补遗
为了弥补pcap文件的缺陷,让抓包文件可以容纳更多的信息,pcapng格式应运而生.关于它的介绍详见<PCAP Next Generation Dump File Format> 当前的w ...
- JDOM,dom4j方式解析XML
<?xml version="1.0" encoding="UTF-8"?> <dataSources> <!-- 定义MySQL ...
- 如何使用DOM4j+xpath 解析
1.首先去dom4j官网下载一个对应版本的JAR包 XML的配置文件 导入一个config.xml文件 下面是dom4j+xpath解析XML文件
- dom4j递归解析XML字符串所有子节点
/** * dom4j递归解析所有子节点 * * @param childElements * @param mapEle * @return */ public Map<String, Obj ...
随机推荐
- What is the relationship between Xcode, Swift and Cocoa?
Xcode is an IDE for developing Swift or Objective-C applications, which can use the Cocoa API (which ...
- Process Autocad by python
一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...
- MVC5中使用jQuery Post 二维数组和一维数组到Action
很久没有写了,最近在做一个MVC项目,这是我做的第一个MVC项目.之前可以说多MVC一点都不了解,今天把昨天遇到的一个问题记录下来.MVC大神就请飘过吧,跟我遇到同样问题的可以进来看看.遇到的第一个问 ...
- android-async-http框架
android-async-http 简单介绍:An asynchronous, callback-based Http client for Android built on top of Apac ...
- kubernetes-handbook 阅读笔记
文档地址 https://jimmysong.io/kubernetes-handbook/concepts/ Pod是在Kubernetes集群中运行部署应用或服务的最小单元,它是可以支持多容器的. ...
- yuicompressor
yui/yuicompressor: YUI Compressor https://github.com/yui/yuicompressor YUI Compressor 详细介绍 YUI ...
- IP address could not be resolved: Name or service not known
[root@test ~]# /usr/local/mysql/bin/mysqld2018-08-05T07:00:33.647509Z 0 [Warning] [MY-011070] [Serve ...
- kernel.panic
sysctl -a kernel.panic = 0kernel.panic_on_io_nmi = 0kernel.panic_on_oops = 1kernel.panic_on_stackove ...
- Leetcode:remove_duplicates_from_sorted_list
一. 题目 给定一个排好序的链表,删除全部反复的节点,使每个节点都仅仅出现一次 比如: Given 1->1->2, return 1->2. Given 1->1-& ...
- vue中引入百度统计
vue作为单页面的,引入百度统计,需要注意不少. 一.基本的流量统计 在index.html 入口文件中引入百度统计生成的一连串代码: var _hmt = _hmt || []; (function ...