Page 1 of 1

FormatXML: Tabs instead of spaces

Posted: Sun Mar 22, 2020 11:38 am
by wayne-c
From the manual:

Code: Select all

FormatXML(#XML, Flags [, IndentStep])

IndentStep (optional) : The indent to apply (in characters) when using the #PB_XML_ReFormat or #PB_XML_ReIndent flags
Indentation is done with spaces - how can I have real tab (#TAB$) characters instead of the spaces?

Re: FormatXML: Tabs instead of spaces

Posted: Sun Mar 22, 2020 2:52 pm
by infratec
Use an ident which does not occure in normal text (4 in my example) and replace it afterwards with the #TAB$

Code: Select all

xml = CreateXML(#PB_Any) 
mainNode = CreateXMLNode(RootXMLNode(xml), "Zoo") 

item = CreateXMLNode(mainNode, "Animal") 
SetXMLAttribute(item, "id", "1") 
SetXMLNodeText(item, "Elephant") 

item = CreateXMLNode(mainNode, "Animal") 
SetXMLAttribute(item, "id", "2") 
SetXMLNodeText(item, "Tiger") 

FormatXML(xml, #PB_XML_ReFormat, 4)

XML$ = ComposeXML(xml)

Debug XML$

XML$ = ReplaceString(XML$, "    ", #TAB$)

Debug XML$

Re: FormatXML: Tabs instead of spaces

Posted: Sun Mar 22, 2020 3:01 pm
by wayne-c
infratec wrote:Use an ident which does not occure in normal text (4 in my example) and replace it afterwards with the #TAB$

XML$ = ReplaceString(XML$, " ", #TAB$)
[/code]
No, that does not work as the 4 spaces may occur inside regular node text or inside CDATA.

It is possible to do this with RegExp but the most elegant solution would be that the FormatXML function has an additional flag #PB_XML_IndentUseTabs...

Re: FormatXML: Tabs instead of spaces

Posted: Sun Mar 22, 2020 3:15 pm
by infratec
I agree, but for now you have to use what's available.
Since I don't like to include the large regeex lib I still prefer the spaces :wink:

If you use 100 or more it will be more 'safe'.

Code: Select all

FormatXML(xml, #PB_XML_ReFormat, 100)

XML$ = ComposeXML(xml)

Debug XML$

XML$ = ReplaceString(XML$, Space(100), #TAB$)

Debug XML$

Re: FormatXML: Tabs instead of spaces

Posted: Sun Mar 22, 2020 3:32 pm
by kenmo
I would post this in Feature Requests... I'm surprised it's not already implemented!

Here's a procedure based on infratec's suggestion... silly, but it should work :lol:

Code: Select all

Procedure FormatXMLWithTabs(XML.i)
  FormatXML(XML, #PB_XML_ReFormat)
  Text.s = ComposeXML(XML)
  nSpaces.i = 4
  IndentStr.s = Space(nSpaces)
  While (FindString(Text, IndentStr))
    nSpaces * 2
    IndentStr = Space(nSpaces)
  Wend
  FormatXML(XML, #PB_XML_ReFormat, nSpaces)
  ParseXML(XML, ReplaceString(ComposeXML(XML), IndentStr, #TAB$))
EndProcedure

CreateXML(0)

*Main = CreateXMLNode(RootXMLNode(0), "main")
*Child = CreateXMLNode(*Main, "child")
For i = 1 To 3
  CreateXMLNode(*Child, "sub")
Next i
*Child = CreateXMLNode(*Main, "child")
For i = 1 To 3
  CreateXMLNode(*Child, "sub")
Next i

FormatXMLWithTabs(0)

SetCurrentDirectory(GetTemporaryDirectory())
SaveXML(0, "temp.xml")
RunProgram("temp.xml")

Re: FormatXML: Tabs instead of spaces

Posted: Sun Mar 22, 2020 5:32 pm
by wayne-c
Feature Request posted, see viewtopic.php?f=3&t=74928