#169490 - 2006-10-18 10:02 PM
Re: XML - writing to existing file
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
|
Top
|
|
|
|
#169491 - 2006-10-18 10:08 PM
Re: XML - writing to existing file
|
Faithfulman
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
KiX Supporter
Registered: 2002-03-27
Posts: 4673
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
KiX Supporter
Registered: 2002-03-27
Posts: 4673
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
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
|
|
|
|
#169496 - 2006-10-18 11:15 PM
Re: XML - writing to existing file
|
Faithfulman
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
|
|
|
|
#169498 - 2006-10-18 11:32 PM
Re: XML - writing to existing file
|
Faithfulman
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
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
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)
|
Top
|
|
|
|
#169502 - 2006-10-19 12:16 AM
Re: XML - writing to existing file
|
Faithfulman
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 369 anonymous users online.
|
|
|