Recent

Author Topic: XML  (Read 531 times)

P.curtis

  • Jr. Member
  • **
  • Posts: 80
XML
« on: March 29, 2020, 10:00:38 pm »
Hi All,
I was just wondering how do I get/set the standalone="yes" attribute in a XML file?
I use Laz2_XMLRead and Laz2_XMLWrite to work with my XML files.

Thanks

Phil.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8222
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: XML
« Reply #1 on: March 30, 2020, 05:26:34 am »
XML's top level entity is a node, you can't have an attribute without a node.

P.curtis

  • Jr. Member
  • **
  • Posts: 80
Re: XML
« Reply #2 on: March 30, 2020, 06:17:33 am »
Thanks for the reply.

That I understand. I'm new to this and maybe I asked incorrectly. Sorry.

To make myself clear I have the following XML file :-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<musicvideo>
  <title>Pulse</title>
  <artist>Pink Floyd</artist>
</musicvideo>

The first line is what concerns me. How do I create this using FPC?

I read this :-

https://wiki.freepascal.org/Talk:XML_Tutorial

The XML declaration <?xml version="1.0"?> is not a part of node tree. DOM specification levels 1 and 2 do not provide any means to modify it, but DOM level 3 introduces Document.xmlVersion, Document.xmlEncoding and Document.xmlStandalone properties for this purpose. xmlStandalone is not yet implemented by fcl-xml.

So my further question is this - Are there any FPC tools / components to handle DOM 3?

P.curtis

  • Jr. Member
  • **
  • Posts: 80
Re: XML
« Reply #3 on: March 30, 2020, 10:30:18 am »
OK, I decide to do this the easy way .....

Code: Pascal  [Select][+][-]
  1.   writeXMLFile(Doc, 'out.xml');
  2.  
  3.   MyString := TStringList.Create;
  4.  
  5.   MyString.LoadFromFile('out.xml');
  6.  
  7.   MyString.Strings[0] := '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  8.  
  9.   Memo1.clear;
  10.   Memo1.Lines.Assign(MyString);
  11.  
  12.   MyString.Free;
  13.  

The above works, but I would like to write the XML doc to a stream and then assign that stream to the memo.

Can anyone help? Please.

eljo

  • Sr. Member
  • ****
  • Posts: 407
Re: XML
« Reply #4 on: March 30, 2020, 10:40:28 am »
OK, I decide to do this the easy way .....

Code: Pascal  [Select][+][-]
  1.   writeXMLFile(Doc, 'out.xml');
  2.  
  3.   MyString := TStringList.Create;
  4.  
  5.   MyString.LoadFromFile('out.xml');
  6.  
  7.   MyString.Strings[0] := '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  8.  
  9.   Memo1.clear;
  10.   Memo1.Lines.Assign(MyString);
  11.  
  12.   MyString.Free;
  13.  

The above works, but I would like to write the XML doc to a stream and then assign that stream to the memo.

Can anyone help? Please.
open the writexmlfile it probably uses a TFileStream internally  that will give you the exact code you need (I did not test my assumption)

lucamar

  • Hero Member
  • *****
  • Posts: 2920
Re: XML
« Reply #5 on: March 30, 2020, 10:54:41 am »
The above works, but I would like to write the XML doc to a stream and then assign that stream to the memo.

There is an overload of WriteXMLFile() that saves to a stream, declared as:

Code: Pascal  [Select][+][-]
  1. procedure WriteXMLFile(doc: TXMLDocument; AStream: TStream; Flags: TXMLWriterFlags = []); overload;

Assigning then to a memo is as simple as doing:

Code: Pascal  [Select][+][-]
  1. MyMemo.Lines.LoadFromStream(MyXMLStream);
« Last Edit: March 30, 2020, 11:00:26 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

P.curtis

  • Jr. Member
  • **
  • Posts: 80
Re: XML
« Reply #6 on: March 30, 2020, 11:21:55 am »
I tried that using a TMemoryStream but it doesn't work.

Code: Pascal  [Select][+][-]
  1.  MyMemoryStream := TMemoryStream.Create;
  2.   MyString := TStringList.Create;
  3.  
  4.   writeXMLFile(Doc, MyMemoryStream);
  5.  
  6.   MyString.LoadFromStream(MyMemoryStream);
  7.  
  8.   MyString.Strings[0] := '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  9.  
  10.   Memo1.clear;
  11.   Memo1.Lines.Assign(MyString);
  12.  
  13.   MyMemoryStream.Free;
  14.   MyString.Free;                
  15.  


lucamar

  • Hero Member
  • *****
  • Posts: 2920
Re: XML
« Reply #7 on: March 30, 2020, 02:07:11 pm »
Try this; untested, but should work:

Code: Pascal  [Select][+][-]
  1.       MyMemoryStream := TMemoryStream.Create;
  2.       try
  3.         writeXMLFile(Doc, MyMemoryStream);
  4.         MyMemoryStream.Seek(0, soFromBeginning);
  5.         Memo1.Lines.LoadFromStream(MyMemoryStream);
  6.         Memo1.Lines[0] := '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
  7.       finally
  8.         MyMemoryStream.Free;
  9.       end;

Assigning to TMemo.Lines is usually not a good idea; instead use either:
Code: [Select]
    Memo.Lines.Clear;
    Memo.Lines.AddStrings(AStringList);
or
Code: [Select]
    Memo.Lines.Text := AStringList.Text
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

P.curtis

  • Jr. Member
  • **
  • Posts: 80
Re: XML
« Reply #8 on: March 30, 2020, 02:25:35 pm »
It works. Job well done, thanks.
« Last Edit: March 30, 2020, 02:49:12 pm by P.curtis »

 

TinyPortal © 2005-2018