Using TXMLDocument

The starting point for working with an XML document is the Xml.XMLDoc.TXMLDocument component.

The following steps describe how to use TXMLDocument to work directly with an XML document:

  1. Add a TXMLDocument component to your form or data module. TXMLDocument appears on the Internet category of the Tool Palette.
  2. Set the DOMVendor property to specify the DOM implementation you want the component to use for parsing and editing an XML document.
    The Object Inspector lists all the currently registered DOM vendors.
    For information on DOM implementations, see Using the Document Object Model.
  3. Depending on your implementation, you may want to set the ParseOptions property to configure how the underlying DOM implementation parses the XML document.
  4. If you are working with an existing XML document, specify the document:
    • If the XML document is stored in a file, set the FileName property to the name of that file.
    • You can specify the XML document as a string instead by using the XML property.
  5. Set the Active property to True.

Once you have an active TXMLDocument object, you can traverse the hierarchy of its nodes, reading or setting their values.

The root node of this hierarchy is available as the DocumentElement property.

For information on working with the nodes of the XML document, see Working with XML Nodes.

Working with XML Nodes

Once an XML document has been parsed by a DOM implementation, the data it represents is available as a hierarchy of nodes.

Each node corresponds to a tagged element in the document.

For example, given the following XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE StockHoldings SYSTEM "sth.dtd">
<StockHoldings>

<Stock exchange="NASDAQ">
<name>Borland</name>
<price>15.375</price>
<symbol>BORL</symbol>
<shares>100</shares>
</Stock>

<Stock exchange="NYSE">
<name>Pfizer</name>
<price>42.75</price>
<symbol>PFE</symbol>
<shares type="preferred">25</shares>
</Stock>

</StockHoldings>

TXMLDocument would generate a hierarchy of nodes as follows:

The root of the hierarchy would be the StockHoldings node.

StockHoldings would have two child nodes, which correspond to the two Stock tags.

Each of these two child nodes would have four child nodes of its own (namepricesymbol, and shares).

Those four child nodes would act as leaf nodes.

The text they contain would appear as the value of each of the leaf nodes.

Note:

This division into nodes differs slightly from the way a DOM implementation generates nodes for an XML document.

In particular, a DOM parser treats all tagged elements as internal nodes.

Additional nodes (of type text node) are created for the values of the namepricesymbol, and shares nodes.

These text nodes then appear as the children of the namepricesymbol, and shares nodes.

Each node is accessed through an Xml.XMLIntf.IXMLNode interface, starting with the root node, which is the value of the XML document component's DocumentElementproperty.

Working with a node's value

Given an IXMLNode interface, you can check whether it represents an internal node or a leaf node by checking the IsTextElement property.

  • If it represents a leaf node, you can read or set its value using the Text property.
  • If it represents an internal node, you can access its child nodes using the ChildNodes property.
BorlandStock := XMLDocument1.DocumentElement.ChildNodes[];
Price := BorlandStock.ChildNodes['price'].Text;

Working with a node's attributes

If the node includes any attributes, you can work with them using the Attributes property.

You can read or change an attribute value by specifying an existing attribute name.

You can add new attributes by specifying a new attribute name when you set the Attributes property:

BorlandStock := XMLDocument1.DocumentElement.ChildNodes[];
BorlandStock.ChildNodes['shares'].Attributes['type'] := 'common';

Adding and deleting child nodes

You can add child nodes using the AddChild method.

AddChild creates new nodes that correspond to tagged elements in the XML document.

Such nodes are called element nodes.

To create a new element node, specify the name that appears in the new tag and, optionally, the position where the new node should appear.

For example, the following code adds a new stock listing to the document above:

var
NewStock: IXMLNode;
ValueNode: IXMLNode;
begin
NewStock := XMLDocument1.DocumentElement.AddChild('stock');
NewStock.Attributes['exchange'] := 'NASDAQ';
ValueNode := NewStock.AddChild('name');
ValueNode.Text := 'Cisco Systems'
ValueNode := NewStock.AddChild('price');
ValueNode.Text := '62.375';
ValueNode := NewStock.AddChild('symbol');
ValueNode.Text := 'CSCO';
ValueNode := NewStock.AddChild('shares');
ValueNode.Text := '';
end;

You can delete child nodes using the methods of the ChildNodes property.

ChildNodes is an IXMLNodeList interface, which manages the children of a node.

