Delphi learns XML parsing through code examples

Source: Internet
Author: User
Tags stringreplace

This program can be used to parse any valid XML string.

The first is to look at the operation of the program effect:

To parse such an XML string, for example:

<?xml version= "1.0" encoding= "UTF-8"?><bookinfo><owner><ownername> Zhang San </OwnerName> <OwnerAge>1234</OwnerAge></Owner><BookMes><BookName> Short History </BookName>< Isdn>234343453534</isdn><writer> Hawking </Writer></BookMes></BookInfo>

The first is to open the application

Then copy the string shown above into the text edit box and click the Resolve button, and the following effect will appear

As shown, you can display the parsed effect in the Format "field label in XML: Value of the corresponding label in XML."

And look at the code.

Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, X    mlintf, Xmldoc;type TForm1 = Class (Tform) Edt1:tedit;    Btn1:tbutton;  Procedure Btn1click (Sender:tobject); Private {Private declarations} public {public declarations} End;var form1:tform1;implementation{$R *.dfm}pro  Cedure ReadXml (Node:ixmlnode; var showmess:string); var nodelist:ixmlnodelist;  strname:string;  I:integer;begin if not node.haschildnodes then Exit; NodeList: = node.  ChildNodes; For I: = 0 to Nodelist.count-1 do begin strName: = Nodelist[i].    NodeName; If Nodelist[i]. Istextelement then//if the element begin showmess:= Showmess + strName + ': ' + nodelist[i].    NodeValue + #13 # #; End else if Nodelist[i].    HasChildNodes then//If there are child nodes begin READXML (Nodelist[i], showmess);  End  End;end;procedure Tform1.btn1click (sender:tobject); var sXML, showmess:string;  Cominstrxml:ixmldocument; Mainnode:ixmlnode;Begin showmess:= '; Sxml:= edt1.   Text;   Sxml:= StringReplace (SXML, ' UTF-8 ', ' GBK ', [Rfreplaceall]);     Try cominstrxml:= loadxmldata (SXML);     Mainnode:= cominstrxml.documentelement;     ReadXML (Mainnode, showmess);   ShowMessage (showmess);     Except on E:exception do begin ShowMessage (e.message);   End End;end;end.

  

Explain the code.

0. It is recommended that you use XML unpacking

The program in this example, after unpacking, displays the label and the corresponding value on the popup box, just to demonstrate the effect of parsing the XML.

  It is recommended that XML nodes be parsed in the development, with the node's label as key, the content of the node as value, inserted into a list of links that can be searched, or any other container class that can be searched according to key, then it is convenient to search the parsed value after use.

1. Recursive unpacking XML

Because XML itself is a recursive structure, the solution to the XML, that is, the ReadXml method in which the recursive method is used, the recursive end flag is: The XML tag being parsed is an element, there is no child node.

2. Problem with character encoding

In the Tform1.btn1click (Sender:tobject) method, we can see first replacing the ' UTF-8 ' in the XML string with ' GBK ', because if the encoding is Utf-8:<?xml version= "1.0" Encoding= "UTF-8", then if the XML exists in Chinese, then the parsing will be an error (if all in English, then will not error), such as

If you replace UTF-8 with GBK, you will not be able to report such errors when parsing the XML.

Also if you have an XML string that does not contain an encoded format, such as:<BookInfo><Owner><OwnerName> Zhang San </OwnerName><OwnerAge> 1234</ownerage></owner><bookmes><bookname> Time History </BookName><ISDN> 234343453534</isdn><writer> Hawking </Writer></BookMes></BookInfo>, no <?xml   Version= "1.0" encoding= "UTF-8"?> words, at this time even if there is sxml:= stringreplace (SXML, ' UTF-8 ', ' GBK ', [Rfreplaceall]); This substitution character code is also useless, and there is no way to change to GBK encoding, so the above exception is still reported.

So please pay attention to the character encoding problem, especially when both Chinese and English are available.

Use of 3.stringReplace functions

function StringReplace (const S, Oldpattern, newpattern:string; Flags:treplaceflags): string;

Rfreplaceall: Replace All
Rfignorecase: Ignoring case

Example

var     astr:string; begin     AStr: = ' This was a book, not a pen! ';     ShowMessage (StringReplace (aStr, ' a ', ' two ', []));//this is the two book, and not a pen! only replaces the first conforming string     showmessage (  StringReplace (ASTR, ' A ', ' both ', [Rfreplaceall]));//this is the book, and not both pen! replaces all strings that match     aStr: = ' This is a book, Not A pen! ';     ShowMessage (StringReplace (aStr, ' a ', ' two ', [Rfreplaceall]));//this is the two book, and not a pen! only replaces the conforming string (lowercase a)     ShowMessage (StringReplace (aStr, ' a ', ' a ', ' n ', [Rfreplaceall, Rfignorecase]));//this is the book, and not both pen! regardless of capitalization replace all matching String end;

4. Line break

Note that there is a line of code in the Code:

showmess:= showmess + strName + ': ' + nodelist[i]. NodeValue + #13 # #;

Where the #13 # # represents the line break

  

Delphi learns XML parsing through code examples

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.