Lazarus

Programming => General => Topic started by: P.curtis on March 29, 2020, 10:00:38 pm

Title: XML
Post by: P.curtis 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.
Title: Re: XML
Post by: Leledumbo 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.
Title: Re: XML
Post by: P.curtis 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 (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?
Title: Re: XML
Post by: P.curtis 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.
Title: Re: XML
Post by: eljo 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)
Title: Re: XML
Post by: lucamar 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);
Title: Re: XML
Post by: P.curtis 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.  

Title: Re: XML
Post by: lucamar 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
Title: Re: XML
Post by: P.curtis on March 30, 2020, 02:25:35 pm
It works. Job well done, thanks.
TinyPortal © 2005-2018