Working with XML nodes

转载 2006年05月28日 21:12:00

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 (name, price, symbol, 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 name, price, symbol, and shares nodes. These text nodes then appear as the children of the name, price,
symbol, and shares nodes.

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

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.

Thus, for example, using the XML document above, you can read the price of Borland's stock as follows:

BorlandStock := XMLDocument1.DocumentElement.ChildNodes[0];
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[0];
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 := '25';

end;

An overloaded version of AddChild lets you specify the namespace URI in which the tag name is defined.

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 - 1);

【IDEA】IDEA常用配置

初学IDEA,好多不懂得地方,遇到问题有解决方法的,在本bolg中记录。持续更新~~~~~~~~~~~~~~~~~~~~~~~~~ 用*标识编辑过的文件  Editor –> Editor ...
  • Lucky_mn
  • Lucky_mn
  • 2016年12月13日 23:17
  • 694

在git提交环节,存在三大部分:working tree, index file, commit

这三大部分中: working tree:就是你所工作在的目录,每当你在代码中进行了修改,working tree的状态就改变了。 index file:是索引文件,它是连接working tre...
  • lksodit_yiyi
  • lksodit_yiyi
  • 2013年04月07日 13:22
  • 7403

Windows下的webrtc下载与编译

拆腾了好几天,先把我记的记录贴上来
  • free_syj
  • free_syj
  • 2014年09月10日 16:04
  • 4823

使用Eclipse的Working Set管理项目

转自:http://iyuanbo.iteye.com/blog/1158136 想必大家的Eclipse里也会有这么多得工程......     每次工作...
  • yasi_xi
  • yasi_xi
  • 2015年01月29日 13:13
  • 25110

hadoop 搭建3节点集群,遇到Live Nodes显示为0时解决办法

首先,尼玛哥在搭建hadoop 的3节点集群时,安装基本的步骤,配置好以下几个文件 core-site.xml hadoop-env.sh hdfs-site.xml yarn-env.sh yarn...
  • u010801439
  • u010801439
  • 2017年08月08日 21:14
  • 679

你真的懂任务管理器中有关内存的参数Private(提交大小)和working set(工作设置)吗?

任务管理器中跟内存相关有两个重要的指标Private(提交大小)和working set(工作设置)。如下图所示: 这两个指标在process explorer中叫做Private By...
  • wzsy
  • wzsy
  • 2015年01月15日 10:36
  • 2336

企业级搜索elasticsearch应用04-集群和常用插件安装

一。配置集群      二。常用插件安装
  • liaomin416100569
  • liaomin416100569
  • 2017年12月11日 17:29
  • 203

gitflow分支管理模型

http://www.berlinix.com/it/gitflow.php http://www.berlinix.com/it/gitflow.php http:/...
  • u011904605
  • u011904605
  • 2016年08月16日 15:48
  • 725

Git常用命令——基本操作

Git常用命令——基本操作 Git命令总览 SwitchdeMacBook-Pro:git-tutorial switch$ git --help -a usage: git...
  • q547550831
  • q547550831
  • 2016年08月21日 21:25
  • 4485

Cordova CLI源码分析(五)——添加平台

添加平台支持,其实本质是利用各平台SDK环境创建原生工程文件。所以,所有函数也是围绕这一主题展开。 入口当然是platform函数 module.exports = function plat...
  • mociml
  • mociml
  • 2013年08月30日 10:35
  • 4212
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Working with XML nodes
举报原因:
原因补充:

(最多只允许输入30个字)