Forum > Networking and Web Programming

[Solved] Read XML from string

(1/1)

SKBotNL:
Hello,
I have this XML data:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---<s:Envelope        xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">        <s:Body>                <u:GetVolumeResponse                        xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">                        <CurrentVolume>6</CurrentVolume>                </u:GetVolumeResponse>        </s:Body></s:Envelope>And I need to read "CurrentVolume", when creating a TXMLDocument from a TMemoryStream with the string I get the error "root element is missing".
How can I do this?

SKBotNL:
I have solved the issue with this code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---doc := TXMLDocument.Create;ReadXMLFile(doc, TStringStream.Create(s));Reading the docs is hard sometimes ;)

wp:
You are creating two memory leaks here:

* doc := TXMLDocument.Create creates an XMLDocument, but ReadXMLFile does the same and overwrites the variable pointing to the XMLDocument created in the first line so that you cannot destroy the doc created by yourself any more. --> Simply call ReadXMLFile, without creating the XMLDocument yourself
* TStringStream.Create(s) creates a TStringStream, but you have no access to it for destroying the stream later when you are done with reading the xml. --> Assign the created TStringStream to a variable in your code and use that in ReadXMLFile
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  stream: TStringStream;  doc: TXMLDocument = nil;begin  stream := TStringStream.Create(s);  try    ReadXMLFile(doc, stream);   // This creates the doc.    ...  finally    doc.Free;    stream.Free;  end;

SKBotNL:

--- Quote from: wp on May 13, 2023, 06:37:03 pm ---You are creating two memory leaks here:

* doc := TXMLDocument.Create creates an XMLDocument, but ReadXMLFile does the same and overwrites the variable pointing to the XMLDocument created in the first line so that you cannot destroy the doc created by yourself any more. --> Simply call ReadXMLFile, without creating the XMLDocument yourself
* TStringStream.Create(s) creates a TStringStream, but you have no access to it for destroying the stream later when you are done with reading the xml. --> Assign the created TStringStream to a variable in your code and use that in ReadXMLFile
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  stream: TStringStream;  doc: TXMLDocument = nil;begin  stream := TStringStream.Create(s);  try    ReadXMLFile(doc, stream);   // This creates the doc.    ...  finally    doc.Free;    stream.Free;  end;
--- End quote ---
Thank you!

Navigation

[0] Message Index

Go to full version