Recent

Author Topic: Access XML Nodes Individually with Path String?  (Read 1433 times)

Dennis1

  • New Member
  • *
  • Posts: 14
Access XML Nodes Individually with Path String?
« on: February 27, 2020, 05:18:25 pm »
I'm trying to write a simple XML-based application that will be able to read and write single nodes using a path query like this: root/year/month/day/[string data]. Long ago I wrote a parser in VB6 that would do this using string methods, but it was messy and I'd prefer not to have to do that again.

The XML data structure looks like this:

<root>
     <2020>
          <February>
               <D27>string data</D27>
          </February>
     </2020>
</root>

I've read the XML Wiki page, but nothing like this is described. Is it even possible with DOM methods?

Thanks.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Access XML Nodes Individually with Path String?
« Reply #1 on: February 27, 2020, 06:19:53 pm »
It is possible with the XMLConfig classes.

Dennis1

  • New Member
  • *
  • Posts: 14
Re: Access XML Nodes Individually with Path String?
« Reply #2 on: February 27, 2020, 10:49:38 pm »
Unfortunately, XMLConfig only seems made to store snippets as attributes rather than inside elements. You can do this:

<CONFIG>
  <L1>
    <L2 L3="data"/>
  </L1>
</CONFIG>

but not this:

<CONFIG>
  <L1>
     <L2>
       <L3>data</L3>
     </L2>
  </L1>
</CONFIG>


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Access XML Nodes Individually with Path String?
« Reply #3 on: February 27, 2020, 10:57:58 pm »
I'm not sure, but I believe you could by accessing node #text.

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: Access XML Nodes Individually with Path String?
« Reply #4 on: February 27, 2020, 11:39:17 pm »
In short  :D (note that I changed `2020` to 'y2020' because XML keys should to start with letter)

Code: Pascal  [Select][+][-]
  1. program xmltest;
  2.  
  3. uses
  4.     Classes,
  5.     XMLRead, XMLWrite, XPath, DOM;
  6.  
  7. const
  8.      xmlContent = '<root><y2020><February><D27>string data</D27></February></y2020></root>';
  9.  
  10. procedure ReadXMLString(out ADoc: TXMLDocument; const AContent: string);
  11. var
  12.     st: TStringStream;
  13. begin
  14.     st := TStringStream.Create(AContent);
  15.     try
  16.         ReadXMLFile(ADoc, st);
  17.     finally
  18.         st.Free;
  19.     end;
  20. end;
  21.  
  22. function WriteXMLString(ADoc: TXMLDocument): string;
  23. var
  24.     st: TStringStream;
  25. begin
  26.     st := TStringStream.Create;
  27.     try
  28.         WriteXMLFile(ADoc, st);
  29.         Result := st.DataString;
  30.     finally
  31.         st.Free;
  32.     end;
  33. end;
  34.  
  35. var
  36.     xml: TXMLDocument;
  37.     node: TXPathVariable;
  38. begin
  39.     ReadXMLString(xml, xmlContent);
  40.     node := EvaluateXPathExpression('/root/y2020/February/D27', xml.DocumentElement);
  41.     WriteLn('Current value: ', node.AsText);
  42.     if node.AsNodeSet.Count > 0 then
  43.     begin
  44.         WriteLn('Modified content:');
  45.         TDOMNode(node.AsNodeSet[0]).TextContent := 'foo bar';
  46.         WriteLn(WriteXMLString(xml));
  47.     end;
  48. end.
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Dennis1

  • New Member
  • *
  • Posts: 14
Re: Access XML Nodes Individually with Path String?
« Reply #5 on: February 28, 2020, 02:04:40 am »
Wow! Thanks! I see I have quite a bit more to learn. 

 

TinyPortal © 2005-2018