Faithfulman
(Getting the hang of it)
2006-10-16 10:59 PM
XML - writing to existing file

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


LonkeroAdministrator
(KiX Master Guru)
2006-10-17 12:37 AM
Re: XML - writing to existing file

read the file in and write it out and use join() split() to replace the text.

Faithfulman
(Getting the hang of it)
2006-10-17 12:40 AM
Re: XML - writing to existing file

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


Benny69
(MM club member)
2006-10-17 01:31 AM
Re: XML - writing to existing file

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.


Faithfulman
(Getting the hang of it)
2006-10-17 01:59 AM
Re: XML - writing to existing file

I really have not done too much of this. Can someone give me some example or some guidance?

Thanks,

Faithful


Mart
(KiX Supporter)
2006-10-17 09:30 AM
Re: XML - writing to existing file

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]


Faithfulman
(Getting the hang of it)
2006-10-18 01:39 AM
Re: XML - writing to existing file

Code didn't take so I have posted the issue at hand down below:

Les
(KiX Master)
2006-10-18 01:55 AM
Re: XML - writing to existing file

[CODE] tags need to be in lowercase.

Faithfulman
(Getting the hang of it)
2006-10-18 02:00 AM
Re: XML - writing to existing file

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)



Faithfulman
(Getting the hang of it)
2006-10-18 06:54 AM
Re: XML - writing to existing file

Does anyone have any ideas on this??

NTDOCAdministrator
(KiX Master)
2006-10-18 07:20 AM
Re: XML - writing to existing file

What is it doing or not doing that you want it to do?

Gargoyle
(MM club member)
2006-10-18 07:55 AM
Re: XML - writing to existing file

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.


Mart
(KiX Supporter)
2006-10-18 02:02 PM
Re: XML - writing to existing file

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)



Benny69
(MM club member)
2006-10-18 02:34 PM
Re: XML - writing to existing file

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)



Mart
(KiX Supporter)
2006-10-18 03:08 PM
Re: XML - writing to existing file

Yeah I know. Didn't feel like a game of golf so just typed an else for fun

Faithfulman
(Getting the hang of it)
2006-10-18 06:36 PM
Re: XML - writing to existing file

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.


Faithfulman
(Getting the hang of it)
2006-10-18 08:15 PM
Re: XML - writing to existing file

Anyone have any thoughts on this?

Les
(KiX Master)
2006-10-18 08:18 PM
Re: XML - writing to existing file

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?

Faithfulman
(Getting the hang of it)
2006-10-18 08:23 PM
Re: XML - writing to existing file

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


Les
(KiX Master)
2006-10-18 08:50 PM
Re: XML - writing to existing file

The code Benny gave you does just that... replaces whatever with something else.

Mart
(KiX Supporter)
2006-10-18 09:20 PM
Re: XML - writing to existing file

Did some stuff with xml reading and writing some time ago. I'll se if I can brew a script that actually reads and writes the file as xml and not as plain text.

Gimme and hour or so.
Stand by.....


BTW if you want to replace the whole line you could do:

Code:

If $line = "some words here"
$line = "some other words here"
EndIf



Mart
(KiX Supporter)
2006-10-18 09:43 PM
Re: XML - writing to existing file

Do you have the entire structure of the XML file at hand?
If so can you post it or mail (see profile for mail address) it to me? I have a script (thanx to Shawn, Jim and Jose) to write a new xml file using kix.


NTDOCAdministrator
(KiX Master)
2006-10-18 10:02 PM
Re: XML - writing to existing file

You can start reading and learning more about it here.


RFC: XML UDF's jtokach
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=135678
 
RFC: ReadXmlString/WriteXmlString
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=134644
 
RFC: fnLoadXML() - Loads & validates XML documents
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB2&Number=134643


Faithfulman
(Getting the hang of it)
2006-10-18 10:08 PM
Re: XML - writing to existing file

No it does not. It replaces a specific string with something else.

I need to be able to replace < receipt >this is one text < /receipt >
with < receipt >this is another text < /receipt >
even if the XML file looks like < receipt > < /receipt >
Or if it looks like < receipt >this is another another text < /receipt >

I need the script look for line that says < receipt > and be able to replace that with what I want it to be dynamically. Every user's configuration will be slightly different.

