​个人博客网:https://wushaopei.github.io/    (你想要这里多有)

package com.example.poiutis.xml;

import com.example.poiutis.model.InvoiceOrder;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; /**
* @ClassName 用JDOM方式读取xml文件
* @Description TODO
* @Author wushaopei
* @Date 2019/8/1 15:14
* @Version 1.0
*/
public class ReadXMLByJDom { private List<InvoiceOrder> invoiceOrders = null;
private InvoiceOrder invoiceOrder = null; public List<InvoiceOrder> getInvoiceOrders(String fileName) {
SAXBuilder saxBuilder = new SAXBuilder();
try {
Document document = saxBuilder.build(new FileInputStream(fileName));
//获取根节点bookstore
Element rootElement = document.getRootElement();
//获取根节点的子节点,返回子节点的数组
List<Element> bookList = rootElement.getChildren();
invoiceOrders = new ArrayList<InvoiceOrder>();
for (Element bookElement : bookList) {
invoiceOrder = new InvoiceOrder();
//获取bookElement的属性
List<Attribute> bookAttributes = bookElement.getAttributes();
for (Attribute attribute : bookAttributes) {
if (attribute.getName().equals("id")) {
String id = attribute.getValue(); //System.out.println(id);
invoiceOrder.setId(Integer.parseInt(id));
}
}
//获取bookElement的子节点
List<Element> children = bookElement.getChildren(); for (Element child : children) {
if (child.getName().equals("invoiceOrder")) {
String invoiceOrderid = child.getValue();
invoiceOrder.setInvoiceOrder(invoiceOrderid);
// System.out.println("发票单号"+"---"+invoiceOrderid);
} else if (child.getName().equals("companyName")) {
String companyName = child.getValue();
invoiceOrder.setCompanyName(companyName);
// System.out.println("公司名"+"---"+content);
} else if (child.getName().equals("taxNumber")) {
String taxNumber = child.getValue();
invoiceOrder.setTaxNumber(taxNumber);
// System.out.println("金额"+"---"+content);
} else if (child.getName().equals("accountBank")) {
String accountBank = child.getValue();
invoiceOrder.setAccountBank(accountBank);
// System.out.println("开户行"+"---"+content);
} else if (child.getName().equals("companyAddress")) {
String companyAddress = child.getValue();
invoiceOrder.setCompanyAddress(companyAddress);
// System.out.println("公司地址"+"---"+content);
} else if (child.getName().equals("bankNumber")) {
String bankNumber = child.getValue();
invoiceOrder.setBankNumber(bankNumber);
// System.out.println("账号"+"---"+bankNumber);
} else if (child.getName().equals("companyTelephone")) {
String companyTelephone = child.getValue();
invoiceOrder.setCompanyTelephone(companyTelephone);
// System.out.println("公司电话"+"---"+companyTelephone);
} else if (child.getName().equals("accountName")) {
String accountName = child.getValue();
invoiceOrder.setAccountName(accountName);
// System.out.println("账户类型"+"---"+accountName);
} } invoiceOrders.add(invoiceOrder);
invoiceOrder = null; } } catch (FileNotFoundException e) { e.printStackTrace();
} catch (JDOMException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} return invoiceOrders; }
public static void main(String[] args) { String fileName = "src/main/resources/invoiceOrder.xml";
List<InvoiceOrder> invoiceOrders= new ReadXMLByJDom().getInvoiceOrders(fileName);
for(InvoiceOrder invoiceOrder : invoiceOrders){
System.out.println(invoiceOrder);
} } }

最新文章

  1. zip压缩与解压缩示例
  2. PHP获取IP地址
  3. 怎么在php里面利用str_replace防注入
  4. 第四篇 :微信公众平台开发实战Java版之完成消息接受与相应以及消息的处理
  5. noproguard.classes-with-local.dex
  6. FTP进行上传下载文件
  7. system.exit(0) vs system.exit(1)
  8. jquery 封装
  9. 基于Vue.js的大型报告页项目实现过程及问题总结(一)
  10. Java基础学习笔记二十 IO流
  11. Maven 项目管理工具基础入门系列(二)
  12. jexus System.BadImageFormatException Details: Non-web exception. Exception origin (name of application or object): App_global.asax_ai3fjolq.
  13. unittest框架(惨不忍睹低配版)
  14. NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus
  15. java框架之SpringCloud(4)-Ribbon&amp;Feign负载均衡
  16. MySQL乐观锁和悲观锁的概念和原理
  17. 恶意软件正在利用SSLserver窃取用户个人信息!
  18. VirtualBox虚拟机安装ubuntu系统(图文详解)
  19. python3.6执行AES加密及解密方法
  20. WebService 与 Socket 区别

热门文章

  1. 【FreeRTOS学习04】小白都能懂的 Queue Management 消息队列使用详解
  2. Spring Cloud 系列之 Config 配置中心(三)
  3. 24款WordPress网站AI插件大盘点
  4. 设计模式GOF23之工厂模式01
  5. [hdu4513]常规dp
  6. 【转载】文件上传那些事儿,文件ajax无刷上传
  7. java读取文件内容常见几种方式
  8. 「雕爷学编程」Arduino动手做(28)——RGB全彩LED模块
  9. 详解vue生命周期及每个阶段适合进行的操作
  10. 函数的不同调用方式决定了this的指向不同