How to use a query in an XML file in Delphi?

I am new to Delphi, and here is what I want to do. I created an XML file as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Row> <Designation>1234102</Designation> <Inner>5.412</Inner> <Outer>3.588</Outer> <Spin>4.732</Spin> <Cage>0.399</Cage> </Row> <Row> <Designation>1342153</Designation> <Inner>5.916</Inner> <Outer>4.084</Outer> <Spin>5.277</Spin> <Cage>0.408</Cage> </Row> ........ </Data> 

and I want to request it using Delphi. For example: I need data on where 1342153. Where is the best and easiest solution?

Thanks in advance for an example and explanation.

+4
source share
4 answers

I'm going to make the assumption that as soon as you find Designation , you will also want to read the other notes ( Inner , Outer , Spin and Cage ) that come with the designation.

XPath is the perfect solution to this problem. My example uses a new form with TMemo and TButton just dropped and the TButton event handler added:

 uses MSXML, ComObj, ActiveX; const XMLText = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + '<Data>' + '<Row>' + '<Designation>1234102</Designation>' + '<Inner>5.412</Inner>' + '<Outer>3.588</Outer>' + '<Spin>4.732</Spin>' + '<Cage>0.399</Cage>' + '</Row>' + '<Row>' + '<Designation>1342153</Designation>' + '<Inner>5.916</Inner>' + '<Outer>4.084</Outer>' + '<Spin>5.277</Spin>' + '<Cage>0.408</Cage>' + '</Row>' + '</Data>'; procedure TForm1.Button1Click(Sender: TObject); var XMLDoc: IXMLDOMDocument; Node, SibNode: IXMLDOMNode; begin Memo1.Clear; XMLDoc := CoDOMDocument.Create; XMLDoc.loadXML(XMLText); // Select the node with the Designation you want. Node := XMLDoc.selectSingleNode('//Designation[text()="1342153"]'); if Assigned(Node) then begin Memo1.Lines.Add('Found it.'); Memo1.Lines.Add(Node.nodeName + ' = ' + Node.firstChild.nodeValue); // Read all the nodes at the same level as the Designation SibNode := Node.nextSibling; while SibNode <> nil do begin Memo1.Lines.Add(SibNode.nodeName + ' = ' + SibNode.firstChild.nodeValue); Sib := Sib.nextSibling; end; end; end; 

If you want to just grab all the <Row> elements and skip the information contained in them, you can use this (add a second button to the test application above and use this for the Button2.OnClick handler).

 procedure TForm1.Button2Click(Sender: TObject); var XMLDoc: IXMLDOMDocument; NodeList: IXMLDOMNodeList; Node, SibNode: IXMLDOMNode; i: Integer; begin Memo1.Clear; XMLDoc := CoDOMDocument.Create; XMLDoc.loadXML(XMLText); NodeList := XMLDoc.selectNodes('/Data/Row'); if Assigned(NodeList) then begin for i := 0 to NodeList.length - 1 do begin Node := NodeList.item[i]; SibNode := Node.firstChild; while Assigned(SibNode) do begin Memo1.Lines.Add(SibNode.nodeName + ' = ' + SibNode.firstChild.nodeValue); SibNode := SibNode.nextSibling; end; end; // Add a blank line between groupings for readability Memo1.Lines.Add(''); end; end; 
+3
source

Like others, you can use XPath to search for a specific value, in this case using this expression /Data/Row/Designation[text()="1342153"] will find a node that contains the value 1342153 in the Designation.

Try this sample code

 {$APPTYPE CONSOLE} {$R *.res} uses MSXML, SysUtils, ActiveX, ComObj; Const XmlStr = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+ '<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+ ' <Row>'+ ' <Designation>1234102</Designation>'+ ' <Inner>5.412</Inner>'+ ' <Outer>3.588</Outer>'+ ' <Spin>4.732</Spin>'+ ' <Cage>0.399</Cage>'+ ' </Row>'+ ' <Row>'+ ' <Designation>1342153</Designation>'+ ' <Inner>5.916</Inner>'+ ' <Outer>4.084</Outer>'+ ' <Spin>5.277</Spin>'+ ' <Cage>0.408</Cage>'+ ' </Row>'+ '</Data>'; procedure Test; Var XMLDOMDocument : IXMLDOMDocument; XMLDOMNode : IXMLDOMNode; begin XMLDOMDocument:=CoDOMDocument.Create; XMLDOMDocument.loadXML(XmlStr); XMLDOMNode := XMLDOMDocument.selectSingleNode('/Data/Row/Designation[text()="1342153"]'); if XMLDOMNode<>nil then Writeln('Found'); end; begin try CoInitialize(nil); try Test; finally CoUninitialize; end; except on E:EOleException do Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); on E:Exception do Writeln(E.Classname, ':', E.Message); end; Writeln('Press Enter to exit'); Readln; end. 
+3
source

IXMLDocument and XPath are your friends, when you request xml in Delphi you can find many sources for this, for example. XPath and TXmlDocument

+1
source

As you want to request data from xml, I recommend that you use the XML Transformation registered here http://docwiki.embarcadero.com/RADStudio/XE4/en/Converting_XML_Documents_into_Data_Packets

This will match you xml with ClientDataSet, you can filter the records by collamines you want, or you can use the DataBinding Wizard, it has an explanation in the documents at this URL http://docwiki.embarcadero.com/RADStudio/XE4/en/ Using_the_XML_Data_Binding_Wizard

For other ways to work with xml, you can see the main document pointer here http://docwiki.embarcadero.com/RADStudio/XE4/en/Working_with_XML_documents_Index

0
source

Source: https://habr.com/ru/post/1490906/


All Articles