SAX 解析到文件,缓存到内存

2024-04-01 21:58
文章标签 内存 解析 缓存 sax

本文主要是介绍SAX 解析到文件,缓存到内存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目的
    通过一个小的SAX例子,我们更清晰的理解SAX的工作原理。

    本文例子主要实现:
    1. 将每个Employee信息输出到自己的文件中,文件名是以Employee ID和Employee Name来命名的,注意,观察代码中是如何得到Employee ID和Employee Name;
    2. 将每个Employee信息存入到Map中,其中,Map中的每个Value对应一个Employee的Collection,Map中的每个Key对应该Employee的ID。


    package shuai.study.sax.demo;  import java.io.File;  import java.io.IOException;  import java.util.Collection;  import java.util.HashMap;  import java.util.LinkedList;  import java.util.Map;  import javax.xml.parsers.ParserConfigurationException;  import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParserFactory;  import org.apache.commons.io.FileUtils;  import org.apache.commons.lang3.StringUtils;  import org.xml.sax.Attributes;  import org.xml.sax.SAXException;  import org.xml.sax.helpers.DefaultHandler;  /** * @author shengshu *  */  public class SaxHandler extends DefaultHandler {  private final static String leafNodeText = "|firstname|;|lastname|;|sex|;|country|;|province|;|city|;|village|;|mobile|;|mail|;|qq|;|postcode|;|profession|";  private Map<String, Collection<String>> companyMap = null;  private Collection<String> employeeCollection = null;  private String currentValue = null;  private String currentCharacters = null;  private StringBuffer idAndNameStringBuffer = null;  public SaxHandler(File inputFile) {  this.parseDocument(inputFile);  }  private void parseDocument(File inputFile) {  SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();  try {  SAXParser saxParser = saxParserFactory.newSAXParser();  saxParser.parse(inputFile, this);  } catch (ParserConfigurationException pce) {  pce.printStackTrace();  } catch (SAXException saxe) {  saxe.printStackTrace();  } catch (IOException ioe) {  ioe.printStackTrace();  }  }  @Override  public void startDocument() throws SAXException {  super.startDocument();  this.companyMap = new HashMap<String, Collection<String>>();  }  @Override  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {  if (qName.equalsIgnoreCase("Employee")) {  this.employeeCollection = new LinkedList<String>();  this.idAndNameStringBuffer = new StringBuffer();  this.currentValue = attributes.getValue("ID");  }  }  @Override  public void characters(char[] buffer, int start, int length) {  this.currentCharacters = new String(buffer, start, length);  }  @Override  public void endElement(String uri, String localName, String qName) throws SAXException {  if (StringUtils.containsIgnoreCase(leafNodeText, "|" + qName + "|")) {  this.employeeCollection.add(qName + ": " + this.currentCharacters);  if (qName.equalsIgnoreCase("FirstName")) {  this.idAndNameStringBuffer.append(this.currentCharacters);  }  if (qName.equalsIgnoreCase("LastName")) {  this.idAndNameStringBuffer.append(this.currentCharacters);  }  }  if (qName.equalsIgnoreCase("Employee")) {  this.companyMap.put(this.currentValue, this.employeeCollection);  this.idAndNameStringBuffer.append("-").append(this.currentValue);  this.writeEmployee(employeeCollection, idAndNameStringBuffer.toString());  }  }  private void writeEmployee(Collection<String> employeeCollection, String fileName) {  String outputFileDirectory = SaxHandler.class.getResource("/file/output/").getPath();  String outputFilePath = outputFileDirectory + fileName + ".xml";  File outputFile = new File(outputFilePath);  try {  FileUtils.writeLines(outputFile, employeeCollection, false);  } catch (IOException ioe) {  ioe.printStackTrace();  }  }  @Override  public void endDocument() throws SAXException {  super.endDocument();  }  public Map<String, Collection<String>> getCompanyMap() {  return this.companyMap;  }  }  

    package shuai.study.sax.demo;  import java.io.File;  import java.util.Collection;  import java.util.Iterator;  import java.util.Map;  import java.util.Map.Entry;  /** * @author shengshu *  */  public class SaxDemo {  public static void displayCompany(Map<String, Collection<String>> companyMap) {  Iterator<Entry<String, Collection<String>>> companyIterator = companyMap.entrySet().iterator();  while (companyIterator.hasNext()) {  Entry<String, Collection<String>> companyEntry = companyIterator.next();  String id = companyEntry.getKey();  System.out.println("============== Employee ID " + id + " Start ==============");  Collection<String> employeeCollection = companyEntry.getValue();  Iterator<String> employeeIterator = employeeCollection.iterator();  while (employeeIterator.hasNext()) {  String leafNodeAndValue = employeeIterator.next();  System.out.println(leafNodeAndValue);  }  System.out.println("============== Employee ID " + id + " End ==============");  }  }  public static void main(String[] args) {  String inputFilePath = SaxDemo.class.getResource("/file/input/company.xml").getPath();  File inputFile = new File(inputFilePath);  SaxHandler saxHandler = new SaxHandler(inputFile);  Map<String, Collection<String>> companyMap = saxHandler.getCompanyMap();  SaxDemo.displayCompany(companyMap);  }  }  

    <?xml version = "1.0" encoding="UTF-8"?>  <Company>  <Employee ID="37">  <Name>  <FirstName>Zhou</FirstName>  <LastName>Shengshuai</LastName>  </Name>  <Sex>Male</Sex>  <Address>  <Country>China</Country>  <Province>ShanDong</Province>  <City>LinYi</City>  <Village>FengHuangYu</Village>  <Contact>  <Mobile>18108***778</Mobile>  <Mail>zhoushengshuai2007@163.com</Mail>  <QQ>254392398</QQ>  <Postcode>276422</Postcode>  </Contact>  </Address>  <Profession>Software</Profession>  </Employee>  <Employee ID="66">  <Name>  <FirstName>Wang</FirstName>  <LastName>Eric</LastName>  </Name>  <Sex>Male</Sex>  <Address>  <Country>China</Country>  <Province>HeBei</Province>  <City>QinHuangDao</City>  <Village>hhh</Village>  <Contact>  <Mobile>150*****955</Mobile>  <Mail>eric@163.com</Mail>  <QQ>666666666</QQ>  <Postcode>111666</Postcode>  </Contact>  </Address>  <Profession>Software</Profession>  </Employee>  <Employee ID="99">  <Name>  <FirstName>Shi</FirstName>  <LastName>Stone</LastName>  </Name>  <Sex>Male</Sex>  <Address>  <Country>China</Country>  <Province>HeNan</Province>  <City>PingDingShan</City>  <Village>nnn</Village>  <Contact>  <Mobile>186*****015</Mobile>  <Mail>stone@163.com</Mail>  <QQ>999999999</QQ>  <Postcode>111999</Postcode>  </Contact>  </Address>  <Profession>Software</Profession>  </Employee>  </Company>  

这篇关于SAX 解析到文件,缓存到内存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/868234

相关文章

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

PyCharm如何更改缓存位置

《PyCharm如何更改缓存位置》:本文主要介绍PyCharm如何更改缓存位置的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录PyCharm更改缓存位置1.打开PyCharm的安装编程目录2.将config、sjsystem、plugins和log的路径

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

JSR-107缓存规范介绍

《JSR-107缓存规范介绍》JSR是JavaSpecificationRequests的缩写,意思是Java规范提案,下面给大家介绍JSR-107缓存规范的相关知识,感兴趣的朋友一起看看吧... 目录1.什么是jsR-1072.应用调用缓存图示3.JSR-107规范使用4.Spring 缓存机制缓存是每一

Spring 缓存在项目中的使用详解

《Spring缓存在项目中的使用详解》Spring缓存机制,Cache接口为缓存的组件规范定义,包扩缓存的各种操作(添加缓存、删除缓存、修改缓存等),本文给大家介绍Spring缓存在项目中的使用... 目录1.Spring 缓存机制介绍2.Spring 缓存用到的概念Ⅰ.两个接口Ⅱ.三个注解(方法层次)Ⅲ.

Spring Boot 整合 Redis 实现数据缓存案例详解

《SpringBoot整合Redis实现数据缓存案例详解》Springboot缓存,默认使用的是ConcurrentMap的方式来实现的,然而我们在项目中并不会这么使用,本文介绍SpringB... 目录1.添加 Maven 依赖2.配置Redis属性3.创建 redisCacheManager4.使用Sp