Regular expression help Or XML Help

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 501
Joined: Tue Oct 14, 2014 12:09 pm

Regular expression help Or XML Help

Post by loulou2522 »

I am looking for the solution to the following problem
Either the following XML string
<AdrTp> </AdrTp>
<Name> </Name>
When I create the xml file I get the following result
<AddrTp/>
<Name/>
But I absolutely need the following exit
<AdrTp></AdrTp>
<Name></Name>
Is it possible to get this result in XML creation or if not how can I solve the problem with a regular expression (I couldn't find the right expression so if someone had it it would be nice to pass it to me)
Thank you in advance
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Regular expression help Or XML Help

Post by helpy »

Something like this:

Code: Select all

EnableExplicit

Procedure.s ReplaceSeflClosingXmlTags(xmlString.s)
	Static r
	If Not r 
		r = CreateRegularExpression(#PB_Any, "<([^ >]+)([^>]*)/>")
		If Not r : ProcedureReturn #Empty$ : EndIf
	EndIf
	While ExamineRegularExpression(r, xmlString)
		If NextRegularExpressionMatch(r)
			xmlString = Left(xmlString, RegularExpressionMatchPosition(r)-1) + 
			        "<" + RegularExpressionGroup(r, 1) + RegularExpressionGroup(r, 2) + ">" +
			        "</" + RegularExpressionGroup(r, 1) + ">" + 
			        Right(xmlString, Len(xmlString) - RegularExpressionMatchLength(r) - RegularExpressionMatchPosition(r) +1)
		Else
			Break
		EndIf
	Wend
	ProcedureReturn xmlString
EndProcedure

Define xml
Define nMain
Define nAdrTpl
Define nName
Define sXML1.s
Define sXML2.s

xml = CreateXML(#PB_Any)
nMain = CreateXMLNode(RootXMLNode(xml), "Main")
nAdrTpl = CreateXMLNode(nMain, "AdrTpl")
nName = CreateXMLNode(nMain, "Name")

sXML1 = ComposeXML(xml)
sXML2 = ReplaceSeflClosingXmlTags(sXML1)

Debug sXML1
Debug "----------"
Debug sXML2
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Regular expression help Or XML Help

Post by helpy »

This one should be more effective, if there are repetitions of self-closing tags:

Code: Select all

Procedure.s ReplaceSeflClosingXmlTags(xmlString.s)
	Static r
	Protected search.s, replace.s
	
	If Not r 
		r = CreateRegularExpression(#PB_Any, "<([^ >]+)([^>]*)/>")
		If Not r : ProcedureReturn #Empty$ : EndIf
	EndIf
	
	While ExamineRegularExpression(r, xmlString)
		If NextRegularExpressionMatch(r)
			search = RegularExpressionMatchString(r)
			replace = "<" + RegularExpressionGroup(r, 1) + RegularExpressionGroup(r, 2) + ">" +
			          "</" + RegularExpressionGroup(r, 1) + ">"
			xmlString = ReplaceString(xmlString, search, replace)
		Else
			Break
		EndIf
	Wend
	ProcedureReturn xmlString
EndProcedure
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Regular expression help Or XML Help

Post by infratec »

My version without additional libraries:

Code: Select all

xml = CreateXML(#PB_Any)
nMain = CreateXMLNode(RootXMLNode(xml), "Main")
nAdrTpl = CreateXMLNode(nMain, "AdrTpl")
SetXMLNodeText(nAdrTpl, " ")

nName = CreateXMLNode(nMain, "Name")
SetXMLNodeText(nName, " ")

XML$ = ComposeXML(xml)

xml$ = ReplaceString(XML$, "> <", "><")

Debug XML$
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Regular expression help Or XML Help

Post by helpy »

infratec wrote:My version without additional libraries:
This only works if your application does not need to handle slingle spaces as node text!
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Regular expression help Or XML Help

Post by infratec »

Ok, so replace > < by >jqzwguqfguqwezgfuz<

I think thats secure enough :wink:
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Regular expression help Or XML Help

Post by infratec »

Special version for helpy:

Code: Select all

#DummyRemovalText$ = "xyz123-321zxy"

xml = CreateXML(#PB_Any)
nMain = CreateXMLNode(RootXMLNode(xml), "Main")
nAdrTpl = CreateXMLNode(nMain, "AdrTpl")
SetXMLNodeText(nAdrTpl, #DummyRemovalText$)

nName = CreateXMLNode(nMain, "Name")
SetXMLNodeText(nName, #DummyRemovalText$)

XML$ = ComposeXML(xml)

xml$ = ReplaceString(XML$, ">" + #DummyRemovalText$ + "<", "><")

Debug XML$
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Regular expression help Or XML Help

Post by helpy »

:!: :idea: TOP :thumbs-up:
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Post Reply