The following xml file is available:

Base xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" BaseName="MyTestbase2"> Tables Count="3"> Table TableName="Abonents"> Column DataType="Integer"> iID /Column> Column DataType="Character" ValueSize="50"> cName /Column> Column DataType="Character" ValueSize="300"> cAddress /Column> Column DataType="Date"> dBornDate /Column> Column DataType="Blob" ValueSize="1000"> bPhoto /Column> /Table> 

How to read DataType, ValueSize and attribute name data (for example, cName) in Delphi using the XMLDocument component?

  • that no one knows how to do this? - IntegralAL
  • Read this article here: Parsing XML to Delphi . I think she will help you. - Drac5 1:01 pm
  • Thanks for the link, I already looked at it ... The question is how can I position on a certain element (for example, on a Table), if there are several of them and they contain nested fields? - IntegralAL
  • XMLDocument1.DocumentElement.ChildNodes.Nodes [i] .Text - enter only i correctly. - Drac5

1 answer 1

You cannot position on a specific element :) You need to know the structure of the XML file.

 procedure Parse(filename: string); var doc: IXMLDocument; node, table, field: IXMLNode; tables, fields: IXMLNodeList; i, tablescount, j: integer; tmpvalue: string; begin doc := TXMLDocument.Create(filename); doc.LoadFromFile(''); node := doc.DocumentElement.ChildNodes.FindNode('Tables'); tablescount := node.Attributes['Count']; tables := node.ChildNodes; for i := 0 to tablescount - 1 do begin table := tables[i]; tmpvalue := table.Attributes['TableName']; fields := table.ChildNodes; for j := 0 to fields.Count - 1 do begin field := fields[j]; tmpvalue := fields.ChildValues[j]; //имя поля tmpvalue := field.Attributes['DataType']; //тип поля tmpvalue := field.Attributes['ValueSize']; //размер поля end; end; end; 
  • doc: = TXMLDocument.Create (filename); and tmpvalue: = fields.ChildValues ​​[j]; problem lines shows an error. - IntegralAL
  • Why are unknown, known. uses XMLIntf and F1 to help you. :) In addition, the site parser ate tags and the procedure is written under the assumption that Table elements are contained in Tables. If not, then you will have to go through all the children of the primary element, check their name and act depending on it. - uilenspiegel
  • and what error shows? - uilenspiegel
  • first error: [DCC Error] Unit1.pas (36): E2018 Record, object or class type required second error: [DCC Error] Unit1.pas (49): E2003 Undeclared identifier: 'ChildValues' - IntegralAL
  • I do not have the first error (D7), but there is a second one. Not fields.ChildValues ​​[j], but table.ChildValues ​​[j]. - uilenspiegel