zoukankan      html  css  js  c++  java
  • Java 解析 XML文件

    ​个人博客网: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);
            }
           
        }
    
    }

  • 相关阅读:
    win10下安装Anaconda3+keras+tensorflow
    Ubuntu 下安装安装 Anaconda3 与Keras
    ESP32开发之Windows开发环境
    LinuxE2系统刷机后OSCAM安装与读卡器设置
    安德鲁1.2Ku全下125C波(CCTV4K除外)
    安德鲁1.2Ku使用感受
    ubuntu10.04换官方源
    密室逃脱游戏解决方案-森林迷宫-炸弹人等
    Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
    ESP32 做Web服务器 http Server步骤
  • 原文地址:https://www.cnblogs.com/wushaopei/p/11989103.html
Copyright © 2011-2022 走看看