It is currently Fri Jan 22, 2021 11:08 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: FormatXML: Tabs instead of spaces
PostPosted: Sun Mar 22, 2020 11:38 am 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 08, 2004 10:29 am
Posts: 282
Location: Zurich, Switzerland
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?

_________________
As you walk on by, Will you call my name? Or will you walk away?


Top
 Profile  
Reply with quote  
 Post subject: Re: FormatXML: Tabs instead of spaces
PostPosted: Sun Mar 22, 2020 2:52 pm 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5209
Location: Germany
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$


Top
 Profile  
Reply with quote  
 Post subject: Re: FormatXML: Tabs instead of spaces
PostPosted: Sun Mar 22, 2020 3:01 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 08, 2004 10:29 am
Posts: 282
Location: Zurich, Switzerland
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: FormatXML: Tabs instead of spaces
PostPosted: Sun Mar 22, 2020 3:15 pm 
Offline
Always Here
Always Here

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 5209
Location: Germany
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$


Top
 Profile  
Reply with quote  
 Post subject: Re: FormatXML: Tabs instead of spaces
PostPosted: Sun Mar 22, 2020 3:32 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Dec 23, 2003 3:54 am
Posts: 1869
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")

_________________
On GitHub: PB Includes - IDE Tools - Color Themes - IDE Branches - TabBarGadget Mods


Top
 Profile  
Reply with quote  
 Post subject: Re: FormatXML: Tabs instead of spaces
PostPosted: Sun Mar 22, 2020 5:32 pm 
Offline
Enthusiast
Enthusiast

Joined: Tue Jun 08, 2004 10:29 am
Posts: 282
Location: Zurich, Switzerland
Feature Request posted, see http://forums.purebasic.com/english/viewtopic.php?f=3&t=74928

_________________
As you walk on by, Will you call my name? Or will you walk away?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: flaith and 47 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye