搜索插件、代码仓与 APP 分类

插件详情v1.1.1

xmlstream

一个轻量级库,用于以事件驱动的方式流式解析XML文档,类似于SAX。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
16.8万
喜欢数
7
Pub 分数
120

安装插件

flutter pub add xmlstream

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

xmlstream 是一个轻量级、事件驱动的 Dart 和 Flutter XML 解析器,采用 SAX(用于 XML 的简单 API)方法处理 XML 文档。它以流式方式增量处理 XML 数据,在遇到元素、属性和文本时发出事件,从而在处理大文件时更加高效,相比将整个文档加载到内存中,可显著降低内存使用。非常适合实时解析结构化数据或处理流式 XML 源。

适合场景

  • 高效解析大型XML文件
  • 实时XML数据处理
  • 从网络请求流式传输 XML
  • 在低内存环境中的XML处理
  • 与返回 XML 的 API 集成
  • 构建基于XML的协议

主要特点

  • 基于事件的解析模型
  • 低内存占用
  • 支持流式输入
  • SAX-style 事件回调
  • 跨平台兼容性
  • 简单、直观的 API

适合对象

  • 处理大型XML数据集的开发人员
  • 需要实时XML处理的应用程序
  • 具有内存限制的项目
  • 网络应用程序消耗XML源
  • 解析XML格式的配置文件或数据文件的工具

使用建议

  • 不适用于对 XML 节点的随机访问
  • 需要手动处理事件
  • 仅限顺序解析
  • 不支持内置的 XML 架构
  • 当顺序和流程比结构更重要时,最佳使用

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

xmlstream

A lightweight api for parsing xml

You can listen to xml encounters and states by parsing the xml char by char.

Simple usage

The xmlStreamer will send an XmlEvent that will contain the state and value of a specific part of the xml.

See here a simple example how to use it in your application.

var xmlStreamer = new XmlStreamer(rawText);
...

These are the xml states:

  • Top
  • Open
  • Closed
  • Attribute
  • Characters

Namespace and cdate are still on the todo list to support!

You can add an option to XmlStream to trim space or not, standard he will do this.

var xmlStreamer = new XmlStreamer(rawText, trimSpaces: false);
xmlStreamer.read().listen((e) => print("listen: $e"));
Object builder usage

In this package we provide also the opportunity to build and stream Objects.

If we have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<item name="flow">world</item>
<item name="text">say what</item>

You see an item object in it. So we want to pump the data in the following object.

class Item {
  String name;
  String value;

  String toString() => "$name - $value";
}

You can do that by creating a class that extends from XmlProcessor.

class ItemProcessor extends XmlProcessor<Item> {
	ItemProcessor() {
		tagName = "item";
	}
	
	void onOpenTag(String tag) {
 		element = new Item();
	}
  	void onAttribute(String key, String value) {
		if (key == "name") {
  			element.name = value;
		}
	}
	void onCharacters(String text) {
		element.value = text;
	} 
}

Now bootstrap the processing and you are ready to build a list of Items.

var xmlStreamer = new XmlStreamer(rawText);
var xmlObjectBuilder = new XmlObjectBuilder<Item>(xmlStreamer, new ItemProcessor());
xmlObjectBuilder.onProcess().listen((e) => print("listen: $e"));

The listen method will produce Item objects and you can listen to the production of it. If you want to parse an hierachical object structure then you can take a look at XmlParentProcessor class.

In my project on github you will find some examples how to use it. Any suggestions for improvements are welcome.

Ideas

It is based upon the SAX's and StAX's principles from Java.

Contributing

If you found a bug, just create a new issue or even better fork and issue a pull request with you fix.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。