You can use its Delete method to delete a single child node that is identified by position or by name.

For example, the following code deletes the last stock listed in the document above:

StockList := XMLDocument1.DocumentElement;
StockList.ChildNodes.Delete(StockList.ChildNodes.Count - );

Xml.XMLIntf.TNodeType

TNodeType = (
ntReserved,
ntElement,
ntAttribute,
ntText,
ntCData,
ntEntityRef,
ntEntity,
ntProcessingInstr,
ntComment,
ntDocument,
ntDocType,
ntDocFragment,
ntNotation);
Value Meaning

ntReserved

Not used

ntElement

The node represents an element.

Element nodes represent simple tags that have child nodes.

(Note that sometimes these child nodes do not appear when using IXMLNode.

For example child nodes of type ntText are typically hidden by IXMLNode and appear only as the value of the Text property).

The child nodes of an element node can have the following node types:

ntElement, ntText, ntCData, ntEntityRef, ntProcessingInstr, and ntComment.

Element nodes can also have attributes (ntAttribute).

Element nodes can be the child of a node of type

ntDocument, ntDocFragment, ntEntityRef, and ntElement.

ntElement -> ntElement -> ntElement ... --> ntText

ntAttribute

The node represents an attribute of an element.

It is not the child of another node, but its value can be accessed using the Attribute property of the element node's IXMLNode interface.

An attribute node can have child nodes of type ntText and ntEntityRef.

ntText

The node represents the text content of a tag.

A text node cannot have any child nodes,

but can appear as the child node of a node of type

ntAttribute, ntDocFragment, ntElement, or ntEntityRef.

ntCData

The node represents a CDATA section in the XML source.

CDATA sections identify blocks of text that would otherwise be interpreted as markup.

An ntCData node can't have any child nodes.

They can appear as the child of an ntDocFragment, ntEntityRef, or ntElement node.

ntEntityRef

The node represents a reference to an entity in the XML document.

This can be any type of entity, including character entity references.

The children of an entity reference node can be of the following types:

ntElement, ntProcessingInstr, ntComment, ntText, ntCData, and ntEntityRef.

The entity reference node can appear as the child of an

ntAttribute, ntDocFragment, ntElement, or ntEntityRef node.

ntEntity

The node represents an expanded entity.

Entity nodes can have child nodes that represent the expanded entity (for example, ntText and ntEntityRef nodes).

Entity nodes only appear as the child of an ntDocType node.

ntProcessingInstr

The node represents a processing instruction (PI) from the XML document.

A PI node cannot have any child nodes,

but can appear as the child node of a node of type

ntDocument, ntDocFragment, ntElement, or ntEntityRef.

ntComment

The node represents a comment in the XML document.

Comment nodes do not have child nodes.

They appear as the child of an

ntDocument, ntDocFragment, ntElement, or ntEntityRef node.

ntDocument

The node represents a document object, which is the root of the entire XML document.

Document nodes have a single ntElement node as a child (the DocumentElement).

In addition, they can have child nodes of type

ntProcessingInstr, ntComment, and ntDocType.

Because the document is the root of the XML document, it never appears as a child node.

ntDocType

The node represents the document type declaration, indicated by the <!DOCTYPE > tag.

The document type node can child nodes of type

ntNotation and ntEntity.

It always appears as the child of the document node.

ntDocFragment

The node represents a document fragment.

A document fragment node associates a node or subtree with a document without actually being contained in the document.

Document fragment nodes can have child nodes of type

ntElement, ntProcessingInstr, ntComment, ntText, ntCData, and ntEntityRef.

It never appears as the child of another node.

ntNotation

A node represents a notation in the document type declaration.

It always appears as the child of an ntDocType node and

never has any child nodes.

