PureBasic Forum
http://forums.purebasic.com/english/

FormatXML: Tabs instead of spaces
http://forums.purebasic.com/english/viewtopic.php?f=13&t=74925
Page 1 of 1

Author:  wayne-c [ Sun Mar 22, 2020 11:38 am ]
Post subject:  FormatXML: Tabs instead of spaces

From the manual:
Code:
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?

Author:  infratec [ Sun Mar 22, 2020 2:52 pm ]
Post subject:  Re: FormatXML: Tabs instead of spaces

Use an ident which does not occure in normal text (4 in my example) and replace it afterwards with the #TAB$

Code:
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$

Author:  wayne-c [ Sun Mar 22, 2020 3:01 pm ]
Post subject:  Re: FormatXML: Tabs instead of spaces

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...

Author:  infratec [ Sun Mar 22, 2020 3:15 pm ]
Post subject:  Re: FormatXML: Tabs instead of spaces

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:
FormatXML(xml, #PB_XML_ReFormat, 100)

XML$ = ComposeXML(xml)

Debug XML$

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

Debug XML$

Author:  kenmo [ Sun Mar 22, 2020 3:32 pm ]
Post subject:  Re: FormatXML: Tabs instead of spaces

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:
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")

Author:  wayne-c [ Sun Mar 22, 2020 5:32 pm ]
Post subject:  Re: FormatXML: Tabs instead of spaces

Feature Request posted, see http://forums.purebasic.com/english/viewtopic.php?f=3&t=74928

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/