Detailed description of the xmldocument class in Delphi (5)-getting Element Content

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, xmldom, xmlintf, msxmldom, xmldoc, stdctrls; Type tform1 = Class (tform) xmldocument1: txmldocument; button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; Procedure upload (Sender: tobject); Procedure button1click (Sender: tobject ); procedure button2click (Sender: tobject); Procedure button3click (Sender: tobject); Procedure button4click (Sender: tobject); Private {private Declarations} public {public declarations} end; var form1: tform1; implementation {$ R *. DFM} // open procedure tform1.formcreate (Sender: tobject); begin xmldocument1.loadfromfile ('C: \ temp \ test. XML '); {you must use the XML test file provided in case to have the same return value} end; // access the information of the first person procedure tform1.button1click (Sender: tobject ); begin showmessage (xmldocument1.documentelement. childnodes [0]. childnodes ['name']. text); {Zhang San} showmessage (xmldocument1.documentelement. childnodes [0]. childnodes ['gender']. text); {male} showmessage (xmldocument1.documentelement. childnodes [0]. childnodes ['age']. text); {34} {sub-nodes can be accessed by location. For example, childnodes [0] indicates the first element in the sub-node list} {sub-nodes can be accessed by the sub-node name, for example: childnodes ['name']} {but in the case of the same node name, you can only access through location} {in any case, you can access through location, for example :} showmessage (xmldocument1.documentelement. childnodes [0]. childnodes [0]. text); {Zhang San} end; // access the information of the second person procedure tform1.button2click (Sender: tobject); var nodelist: ixmlnodelist; node: ixmlnode; begin nodelist: = xmldocument1.documentelement. childnodes; node: = nodelist [1]; showmessage (node. childnodes ['name']. text); {Li Si} showmessage (node. childnodes ['gender']. text); {female} showmessage (node. childnodes ['age']. text); {43} end; // obtain all Member names procedure tform1.button3click (Sender: tobject); var nodelist: ixmlnodelist; node: ixmlnode; num, I: integer; begin nodelist: = xmldocument1.documentelement. childnodes; num: = nodelist. count; for I: = 0 to num-1 do begin node: = nodelist [I]; showmessage (node. childnodes ['name']. text); {It will show: Zhang San Li Si Wang Wu Sun 6} end; // get all member ages procedure tform1.button4click (Sender: tobject); var nodelist: ixmlnodelist; node: ixmlnode; num, I: integer; begin nodelist: = xmldocument1.documentelement. childnodes; num: = nodelist. count; for I: = 0 to num-1 do begin node: = nodelist [I]; showmessage (node. childvalues ['age']); {displayed: 34 43 25 52} end; end.
 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.