FormatXML: Tabs instead of spaces

Just starting out? Need help? Post your questions and find answers here.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

FormatXML: Tabs instead of spaces

Post 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?
As you walk on by, Will you call my name? Or will you walk away?
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: FormatXML: Tabs instead of spaces

Post 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$
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: FormatXML: Tabs instead of spaces

Post 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...
As you walk on by, Will you call my name? Or will you walk away?
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: FormatXML: Tabs instead of spaces

Post 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$
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: FormatXML: Tabs instead of spaces

Post 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")
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: FormatXML: Tabs instead of spaces

Post by wayne-c »

Feature Request posted, see viewtopic.php?f=3&t=74928
As you walk on by, Will you call my name? Or will you walk away?
Post Reply