Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Algorithmen, Datenstrukturen und Klassendesign (https://www.delphipraxis.net/78-algorithmen-datenstrukturen-und-klassendesign/)
-   -   Delphi XMLDocument xml lesen (https://www.delphipraxis.net/205006-xmldocument-xml-lesen.html)

Pfaffe 22. Jul 2020 20:26

XMLDocument xml lesen
 
Folgende XML habe ich:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<masterdata exporttime="2020-07-22 10:55:57">
   <language shortcut="deu_DEU" target_language="nld_NLD">
      <dictionaries>
         <dictionary_entry id="16585" translation="1" dict_shortcut="" upperlower="DictionaryMetadataULNotUsed" singularplural="DictionaryMetadataSPNotUsed" inactive="0" is_formula="0">
            <dict_value>
               <p>das will ich haben</p>
            </dict_value>
usw.
So habe ich es probiert:
Delphi-Quellcode:
XMLDocument1.LoadFromFile(dateiname);
XMLDocument1.Active:= true;
LNodeElement1:= XMLDocument1.ChildNodes.FindNode('masterdata').ChildNodes.FindNode('language').ChildNodes.FindNode('dictionaries');
if LNodeElement1 <> nil then
  begin
    for i:= 0 to 10 do // LNodeElement1.ChildNodes.Count - 1 do
      begin
        LNodeElement2:= LNodeElement1.ChildNodes.Get(i);
        id:= LNodeElement2.Attributes['id'];
        LNodeElement3:= LNodeElement2.ChildNodes.FindNode('dict_value');
        dict_value:= LNodeElement3.???; // <---
        Memo1.Lines.Add(id + ': ' + dict_value);
      end;
  end;
XMLDocument1.Active:= false;
Wie komme ich an "das will ich haben"?

Pfaffe 23. Jul 2020 06:06

AW: XMLDocument xml lesen
 
Bin so an meine Text gekommen:
Delphi-Quellcode:
dict_value:= LNodeElement3.xml.Replace('<dict_value>', '').Replace('</dict_value>', '').Replace('<p>', '').Replace('</p>', '').Replace(sLinebreak, '');
Aber das ist sicher nicht im Sinne der xml-Erfinder?

bcvs 23. Jul 2020 07:39

AW: XMLDocument xml lesen
 
Dein Text steht ja nicht in <dict_value>, sondern in dessen ChildNode <p>

also:
Delphi-Quellcode:
  LNodeElement3:= LNodeElement2.ChildNodes.FindNode('dict_value').ChildNodes.FindNode('p');
  dict_value:= LNodeElement3.Text;
  Memo1.Lines.Add(id + ': ' + dict_value);

Pfaffe 23. Jul 2020 08:27

AW: XMLDocument xml lesen
 
Danke, bei näheren Betrachtungsweise eigentlich klar.
Ich habe noch ein "if LNodeElement3.IsTextElement then" eingesetzt.


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:34 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz