背景

这几天手上有个活,解析xml,众所周知xml的解析方法有:

  1. DOM
  2. SAX
  3. linq to xml
  4. 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>

xml下载链接

测试代码

统计时间(只是粗略统计了一下运行时间)

 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读取方法比较的更多相关文章

  1. DOM、SAX、JDOM、DOM4J四种XML解析方法PK

    基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式   --优点:          (1)形成了树结构,直观好理解,代码更易编写     ...

  2. asp.net写入读取xml的方法

    添加命名空间 using System.Xml; 我自己的代码(添加其中的节点) XmlDocument xmlDoc = new XmlDocument();xmlDoc.Load(Server.M ...

  3. winform,xml读取与写入

    创建两个xml文件,一个 xml做为模板配置项,另一个做为临时接收数据. private static string localPath = System.Environment.CurrentDir ...

  4. Java获取路径方法&相对路径读取xml文件方法

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...

  5. JAVA文件的两种读取方法和三种写入方法

    在使用java对文件进行读写操作时,有多种方法可以使用,但不同的方法有不同的性能. 此文对常用的读写方法进行了整理,以备不时之需. 1.文件的读取 主要介绍两种常用的读取方法.按行读取和按字符块读取. ...

  6. python之xml 文件的读取方法

    ''' xml 文件的读取方法 ''' #!/usr/bin/env python # -*- coding: utf- -*- import xml.etree.ElementTree as ET ...

  7. JAVA常用的XML解析方法

    转并总结自(java xml) JAVA常用的解析xml的方法有四种,分别是DOM,JAX,JDOM,DOM4j xml文件 <?xml version="1.0" enco ...

  8. Linq to XML 读取XML 备忘笔记

    本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有 ...

  9. C#基础巩固(3)-Linq To XML 读取XML

    记录下一些读取XML的方法,以免到用的时候忘记了,还得花时间去找. 一.传统写法读取XML 现在我有一个XML文件如下: 现在我要查找名字为"王五"的这个人的 Id 和sex(性别 ...

随机推荐

  1. CSS3中flexbox如何实现水平垂直居中和三列等高布局

    最近这些天都在弥补css以及css3的基础知识,在打开网页的时候,发现了火狐默认首页上有这样一个东西.

  2. android常犯错误记录

    错误:Error:Error: Found item Attr/border_width more than one time 这个容易,属性相同了,按照提示查询一下找出来删了就行了,注意大小写很容易 ...

  3. iOS字体

  4. iOS HTML 字符串中的图片 自适应大小

    本文原文地址:http://www.cnblogs.com/qianLL/p/6095988.html 有时候 我们接收数据的时候  后台给的数据室一串HTML 的字符串  但是 我们要显示出来  这 ...

  5. JavaScript的个人学习随手记(二)

    JS HTML DOM 改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Sat Sep 24 2016 15:06:50 GMT+0800 (中国标准时 ...

  6. 拯救你的文档 – 【DevOps敏捷开发动手实验】开源文档发布

    今天上海的天气真是不错,风和日丽.再次来到微软上海紫竹研发中心,心情很是愉快,喜欢这里的大草坪,喜欢这里的工程气氛,更喜欢今天来陪我的小伙伴们. 这次动手实验培训与以往最大的不同就是采用了开源文档的方 ...

  7. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid

    WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid 故事背景: 需要检索某目录下文件,并列出来,提供选择和其他功能. 第一版需求: 列出文件供选择即可,代码如下 ...

  8. Mysql主从复制,读写分离(mysql-proxy),双主结构完整构建过程

    下面介绍MySQL主从复制,读写分离,双主结构完整构建过程,不涉及过多理论,只有实验和配置的过程. Mysql主从复制(转载请注明出处,博文地址:) 原理是master将改变记录到二进制日志(bina ...

  9. 读《高性能javascript》笔记(一)

    第一章加载与执行:1,js脚本会阻塞页面渲染,<script>尽可能放到<body>标签的底部2, 合并脚本,页面中的<script>标签越少:HTTP请求带来的额 ...

  10. Spring AOP支持的AspectJ切入点语法大全

    原文出处:http://jinnianshilongnian.iteye.com/blog/1420691 Spring AOP支持的AspectJ切入点指示符 切入点指示符用来指示切入点表达式目的, ...