Page 2 of 2 <12
Topic Options
#169488 - 2006-10-18 09:20 PM Re: XML - writing to existing file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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



Edited by Mart (2006-10-18 09:22 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

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

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#169490 - 2006-10-18 10:02 PM Re: XML - writing to existing file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11603
Loc: CA
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


Edited by NTDOC (2006-10-18 10:04 PM)

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

Registered: 2005-02-22
Posts: 68
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 >

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

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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:






Edited by Mart (2006-10-18 10:16 PM)

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

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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



Edited by Mart (2006-10-18 10:24 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

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

Registered: 2005-02-22
Posts: 68
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

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

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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)

_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

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

Registered: 2005-02-22
Posts: 68
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

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

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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.


Edited by Mart (2006-10-18 11:29 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#169498 - 2006-10-18 11:32 PM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
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


Edited by Faithfulman (2006-10-18 11:42 PM)

Top
#169499 - 2006-10-18 11:50 PM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
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.
Top
#169500 - 2006-10-18 11:52 PM Re: XML - writing to existing file
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
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.


Edited by benny69 (2006-10-18 11:55 PM)
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

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

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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.


Edited by Mart (2006-10-18 11:59 PM)
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#169502 - 2006-10-19 12:16 AM Re: XML - writing to existing file
Faithfulman Offline
Getting the hang of it

Registered: 2005-02-22
Posts: 68
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

Top
#169503 - 2006-10-19 02:43 AM Re: XML - writing to existing file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11603
Loc: CA
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.

Top
#169504 - 2006-10-19 04:12 AM Re: XML - writing to existing file
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Quote:

There is no rename in KiXtart.



Sure there is.
RTFM Move.
http://www.kixtart.org/manual_index.html#_Toc146265696
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#169505 - 2006-10-19 05:27 AM Re: XML - writing to existing file
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11603
Loc: CA
Sorry, yes you're right. I just rarely use it so forgot about that method.
Top
#169506 - 2006-10-19 09:38 AM Re: XML - writing to existing file
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4665
Loc: The Netherlands
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"

_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 2 of 2 <12


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

Who's Online
1 registered (vertage) and 155 anonymous users online.
Newest Members
vertage, dimi575, Drecksnacken, Nlill, webtools321
17735 Registered Users

Generated in 0.084 seconds in which 0.03 seconds were spent on a total of 12 queries. Zlib compression enabled.

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