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">
   <Stock exchange="NASDAQ">
   <Stock exchange="NYSE">
      <shares type="preferred">25</shares>

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.


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:

  NewStock: IXMLNode;
  ValueNode: IXMLNode;
  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';


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


Working with XML

  • 2007年04月19日 18:05
  • 368KB
  • 下载

Working with XML (ppt)

  • 2007年12月07日 15:05
  • 55KB
  • 下载

QTP-21 Working with XML 与XML交互

21 Working with XML Concept: QTP provides a built-in utility object tomanipulate XML, but the funct...

Working with XML on Android

  • 2013年06月01日 04:59
  • 59KB
  • 下载


  • 2008年08月28日 18:07
  • 2KB
  • 下载

Android working with Google Maps V2

If you have developed any app that contains Google Maps v1, It’s time to upgrade it to Google Maps V...

Android working with Volley Library

Android volley is a networking library was introduced to make networking calls much easier, faster w...
  • javalfx
  • javalfx
  • 2014年09月17日 09:47
  • 1107

working with microsoft excel

working with microsoft excel 和大多数的microsoft 的产品一样,excel通过COM借口提供了自动化excel的方法,本章只介绍一些实例代码,展示了如何使用QTP...
  • imcora
  • imcora
  • 2012年07月28日 11:26
  • 613

SVN版本问题:This client is too old to work with working copy

【问题描述】:MyEclipse插件版本1.4,客户端TortoiseSVN 1.6.6,用客户端commit代码之后,无法在MyEclipse下对工程就行update等操作,提示错误信息:This ...

iOS SDK: Working with URL Schemes

在自己的viewcontroller上面加 3个button :,然后添加3个对应的方法: -(IBAction) openMaps:(id)sender { ...
您举报文章:Working with XML nodes