XML 读取器和编写器----从文件读取 XML
2006-12-06 19:00
495 查看
如何从文件读取 XML
此示例阐释如何使用 XmlTextReader 类从文件读取 XML。该类提供对 XML 进行直接分析和标记化,并实现 W3C 可扩展标记语言 (XML) 1.0 及 XML 中的命名空间规范。
|
通常情况下,如果您需要将 XML 作为原始数据进行访问,则可以使用 XmlTextReader,这样可不必避免 DOM 开销。省去对 DOM 的访问可使读取 XML 的速度加快。例如,XML 文档可能有一个标头节,用于传递该文档以便在其他地方进行处理。XmlTextReader 有不同的构造函数,以指定 XML 数据的位置。此示例从 books.xml 文件加载 XML,如下列代码所示。
function doClick(index, numTabs, id) {
document.all("tab" + id, index).className = "tab";
for (var i=1; i
td.code {
padding:0,10,0,10;
border-style:solid;
border-width:1;
border-bottom:0;
border-top:0;
border-right:0;
border-color:cccccc;
background-color:ffffee
}
td.tab {
text-align:center;
font:8pt verdana;
width:15%;
padding:3,3,3,3;
border-style:solid;
border-width:1;
border-right:0;
border-color:black;
background-color:eeeeee;
cursor:hand
}
td.backtab {
text-align:center;
font: 8pt verdana;
width:15%;
padding:3,3,3,3;
border-style:solid;
border-width:1;
border-right:0;
border-color:black;
background-color:cccccc;
cursor:hand
}
td.space {
width:70%;
font: 8pt verdana;
padding:0,0,0,0;
border-style:solid;
border-bottom:0;
border-right:0;
border-width:1;
border-color:cccccc;
border-left-color:black;
background-color:white
}
XmlTextReader reader = new XmlTextReader ("books.xml"); Dim reader As XmlTextReader = New XmlTextReader ("books.xml") | ||
C# | VB |
while (reader.Read()) { // Do some work here on the data ... } Do While (reader.Read()) ' Do some work here on the data ... Loop | ||
C# | VB |
while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: // The node is an Element Console.Write("<" + reader.Name); while (reader.MoveToNextAttribute()) // Read attributes Console.Write(" " + reader.Name + "='" + reader.Value + "'"); Console.Write(">"); break; case XmlNodeType.DocumentType: // The node is a DocumentType Console.WriteLine(NodeType + "<" + reader.Name + ">" + reader.Value); break; ... } } Do While (reader.Read()) Select Case reader.NodeType Case XmlNodeType.Element ' The node is an Element Console.Write("<" + reader.Name) while (reader.MoveToNextAttribute()) ' Read attributes Console.Write(" " + reader.Name + "='" + reader.Value + "'") end while Console.Write(">") Case XmlNodeType.DocumentType ' The node is a DocumentType Console.WriteLine(NodeType & "<" & reader.Name & ">" & reader.Value); ... End Select Loop | ||
C# | VB |
为 XmlNodeType 指定的节点类型等效于 W3C DOM 节点类型,并且具有进行只进读取所要求的某些扩展类型。
XmlNodeType 枚举成员 | 说明 |
---|---|
Attribute | 属性。 示例 XML:id='123'。 Attribute 节点可以有下列子节点类型:Text 和 EntityReference。Attribute 节点看上去与任何其他节点类型的子节点不同;请注意,它并不是某个 Element 的子节点。 |
CDATA | CDATA 节。 示例 XML:<![CDATA[my escaped text]]> CDATA 节用于转义文本块,否则这些文本块将被识别为标记。CDATASection 节点不能有任何子节点。CDATASection 节点可作为 DocumentFragment、EntityReference 和 Element 节点的子级。 |
Comment | 注释。 示例 XML:<!-- my comment --> Comment 节点不能有任何子节点。Comment 节点可作为 Document、DocumentFragment、Element 和 EntityReference 节点的子级。 |
Document | 作为文档树的根的文档对象,可提供对整个 XML 文档的访问。 Document 节点可以有下列子节点类型:Element(最多有一个)、ProcessingInstruction、Comment 和 DocumentType。Document 节点不能作为任何节点类型的子级。 |
DocumentFragment | 文档片段。 DocumentFragment 节点将节点或子树与文档关联起来,而它实际上并不包含在该文档中。DocumentFragment 节点可以有下列子节点类型:Element、ProcessingInstruction、Comment、Text、CDATASection 和 EntityReference。DocumentFragment 节点不能作为任何节点类型的子级。 |
DocumentType | 文档类型声明,由 <!DOCTYPE> 标记表示。 示例 XML:<!DOCTYPE ...> DocumentType 节点可以有下列子节点类型:Notation 和 Entity。DocumentType 节点可作为 Document 节点的子级。 |
Element | 元素。 示例 XML:<Name> Element 节点可以有下列子节点类型:Element、Text、Comment、ProcessingInstruction、CDATA 和 EntityReference。Element 节点可作为 Document、DocumentFragment、EntityReference 和 Element 节点的子级。 |
EndElement | 当 XmlReader 到达元素的末尾时返回。 示例 XML:</foo> |
EndEntity | 由于调用 ResolveEntity 而使 XmlReader 到达实体替换的末尾时返回。 |
Entity | 实体声明。 示例 XML:<!ENTITY ...> Entity 节点可以有表示扩展实体(例如 Text 节点和 EntityReference 节点)的子节点。Entity 节点可作为 DocumentType 节点的子级。 |
EntityReference | 对实体的引用。 示例 XML:&foo; 它可应用于所有实体,包括字符实体引用。EntityReference 节点可以有下列子节点类型:Element、ProcessingInstruction、Comment、Text、CDATASection 和 EntityReference。EntityReference 节点可作为 Attribute、DocumentFragment、Element 和 EntityReference 节点的子级。 |
None | 如果未调用 Read 方法,则由 XmlReader 返回。 |
Notation | 文档类型声明中的表示法。 示例 XML:<!NOTATION ...> Notation 节点不能有任何子节点。Notation 节点可作为 DocumentType 节点的子级。 |
ProcessingInstruction | 处理指令 (PI)。 示例 XML:<?pi test?> PI 节点不能有任何子节点。PI 节点可作为 Document、DocumentFragment、Element 和 EntityReference 节点的子级。 |
SignificantWhitespace | 混合内容模型中标记之间的空白,或在 xml:space= "preserve" 范围内的空白。 |
Text | 元素的文本内容。Text 节点不能有任何子节点。Text 节点可作为 Attribute、DocumentFragment、Element 和 EntityReference 节点的子级。 |
Whitespace | 标记间的空白。 |
XmlDeclaration | XML 声明节点。 示例 XML:<?xml version='1.0'?> 它必须是文档中的第一个节点。它不能有子级。它是根节点的子级。它可以具有提供版本和编码信息的属性。 |
private static void Format(XmlReader reader, String nodeType) { // Format the output Console.Write(reader.Depth + " "); Console.Write(reader.AttributeCount + " "); for (int i=0; i < reader.Depth; i++) { Console.Write('\t'); } Console.Write(reader.Prefix + nodeType + "<" + reader.Name + ">" + reader.Value); // Display the attributes values for the current node if (reader.HasAttributes) { Console.Write(" Attributes:"); for (int j=0; j < reader.AttributeCount; j++) { Console.Write(" [{0}] " + reader[j], j); } } Console.WriteLine(); } private shared Sub Format(byref reader as XmlTextReader , nodeType as String) ' Format the output Console.Write(reader.Depth & " ") Console.Write(reader.AttributeCount & " ") Dim i as Integer for i = 0 to reader.Depth Console.Write(Strings.chr(9)) Next Console.Write(nodeType & "<" & reader.Name & ">" & reader.Value) 'Display the attributes values for the current node if (reader.HasAttributes) Console.Write(" Attributes:") Dim j as Integer for j = 0 to reader.AttributeCount - 1 Console.Write(" [{0}] " & reader(j), j) next End if Console.WriteLine() End Sub | ||
C# | VB |
注意:有关通过移到每个属性节点的方式来访问属性的其他方法,以及读取节点的名称和值的方法,请参阅如何从流读取 XML。
以下输出是使用 books.xml 文件运行该示例产生的结果。在此输出中,第一列是 Depth 属性,第二列是 AttributeCount 属性。
0 0 XmlDeclaration<xml>version='1.0' 0 0 Comment<> This file represents a fragment of a book store inventory database 0 0 Element<bookstore> 1 3 Element<book> Attributes: [0] autobiography [1] 1981 [2] 1-861003-11-0 2 0 Element<title> 3 0 Text<>The Autobiography of Benjamin Franklin 2 0 Element<author> 3 0 Element<first-name> 4 0 Text<>Benjamin 3 0 Element<last-name> 4 0 Text<>Franklin 2 0 Element<price> 3 0 Text<>8.99 1 3 Element<book> Attributes: [0] novel [1] 1967 [2] 0-201-63361-2 2 0 Element<title> 3 0 Text<>The Confidence Man 2 0 Element<author> 3 0 Element<first-name> 4 0 Text<>Herman 3 0 Element<last-name> 4 0 Text<>Melville 2 0 Element<price> 3 0 Text<>11.99 1 3 Element<book> Attributes: [0] philosophy [1] 1991 [2] 1-861001-57-6 2 0 Element<title> 3 0 Text<>The Gorgias 2 0 Element<author> 3 0 Element<name> 4 0 Text<>Plato 2 0 Element<price> 3 0 Text<>9.99 Statistics for books.xml file XmlDeclaration: 1 ProcessingInstruction: 0 DocumentType: 0 Comment: 1 Element: 18 Attribute: 9 Text: 11 Whitespace: 27
摘要
XmlTextReader 提供对 XML 数据的快速、非缓存和只进的读取访问。XmlTextReader 实现 W3C 可扩展标记语言 (XML) 1.0 和 XML 中的命名空间规范。
XmlTextReader 提供从文件、流或 TextReader 读取 XML 的构造函数。
Read 方法将读取器作为一个节点按顺序移动。
对于元素节点,可通过使用索引运算符获取属性值。
属性可表示为当前节点的节点列表,并可通过 HasAttributes 属性查看它。
Depth 属性报告当前节点的深度,可用于进行格式化。位于根级别的节点的深度为 0。
相关文章推荐
- XML 读取器和编写器----从URL读取XML
- XML 读取器和编写器----将XML写入文件
- XML 读取器和编写器----读取XML 时应用验证
- 07_数据库创建,添加c3p0操作所需的jar包,编写c3p0-config.xml文件,编写User.java,编写jdbcUtils.java实现操作数据库的模板工具类,UserDao编写,Dao
- 分析ASP.NET读取XML文件4种方法
- 深入浅出学习Struts1框架(七):ActionServlet实例化之读取struts-config.xml配置文件
- dom4j读取xml字符串和读取xml文件
- XML文件读取、保存
- android 实用sax 读取xml文件内容
- JXTree对象,读取外部xml文件数据,生成树的函数
- java读取xml文件
- struts.xml文件中各个组件的编写顺序
- 读取XML文件内容的简单方法
- 读取xml学习:使用jdom读取xml文件
- 使用SAX读取XML文件的练习--(将读取的XML文件原样输出)
- asp.net读取Xml文件到DataSet,以及DataSet保存为Xml,利用自带的强大功能
- ExtJS如何读取XML、Json文件
- 正则表达式/DOM读取xml,php/dom编写xml
- 怎样读取和处理XML的配置文件
- java眼中的XML---文件读取