Recent

Author Topic: Function to quickly format XML  (Read 4095 times)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1269
Function to quickly format XML
« on: September 26, 2013, 04:48:20 am »
G'day,

I have XML in two formats.  The first is a string, the second is a TStringList.  In each case I would like to tidy the XML before exporting it.  Indented tags.

I've established the following does the trick...

Code: [Select]
uses 
  DOM, XMLRead, XMLWrite;

var
  sNewFilename: String;
  oDOM : TXMLDocument;
  dt : TDateTime;
begin
  sNewFilename := ExcludeTrailingBackslash(ExtractFileDir(FileNameEdit1.FileName)) + '\New.xml';

  oDOM := TXMLDocument.Create;
  Try
    dt := Now;
    ReadXMLFile(oDOM, FileNameEdit1.FileName);

    Memo1.Lines.Add('Reading Time');
    Memo1.Lines.Add(Format('%.1f msec', [24*60*60*1000*(Now-dt)]));

    dt:= Now;
    WriteXMLFile(oDOM, sNewFilename);

    Memo1.Lines.Add('Writing Time');
    Memo1.Lines.Add(Format('%.1f msec', [24*60*60*1000*(Now-dt)]));
  finally
    oDom.Free;
  end;
end;

My problem is the above is slow.  30 millisec to read the file.  7 millisec to write it. 

I'm only tidying the XML to scratch an pedantic itch.  No user will ever see the XML, so who cares what it looks like?  Answer: I do :-(   But I'm not willing to pay a nearly 40 millisec cost :-(

Anyone know of a fast routine somewhere that will tidy XML?
Lazarus Trunk/FPC latest fixes on Windows 11
  I'm getting old and stale.  Slowly getting used to git, I'll get there...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8834
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Function to quickly format XML
« Reply #1 on: September 26, 2013, 08:28:18 am »
ReadXMLFile builds tree in the background, which is why it's slow. For a faster one, you may want to open it as text and indent manually based on first tag in the current line (maintain a stack of tags to determine the indentation depth).

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: Function to quickly format XML
« Reply #2 on: September 26, 2013, 02:08:56 pm »
Please use NativeXML (http://www.simdesign.nl/forum/viewtopic.php?f=2&t=30860), where they claim :

Quote
// TNativeXml is a fast XML parser (parsing on typical hardware storage
  // 15 Mb per second), because it loads external data in chunks and buffers it in
  // memory. Use Create to create a new instance, use LoadFromFile/LoadFromStream to
  // load the XML document from a file or stream, and use SaveToFile and SaveToStream to
  // save the XML document.

I try to read my XML file with NativeXML from my DLL routines with thread. Thread is usefull to avoid hang/crash when opening apps with a lot of data.

This is my schema to grab XML data :
MyApp + thread to load a DLL library <-- DLL libray + NativeXML + synapse <-- XML data + Web server + database

Hope this could help you.

 

TinyPortal © 2005-2018