Using TXMLDocument, Working with XML Nodes的更多相关文章

  1. Select XML Nodes by Name [C#]

    原文 http://www.csharp-examples.net/xml-nodes-by-name/ To find nodes in an XML file you can use XPath ...

  2. 转载---SQL Server XML基础学习之<6>--XQuery的 value() 方法、 exist() 方法 和 nodes() 方法

    /*------------------------------------------------------------------------------+ #| = : = : = : = : ...

  3. xml类型使用注意事项

    xml 的数据类型在平常的开发也是很常用的,燃鹅.也是有一些地方需要留意.今天我就分享几个测试的例子. 使用 xquery.exist (有但不仅仅限于)的注意事项.通常使用来判断节点是否存在,值是否 ...

  4. LINQ系列:LINQ to XML查询

    1. 读取XML文件 XDocument和XElement类都提供了导入XML文件的Load()方法,可以读取XML文件的内容,并转换为XDocument或XElement类的实例. 示例XML文件: ...

  5. XQuery的 value() 方法、 exist() 方法 和 nodes() 方法

    Xml数据类型 /*------------------------------------------------------------------------------+ #| = : = : ...

  6. 【译】用jQuery 处理XML-- jQuery与XML

    用jQuery 处理XML--写在前面的话 用jQuery 处理XML-- DOM(文本对象模型)简介 用jQuery 处理XML--浏览器中的XML与JavaScript 用jQuery 处理XML ...

  7. 在SQL2008中使用XML应对不确定结构的参数

    目的:统一接口,当数据结构发生变化时,前后端业务接口不发生变化,由业务具体解析结构. 规则:确定的接口用参数表(多行提交),不确定的参数用XML   DECLARE @r TABLE     (    ...

  8. 【转】Sql Server参数化查询之where in和like实现之xml和DataTable传参

    转载至: http://www.cnblogs.com/lzrabbit/archive/2012/04/29/2475427.html 在上一篇Sql Server参数化查询之where in和li ...

  9. SQL操作XML

    前面一段时间为了赶项目的进度,一直在加班,现在空闲了下来将前面碰到的问题整理了一下,发现了一些十分有用的,在此记录下来,看能不能帮助到遇到同样问题的朋友,此文仅是自己个人的意见,若存在问题,还望不宁赐 ...

随机推荐

  1. HDU 6205 2017沈阳网络赛 思维题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6205 题意:给你n堆牌,原本每一堆的所有牌(a[i]张)默认向下,每次从第一堆开始,将固定个数的牌(b ...

  2. 读书笔记 effective c++ Item 3 在任何可能的时候使用 const

    Const可以修饰什么?   Const 关键字是万能的,在类外部,你可以用它修饰全局的或者命名空间范围内的常量,也可以用它来修饰文件,函数和块作用域的静态常量.在类内部,你可以使用它来声明静态或者非 ...

  3. [转载]Windows服务编写原理及探讨(1)

    有那么一类应用程序,是能够为各种用户(包括本地用户和远程用户)所用的,拥有用户授权级进行管理的能力,并且不论用户是否物理的与正在运行该应用程序的计算机相连都能正常执行,这就是所谓的服务了. (一)服务 ...

  4. [会装]Hive安装(基于mysql数据库)

    环境信息:Mac 安装步骤: 1. 下载hive组件(我选择的是社区的2.0.1版本) http://apache.mirror.globo.tech/hive/hive-2.0.1/ 2. 下载my ...

  5. (总结)MySQL自带的性能压力测试工具mysqlslap详解

    PS:今天一同事问我有木有比较靠谱的mysql压力测试工具可用.其实mysql自带就有一个叫mysqlslap的压力测试工具,还是模拟的不错的.下面举例说说.mysqlslap是从5.1.4版开始的一 ...

  6. 2.rabbitmq 工作队列

    1. 生产者 #coding:utf8 import pika import json import sys message = ''.join(sys.argv[1:]) or "hell ...

  7. Android第一篇

    1. 网上下载最新版SDK,里面就有一个集成ADT的Eclipse,可以直接用. 2. 最新版SDK会在layout文件夹下有fregment.xml和activity.xml两个布局文件,如果像我这 ...

  8. serial minicom

    这是一篇讲述串口很好的入门教程: http://users.ece.utexas.edu/~valvano/Volume1/E-Book/C11_SerialInterface.htm 如何显示串口数 ...

  9. Cookie/Session的认识

    Cookie 1.cookie是什么? cookie是一小段文本,伴随着用户请求和页面在web服务器和浏览器之间传递,cookie包含每次用户访问站点时,web应用程序都可以读取的信息 2.为什么需要 ...

  10. java 基础知识-数组的7种算法(排序、求和、最值、遍历...)

    遍历 遍历就是把这个数组的每个元素 显示出来 遍历的方法就是先定义这个数组的大小,然后用FOR循环来完成数组,例如 double[] score = new double[5]; Scanner in ...