Currently his script looks 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")
EndIf
$rc = WriteLine (2, $line + @CRLF)
$line = ReadLine (1)
Loop
;
$rc = Close(1)
$rc = Close(2)



But I don't want to just look for "This is too cool" ... I need to replace that whole line. His code just replaces the text that it searches for. I want to replace everything between < test > and < /test >


Mart
(KiX Supporter)
2006-10-18 10:15 PM
Re: XML - writing to existing file

Slightly different code is needed for that.

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 $line = "<test>This is cool</test>"
$line = "<test>This is hot</test>"
EndIf
$rc = WriteLine (2, $line + @CRLF)
$line = ReadLine (1)
Loop
;
$rc = Close(1)
$rc = Close(2)
Code:




Mart
(KiX Supporter)
2006-10-18 10:21 PM
Re: XML - writing to existing file

Doc,

That's just where I based my xml writing scrip on.

An example from the posts referred to that shows how to write an entirely new XML file.
If you know the new structure and values of the xml file you could do something like this (thanx to Shawn, Jose and Jim).

Remember that reading this XML stuff is very, VERY, VERY , case sensitive. I learned the hard way and that s#cked.

Code:

Break on
;
$filename = "d:\xmltest\test.xml"
;
$xml = LoadXml($filename)
;
$rc = WriteXmlValue($xml, "Test1a/Test1b/Test1c", Test1)
$rc = WriteXmlValue($xml, "Test2a/Test2b/Test2c", Test2)
$rc = WriteXmlValue($xml, "Test3a/Test3b/test3c", Test3)
;
?"Value=" ReadXmlValue($xml, "Test1a/Test1b/Test1c")
;
SaveXml($xml, $filename)
;
$xml = 0
;
Exit 1
;
Function ReadXmlValue($xml, $key, optional $defaultValue)
Dim $sectionNode
$sectionNode = $xml.SelectSingleNode($key);
If NOT $sectionNode
$ReadXmlValue = $defaultValue
Else
$ReadXmlValue = $sectionNode.FirstChild.Text;
EndIf
EndFunction
;
Function LoadXml($filename)
Dim $, $rootNode
$loadXml = CreateObject("Microsoft.XMLDOM");
If NOT $loadXml
Return
EndIf

$= $loadXml.Load($filename)
EndFunction
;
Function WriteXmlValue($xml, $path, $value)
Dim $p, $rootNode, $sectionNode, $parentNode, $childNode
$sectionNode = $xml.SelectSingleNode($path);
If NOT $sectionNode
$parentNode = $xml
For Each $node in Split($path,"/")
$p = $p + $node
$sectionNode = $xml.SelectSingleNode($p)
If NOT $sectionNode
$sectionNode = $xml.CreateElement($node)
$parentNode = $parentNode.AppendChild($sectionNode)
Else
$parentNode = $sectionNode
EndIf
$p = $p + "/"
Next
EndIf
If $sectionNode
$sectionNode.Text = $value
EndIf
EndFunction
;
Function SaveXml($xml, $filename)
$SaveXml = $xml.Save($filename);
EndFunction



Faithfulman
(Getting the hang of it)
2006-10-18 10:46 PM
Re: XML - writing to existing file

Hi Mart,

Your code does not work:

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 $line = "This is cool"
$line = "This is hot"
EndIf
$rc = WriteLine (2, $line + @CRLF)
$line = ReadLine (1)
Loop
;
$rc = Close(1)
$rc = Close(2)



All it does is copy the xml file. I think the problem may be that you are trying to match the line with

Code:

If $line = "This is cool"



The problem is sometimes it is "This is cool" and other times it might be something else. I just want to find the code that has the "" tag and delete it and put a new line there. Can you look at a line of code and search it to see if it contains the "" tag?

Thanks,

Faithful


Mart
(KiX Supporter)
2006-10-18 11:02 PM
Re: XML - writing to existing file

Ok, an other option.
If I understand you last post correctly this should do the trick.
This one checks if both the start and end test tags are in the line. If so it changes the contents of $line to the new stuff including the start and end test tags. Both the tags should be on the same line with the value if not this script fails. A rewrite of the entire xml file with a modified to your situation version of the script I posted above is the road I would try to walk on. Reading and writing xml with kix is new and has not been heavily used before afaik. Shawn, Jim and Jose did some stuff with this but it kind of faded out a little. Recently I picked up reading and writing xml as xml and not text using kix again because we have several projects at work that we can use this for but for me it's also a path full of surprises and new stuff. Learning something new almost each time I use it.

