几种xml读取方法比较
背景
这几天手上有个活,解析xml,众所周知xml的解析方法有:
- DOM
- SAX
- linq to xml
- plinq
测试用xml和生成代码
static void CreateFile()
{
int N = ;
Random rand = new Random();
using (var writer = new XmlTextWriter("VeryHugeXmlFile.xml", Encoding.UTF8))
{
writer.Formatting = Formatting.Indented; writer.WriteStartDocument();
writer.WriteStartElement("Root");
for (int count = ; count <= N; count++)
{
writer.WriteStartElement("Person");
writer.WriteElementString("Id", count.ToString());
writer.WriteElementString("Name", rand.Next().ToString());
writer.WriteElementString("Sex", rand.Next(, ) == ? "男" : "女");
writer.WriteElementString("Age", rand.Next(, ).ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
之后会生成类似于下面的xml文件
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Person>
<Id>1</Id>
<Name>897639886</Name>
<Sex>女</Sex>
<Age>80</Age>
</Person>
<Person>
<Id>2</Id>
<Name>2012162696</Name>
<Sex>女</Sex>
<Age>60</Age>
</Person>
<Person>
测试代码
统计时间(只是粗略统计了一下运行时间)
static void Watch(Action<string> way, string file)
{
Stopwatch watch = new Stopwatch(); watch.Start();
way(file);
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
}
DOM
static void DomWay(string file)
{
XmlDocument doc = new XmlDocument();
doc.Load(file); Console.WriteLine(doc.SelectNodes(YOUR-XPATH-HERE).Count); }
SAX
static void SaxWay(string file)
{
using (XmlTextReader reader = new XmlTextReader(file))
{
int count = ;
while (reader.Read())
{
if (reader.Name == "Person" && reader.NodeType == XmlNodeType.Element)
{
reader.Read();
reader.Read(); int? Id = null;
int? name = null;
string sex = null;
int? age = null; if (reader.Name == "Id")
{
Id = reader.ReadElementContentAsInt();
reader.Read();
name = reader.ReadElementContentAsInt();
reader.Read();
sex = reader.ReadElementContentAsString();
reader.Read();
age = reader.ReadElementContentAsInt();
reader.Read();
} if (reader.Name == "Person" && reader.NodeType == XmlNodeType.EndElement)
reader.Read(); if (Id != null && name != null && sex != null && age != null)
{
if (在此设置自定义过滤条件)
count++;
}
}
} Console.WriteLine(count);
}
}
Linq to Xml
static void LinqWay(string file)
{
var root = XElement.Load(file);
var person = from p in root.Elements("Person")
7 where 在此设置自定义过滤条件
8 select id;
Console.WriteLine(person.Count());
}
PLinq to Xml
static void PLinqWay(string file)
{
var root = XElement.Load(file);
var person = from p in root.Elements("Person").AsParallel()
7 where 在此设置自定义过滤条件
8 select id;
Console.WriteLine(person.Count());
}
统计结果
在6核8G内存机器上,测试程序设置为x64和release模式,在xml查询结果相同的情况下取运行时间(ms),没有详细采集cpu和内存数据
两个模式,区别是加了一个素数的判断。
Id > 5000 && sex == "男" && age > 15 && age < 50 |
Id > 5000 && sex == "男" && age > 15 && age < 50 && IsPrimeInt(name) |
|
sax | 13857 | 40010 |
linq | 27336 | 53760 |
plinq | 24550 | 28846 |
dom | 31737 | 0 |
由于dom模式本身xpath模式不支持嵌入函数,所以第二个测试没有采集结果。
小结
sax:速度优先,内存占用少,但是代码复杂度高。
linq:速度较sax慢,但是代码优雅,维护容易
plinq:同上,在非计算密集型模式中,不比linq和sax模式好多少。但是在计算密集下,后来居上
dom:速度落后,但是原生支持xpath,代码最优雅。
内存方面仅是肉眼观察了任务管理器,sax基本内存曲线为水平线,而linq&plinq在load的时候分配内存,可能其内部也是用了dom。
仓促行文,其中必有不实之处,往各位劳神指教。
几种xml读取方法比较的更多相关文章
- DOM、SAX、JDOM、DOM4J四种XML解析方法PK
基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式 --优点: (1)形成了树结构,直观好理解,代码更易编写 ...
- asp.net写入读取xml的方法
添加命名空间 using System.Xml; 我自己的代码(添加其中的节点) XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.M ...
- winform,xml读取与写入
创建两个xml文件,一个 xml做为模板配置项,另一个做为临时接收数据. private static string localPath = System.Environment.CurrentDir ...
- Java获取路径方法&相对路径读取xml文件方法
(1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...
- JAVA文件的两种读取方法和三种写入方法
在使用java对文件进行读写操作时,有多种方法可以使用,但不同的方法有不同的性能. 此文对常用的读写方法进行了整理,以备不时之需. 1.文件的读取 主要介绍两种常用的读取方法.按行读取和按字符块读取. ...
- python之xml 文件的读取方法
''' xml 文件的读取方法 ''' #!/usr/bin/env python # -*- coding: utf- -*- import xml.etree.ElementTree as ET ...
- JAVA常用的XML解析方法
转并总结自(java xml) JAVA常用的解析xml的方法有四种,分别是DOM,JAX,JDOM,DOM4j xml文件 <?xml version="1.0" enco ...
- Linq to XML 读取XML 备忘笔记
本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有 ...
- C#基础巩固(3)-Linq To XML 读取XML
记录下一些读取XML的方法,以免到用的时候忘记了,还得花时间去找. 一.传统写法读取XML 现在我有一个XML文件如下: 现在我要查找名字为"王五"的这个人的 Id 和sex(性别 ...
随机推荐
- IE8兼容模式设置
设置---兼容性视图设置--添加此网站--在IE8中调试(防止调整IE内核后浏览器崩溃,360可通过设置极速模式-兼容模式 点击地址栏绿色图标)
- OC笔记一:Objective-C简介
1.OC简介 全称:Objective-C,是扩充C的面向对象编程语言,主要用于iOS和Mac OS开发. C语言的基础上,增加了一层最小的面向对象语法 完全兼容C语言 可以在OC代码中混入C语言代码 ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q105-Q108)
Question 105 You are designing a SharePoint 2010 application that contains a single list named Us ...
- iOS通讯录整合,兼容iOS789写法,附demo
苹果的通讯录功能在iOS7,iOS8,iOS9 都有着一定的不同,iOS7和8用的是 <AddressBookUI/AddressBookUI.h> ,但是两个系统版本的代理方法有一些变化 ...
- Android内存泄漏
Java是垃圾回收语言的一种,其优点是开发者无需特意管理内存分配,降低了应用由于局部故障(segmentation fault)导致崩溃,同时防止未释放的内存把堆栈(heap)挤爆的可能,所以写出来的 ...
- 解决:eclipse删除工程会弹出一个对话框提示“[project_name]”contains resources that are not in sync with"[workspace_name...\xx\..xx\..\xx]"
提示“[project_name]”contains resources that are not in sync with"[workspace_name...\xx\..xx\..\xx ...
- “#if 0/#if 1 ... #endif”的作用
1. "#if 0/#if 1 ... #endif"的作用,我们知道,C标准不提供C++里的"//"这样的单行风格注释而只提供"/* */" ...
- Echarts xAxis boundaryGap
Echarts xAxis----->boundaryGap: false 坐标轴两边留白策略,类目轴和非类目轴的设置和表现不一样. 类目轴中 boundaryGap 可以配置为 true 和 ...
- Nginx反向代理和负载均衡部署指南
1. 安装 1) 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最新版本(目前是1.5.13版本)安装包: ...
- SQLite学习笔记(六)&&共享缓存
介绍 通常情况下,sqlite中每个连接都会一个独立的pager对象,pager对象中管理了该连接的缓存信息,通过pragma cache_size指令可以设置缓存大小,默认是2000个page,每个 ...