Link to home
Start Free TrialLog in
Avatar of drtopserv
drtopservFlag for Israel

asked on

open text/xml file in memory and delete entire line if specific string found in it then save

Hi,
I`m seeking for away (fast) to read text/xml in memory then delete entire line if specific string found in it (string: "ID>" .
and to replace a word "root>" found in a line to "usual>" instead , then close save file and close.

i`d like to use access to perform this task using vba.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

in XML, you don't have the notion of "line", only of "nodes"...
of course, the XML file can be considered a "text" file having indeed line breaks, so you might work that way indeed.

you can read the file into memory indeed, do the replacements, and save back.
which part is what you don't know how to do, actually?
Avatar of drtopserv

ASKER

I have google and found many codes around.
http://ezinearticles.com/?How-To-Use-VBA-Code-To-Remove-Blank-Lines-From-A-Text-File&id=7393752
i still can`t figure out how to manipulate the code to delete the entire line of specific string found in it .
in short, the relevant code goes like this
Select Case Len(txt)
  Case 0 
     includeLine = false 

Open in new window


well, instead of that "select case" statements, you do:
if txt like "*ID>*" then includeLine = false

Open in new window


and that variable will just make to skip the line.

tip: instead of using the plain & to concatenate strings like in that article, you really should go the stringbuilder class:
http://www.vbusers.com/code/codeget.asp?ThreadID=601&PostID=1
performance increase, especially for larger files is dramatic
Wow, the article you mention is so complicated for me!
I`m not that strong enough to understand how to modify this code for my needs.

I still missing how to replace string ="dataroot" to "usaul" in the xml/text file
don't bother about that stringbuilder link for the moment, take it for later.

for your "replace", you would do:
if txt like "*dataroot>*" then  txt = replace(txt, "dataroot>", "usual>") 

Open in new window

Also, is it this only way i can delete the line in text file , means to write new file skiping  the lines which i don`t need ?

also i don`t need to write another file, just to overwrite the existing one
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you said "note that I would NOT do that way in really production applications ?"

how you would do it?
as I write: write as new file, and then copy/move if completed.
I have string =generated=2013-09-02T11:43:05">
I need to delete it , or replace it with nothing.. how can i do it , when that date and time change dynamically?
you can add this line:
if txt like "*generated=[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]"">*" then  
  Dim i as integer
  i = instr(1, txt, "generated=")  
  txt = left(txt, i -1) & mid(txt, i + 29)
end if 

Open in new window

thnx alot angelIII, plz I think you are the only one who could help me so far with this problem with my xml .
are you good in exporting tables in access to XML?
I have not actually done that, as I use MS Access close to never in my day to day job.
but it looks like it should be easy:
http://office.microsoft.com/en-us/access-help/exporting-to-xml-from-access-HA001034561.aspx
Yea, but link u mentioned could do only export of a table as a flat, without relating tables.
all element one after the other (no sub-child/sub elements)
I think the best tip then I can give you is to place a dedicated question for that.
put ms access , VBA and XML as your zones.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.