I changed the brackets around test and /test because the board keeps eating them. You should change it back to the proper brackets before using the code.

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, "{test}") AND InStr($line, "{/test}")
$line = "{test}The new stuff should be here{/test}"
EndIf
$rc = WriteLine (2, $line + @CRLF)
$line = ReadLine (1)
Loop
;
$rc = Close(1)
$rc = Close(2)



Faithfulman
(Getting the hang of it)
2006-10-18 11:15 PM
Re: XML - writing to existing file

Okay, this looks like it will do the trick. SO, the last question is ... what if I just want to update the existing file and not create a new one?

Thanks for all your help!
Faithful


Mart
(KiX Supporter)
2006-10-18 11:24 PM
Re: XML - writing to existing file

Hmmmmm..... Damn now you got me pinned in a dark and scary corner

Have not found a way to do this (yet). I'm pretty sure it can be done but I did not found how to make that work yet.

I guess reading the entire source file line by line, writing the outcome to a new file, renaming the original file to .old (or delete it if you are seeking trouble) and renaming the new file to the name the original file had is the quickest way.


Faithfulman
(Getting the hang of it)
2006-10-18 11:32 PM
Re: XML - writing to existing file

Okay, ... so what about escaping quotes? I have the following code:

Code:

$line = "< NetworkPath EditByClient="YES" > Q:\TotaleReceiptsServer < /NetworkPath >"



It doesn't seem to like the quotes around YES. Is there a way to escape the quotations so the print in the new file?

Thanks,

Faithful


Faithfulman
(Getting the hang of it)
2006-10-18 11:50 PM
Re: XML - writing to existing file

If you were wondering what I was talking about before ... the code tags on this forum aren't working ... so I edited my post above.

Benny69
(MM club member)
2006-10-18 11:52 PM
Re: XML - writing to existing file

if you want to be able to modify a file, then you will need to read in the entire file storing it in an array, modify the array, delete the file then rewrite the file back from the array.

you could use a string instead of an array but you are limited to aprox 32,000 chrs per string, you would have to create a stack of strings to accommodate a length greater then 32,000 chrs.


Mart
(KiX Supporter)
2006-10-18 11:55 PM
Re: XML - writing to existing file

Change it to this and it'll work.
See the single quotes around the string. The double quotes inside the string will now stay as they are.

Code:

$line = '< NetworkPath EditByClient="YES" > Q:\TotaleReceiptsServer < /NetworkPath >'



It's midnight here and I think I hear my bed calling me to get my #ss upstairs and go to sleep.
See you guys in the morning.


Faithfulman
(Getting the hang of it)
2006-10-19 12:16 AM
Re: XML - writing to existing file

Thanks Mart, I appreciate your help on this.
Can anyone help me know how to rename a file ?
After I have done this ... I want to be able to
delete file number 1 and rename file 2 to be named the
same as file number 1. This is what I have so far:

del $sourcefile
ren C:\newSourceFile.xml | XMLfile.xml

The delete works, but the rename gives me an error.

Any help would be appreciated!

Thanks,

Faithful


NTDOCAdministrator
(KiX Master)
2006-10-19 02:43 AM
Re: XML - writing to existing file

There is no rename in KiXtart. You would have to SHELL out to %comspec% and rename the file.

Please take a look at SHELL / RUN in the manual and here in the FAQ forum for further information if needed.


Les
(KiX Master)
2006-10-19 04:12 AM
Re: XML - writing to existing file

Quote:

There is no rename in KiXtart.



Sure there is.
RTFM Move.
http://www.kixtart.org/manual_index.html#_Toc146265696


NTDOCAdministrator
(KiX Master)
2006-10-19 05:27 AM
Re: XML - writing to existing file

Sorry, yes you're right. I just rarely use it so forgot about that method.

Mart
(KiX Supporter)
2006-10-19 09:38 AM
Re: XML - writing to existing file

So

Code:

del $sourcefile
move "d:\somenewfile.xml" "d:\somefile.xml"



would work.

Better (safer) would be

Code:

move "d:\somefile.xml" "d:\somefile.old"
move "d:\somenewfile.xml" "d:\somefile.xml"