File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes read and replace text in file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "read and replace text in file" Watch "read and replace text in file" New topic
Author

read and replace text in file

sreeni san
Ranch Hand

Joined: Aug 14, 2001
Posts: 39
HI
I have a xml file shown below
<?xml version="1.0" standalone="no"?>
<destination>
<file name="check.xml">
<tag type="line">data to be replaced as line1</tag>
<junk_tag>xyz</junk_tag>
<tag type="line">data to be replaced as line1</tag>
<tag type="line">data to be replaced as line3</tag>
<tag type="img>c:\....</tag>

</file>
</destination>

I need to read the file and check for <tag type="line"> and replace the value between
<tag type="line"> </tag> in the file.
Also I need to store the value of the tag in a hash table with key value pair as for example
[line1(key), data to be replaced as line1(value)]
How do I do it?
Thanks
sreeni
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Well, I do not think you need to parse the XML file at all. You can use regular expressions. J2SE 1.4 now has a regexp package that can handle regular expressions very well. Check it out!


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Adrian Burke
Greenhorn

Joined: Apr 12, 2011
Posts: 11
Right now, I am defining my String values within my class. I am not catering for multiple Strings to be replaced.

Here is where I'm at:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32599
    
    4
Why are you using Streams rather than Readers if you are dealing with text?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read and replace text in file
 
Similar Threads
MDB Topic
Inhertiting complex type in XML Schema
Message Driven Bean (Topic)
BufferedReader and the ] character
JAXB - complex hashmap