• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help required in implementing find/replace like functionality using Java I/O  RSS feed

 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have couple of questions relating to application of Java I/O :

1) I have an XML file in which I have to search a node which is deep inside the hierarchy and then replace it with some other desired text.

I tried using XPath for searching in the XML but its not giving any output in the console. The console is coming as blank.

Is there some other way through which I can search and achieve the desired results? Do I have to use the method of opening the XML file both in Input as well as Output mode and then read until eof and match the text in the XML which I have to search using an IF condition and then replace the text with the desired text. OR there is some other more faster and efficient way.

2) I have a HashMap which contains a set of key value pairs. I have to iterate through this pair and write to a particular file wherein I have a static text prepended and then this value from the HashMap and then another static text postpended.

Please suggest me a method for doing this.


Please help and suggest me the methods for both of these quickly as this is urgent !!!

Thanks.
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hate to see anyone named Siddharth banging his head against a wall. Let's see if we can clear up some of your problems, so you can get back to your bodhi tree.

1. What do you mean nothing is on the console? Do you mean you are printing out a message when XPath finds a match, and you aren't seeing that message? XPath itself won't print anything to the console as far as I can remember.

You can use a SAX parser if that's easier for you. Just look for an open tag matching whatever you are looking for and then output the changes. You can open a file in read/write mode if that's really what you want to do. The problem there is that if the replacement text isn't exactly the same length as the original, you will garble the file. I don't recommend that approach.


2. I'm not sure I understand your confusion here. Iterate through the keys. Retrieve each value. Write the prepend text, write the value, write the postpend text. Where are you stuck?

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!