Page 1 of 2 12>
Topic Options
#169468 - 2006-10-16 10:59 PM XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Hi Guys, I'm not sure what happened to my last post, but it didn't seem to post early this morning, so I am going to try this again.

I need to be able to use the KIX script to rewrite what is in the following tag:


C:\windows\folderName\


and become (replace with):


G:\NEWfolderName\


Does anyone have any ideas to help me out?

Thanks!

Faithful

Top
#169469 - 2006-10-17 12:37 AM Re: XML - writing to existing file
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
read the file in and write it out and use join() split() to replace the text.
Top
#169470 - 2006-10-17 12:40 AM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
You can't just replace it like you could with an ini file?

[CODE]<Receipt value='YES'>G:\Windows\NewFolder\</Receipt>[/CODE]

It's a rather large file, and I just want to modify the one entry. Any other ideas or is that they only way?

Thanks,

Faithful


Edited by Faithfulman (2006-10-17 12:41 AM)

Top
#169471 - 2006-10-17 01:31 AM Re: XML - writing to existing file
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
Faithfulman,
what Jooel is saying is that a .xml file is like a .txt file and can be opened and then read each line looking for the appropriate text then once you find it write the line back as you desire then close the file and your done.
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#169472 - 2006-10-17 01:59 AM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
I really have not done too much of this. Can someone give me some example or some guidance?

Thanks,

Faithful

Top
#169473 - 2006-10-17 09:30 AM Re: XML - writing to existing file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Something like this.
Not tested but it looks like it should work just fine.

Code:

Break on
;
$sourcefile = "c:\sourcexmlfile.xml"
$newfile = "c:\newxmlfile.xml"
;
$rc = Open (1, $sourcefile, 2)
$rc = Open (2, $newfile, 5)
;
$line = ReadLine (1)
If InStr ($line, "C:\windows\folderName\")
$line = Split($line, "C:\windows\folderName\")
$line = Join ($line, "G:\NEWfolderName\")
$rc = WriteLine (2, $line + @CRLF)
Else
$rc = WriteLine (2, $line + @CRLF)
EndIf
;
$rc = Close(1)
$rc = Close(2)



[edit]
Hmmmm.....
There could be an issue with paths that have C:\windows\folderName\ in it and should not be changed.
Depends on your situation and if all the lines that have C:\windows\folderName\ in it should be changed.
[/edit]


Edited by Mart (2006-10-17 09:42 AM)

Top
#169474 - 2006-10-18 01:39 AM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Code didn't take so I have posted the issue at hand down below:

Edited by Faithfulman (2006-10-18 06:53 AM)

Top
#169475 - 2006-10-18 01:55 AM Re: XML - writing to existing file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
[CODE] tags need to be in lowercase.
Top
#169476 - 2006-10-18 02:00 AM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Okay, from this
Quote:


<Receipt>
<test>This is too cool</test>
</Receipt>





to

Quote:


<Receipt>
<test>This is too cool</test>
</Receipt>





Using This

Code:

Break on
;
$sourcefile = "c:\XMLfile.xml"
$newfile = "c:\newXMLfile.xml"
;
$rc = Open (1, $sourcefile, 2)
$rc = Open (2, $newfile, 5)
;
$line = ReadLine (1)
If InStr ($line, "This is too cool")
$line = Split($line, "This is too cool")
$line = Join ($line, "Dude")
$rc = WriteLine (2, $line + @CRLF)
Else
$rc = WriteLine (2, $line + @CRLF)
EndIf
;
$rc = Close(1)
$rc = Close(2)



Edited by Faithfulman (2006-10-18 02:02 AM)

Top
#169477 - 2006-10-18 06:54 AM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Does anyone have any ideas on this??
Top
#169478 - 2006-10-18 07:20 AM Re: XML - writing to existing file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
What is it doing or not doing that you want it to do?
Top
#169479 - 2006-10-18 07:55 AM Re: XML - writing to existing file
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Quote:


Okay, from this

Quote:
--------------------------------------------------------------------------------



This is too cool




--------------------------------------------------------------------------------



to


Quote:
--------------------------------------------------------------------------------



This is too cool




--------------------------------------------------------------------------------





I am missing something, they both look exactly the same to me.

Top
#169480 - 2006-10-18 02:02 PM Re: XML - writing to existing file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Oops made a mistake in the code I posted above.
Fixed the code you posted and tested it. Works ok like this.

Code:

Break on
;
$sourcefile = "d:\XMLfile.xml"
$newfile = "d:\newXMLfile.xml"
;
$rc = Open (1, $sourcefile, 2)
$rc = Open (2, $newfile, 5)
;
$line = ReadLine (1)
While @ERROR = 0
If InStr ($line, "This is too cool")
$line = Split($line, "This is too cool")
$line = Join ($line, "Dude")
$rc = WriteLine (2, $line + @CRLF)
Else
$rc = WriteLine (2, $line + @CRLF)
EndIf
$line = ReadLine (1)
Loop
;
$rc = Close(1)
$rc = Close(2)

_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#169481 - 2006-10-18 02:34 PM Re: XML - writing to existing file
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
mart,
'Else' is redundant
Code:

Break on
;
$sourcefile = "d:\XMLfile.xml"
$newfile = "d:\newXMLfile.xml"
;
$rc = Open (1, $sourcefile, 2)
$rc = Open (2, $newfile, 5)
;
$line = ReadLine (1)
While @ERROR = 0
If InStr ($line, "This is too cool")
$line = Split($line, "This is too cool")
$line = Join ($line, "Dude")
EndIf
$rc = WriteLine (2, $line + @CRLF)
$line = ReadLine (1)
Loop
;
$rc = Close(1)
$rc = Close(2)

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#169482 - 2006-10-18 03:08 PM Re: XML - writing to existing file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Yeah I know. Didn't feel like a game of golf so just typed an else for fun
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#169483 - 2006-10-18 06:36 PM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Okay guys, yes this did work ... but not exactly how I thought it was going to.

I want to be able to take whatever is in the "<'test'>" tags and replace it with whatever I choose to. So basically, if I have this:

Code:
< Receipt >
< test >D:\windows\folder1\< /test >
< /Receipt >



or if I have


Code:
< Receipt >
< test >C:\juicyfruit\dumplingsandgravey\< /test >
< /Receipt >




I want to be able to strip that whole thing out and replace it
with:

Code:
 < Receipt >
< test >D:\windows\folder44< /test >
< /Receipt >



Is there a way to just find the line that holds the "< test >" tags and replace what's between the "< test >" tags?

Thanks,

Faithful

P.S. The forum keeps removing my tags as soon as post, sorry about the extra spaces.

Top
#169484 - 2006-10-18 08:15 PM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Anyone have any thoughts on this?
Top
#169485 - 2006-10-18 08:18 PM Re: XML - writing to existing file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
You present a very artificial albeit simple example. In that case you could simply replace the whole line. What if it were a multiline value?
Top
#169486 - 2006-10-18 08:23 PM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
Right but the line could be many different things. So, If I could replace the whole line that would be fine. Just not sure how to go about replacing the whole line? Any thoughts?

Thanks,

Faithful

Top
#169487 - 2006-10-18 08:50 PM Re: XML - writing to existing file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
The code Benny gave you does just that... replaces whatever with something else.
Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 466 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.093 seconds in which 0.028 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org