Here is how to format XMLDOM output nicely

Miklos_HollenderMiklos_Hollender Posts: 1,470Member
edited 2014-11-05 in NAV Tips & Tricks
Gave me a hard time, so I thought I would share. If you don't do this, everything will be in one line...
//Name	DataType	Subtype	Length
//Outs	OutStream		
//F	File		
//Reader	Automation	'Microsoft XML, v6.0'.SAXXMLReader60	
//Writer	Automation	'Microsoft XML, v6.0'.MXXMLWriter60	

F.QUERYREPLACE(FALSE);
F.CREATE(FileName);
F.CREATEOUTSTREAM(Outs);

IF ISCLEAR(Writer) THEN
   CREATE(Writer);

Writer.indent := TRUE;
Writer.standalone := FALSE;
Writer.omitXMLDeclaration := TRUE;
Writer.encoding := 'utf-8';

IF ISCLEAR(Reader) THEN
   CREATE(Reader);

Reader.contentHandler := Writer;
Reader.dtdHandler := Writer;   
Reader.errorHandler := Writer;

Reader.putProperty('http://xml.org/sax/properties/declaration-handler', Writer);
Reader.putProperty('http://xml.org/sax/properties/lexical-handler', Writer);

Reader.parse(PXMLDocOut);

PXMLDocOut.loadXML(Writer.output);

PXMLDocOut.save(Outs);
F.CLOSE;

Comments

  • reijermolenaarreijermolenaar Posts: 254Member
    Nice, I have once searched for a solution and did not find one!
    Thanks for sharing!
    Reijer Molenaar
    Object Manager
    Knibble
    TVblik
  • Lars_WestmanLars_Westman Posts: 97Member
    And here's the .Net equivalent:
     PROCEDURE SaveXMLFileOnClient@1000000006(XMLDoc@1000000000 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlDocument";Filename@1000000001 : Text[1024]);
        VAR
          ServerTempFileName@1000000002 : Text[250];
          Writer@1000000003 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlWriter";
          WriterSettings@1000000006 : DotNet "'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Xml.XmlWriterSettings";
        BEGIN
          ServerTempFileName := ServTierMgt.ServerTempFileName('xml');
          WriterSettings := WriterSettings.XmlWriterSettings;
          WriterSettings.Indent := TRUE;
          WriterSettings.OmitXmlDeclaration := TRUE;
          Writer := Writer.Create(ServerTempFileName, WriterSettings);
          XMLDoc.Save(Writer);
          Writer.Close;
          ServTierMgt.DownloadToFile(ServerTempFileName, Filename);
        END;
    
Sign In or Register to comment.