Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Search/Find and Replace in Java

Here is my the scenario:
Lets say we have 2 files
1. XMl file
2. Excel file

Xml file has a text tage eg: <TEXT>Standard Report</TEXT>

Excel file has 2 columns
TEXT in English         Text IN French
Standard Report          Rapport type

code should read the excle column 'TEXT in English' and find the word in XML file, once found, replace it with excel column 'Text in French'and then move on to the next row of Excel file and process the same until the end of the file.
0
MEINKIYAKAROON
Asked:
MEINKIYAKAROON
  • 7
  • 7
1 Solution
 
timbonicusCommented:
Certainly the hard part about this problem is parsing the files, the actual search and replace is not too difficult. The example below assumes you have read the XML file into a String object with FileReader and parsed the Excel file into a HashMap of <String,String> (text to replace, replacement text).

I used String.replaceAll, but you could certainly do something more powerful with the java.util.regex package (Pattern and Matcher).
	String xmlFile = readXMLFile(myFile);
	HashMap<String,String> replace = readExcelFile(myExcel);
	
	Iterator<String> it = replace.keySet().iterator();
	while (it.hasNext()) {
		String key = it.next();
		xmlFile = xmlFile.replaceAll(key, replace.get(key));
	}
	System.out.println(xmlFile);

Open in new window

0
 
muktajindalCommented:
You may modify the attached code snippet to parse an excel file. Then use the values map to replace the values as described above.

	/*
	 * Function to read values from CSV file and store them in a list 
	 */
	public static Map<String, String> readValuesfromCSV() {
		
		String fName = "..\\TestData\\TestData.csv";
		Map<String, String> map = new HashMap<String, String>();
		FileInputStream fis = null;
		
		try {
			fis = new FileInputStream(fName);
		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}
 
		try {
			BufferedReader br =new BufferedReader(new InputStreamReader(fis));
			String thisLine=br.readLine();
 
			while ((thisLine=br.readLine())!= null) {
				String[] arr=thisLine.split(",");
				if (arr.length!=0) {
					try {
						String english = arr[0];
						String french = arr[1];
						map.put(english, french);
					} catch(Exception e) {
						System.out.println(e.getMessage());
						continue;
					}				 
				}	  
			}
		} catch(Exception e) {
			System.out.println(e.getMessage());
		}
 
		return map;
	}

Open in new window

0
 
MEINKIYAKAROONAuthor Commented:
I really apologise about java knowledge , could you please write me the steps from start to finish and how do I compile this code and test it.

Thanks for yor help
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
muktajindalCommented:
You will need to open your excel file and save it as csv file. Accept all the Yes/No pop-ups while saving as csv. Use the attached java file to use this csv file to modify your xml file. (Remove .txt from all the filenames)
FileWriteSample.java.txt
excel.csv.txt
excel.xls.txt
xml.xml.txt
0
 
muktajindalCommented:
Save csv and xml file in C: drive and then run this java file. You should see the modification done in the xml file as per your csv file entries.
0
 
MEINKIYAKAROONAuthor Commented:
how to run this java file (filewritesample.java), in which environment??
0
 
MEINKIYAKAROONAuthor Commented:
I have attached the error file while running the program.
thanks for your help


FileWriteSample.doc
0
 
MEINKIYAKAROONAuthor Commented:
Mukta, could you please answer as are close to the solution.
 
Thanks
0
 
muktajindalCommented:
Seems to be some classpath issue.. try giving the classpath while executing the class from command line. If you are using any IDE like eclipse, try executing it from there.
0
 
muktajindalCommented:
Are you sure that the java file compiled successfully ? Make sure that package (com.tests) is correct.
Try following command (assuming that FileWriteSample.class is paced in C:\Java) :

java -cp C:\Java com.tests.FileWriteSample

0
 
MEINKIYAKAROONAuthor Commented:
Please see the attached file.
Thanks for your help

Java-Code-error.doc
0
 
muktajindalCommented:
Can you tell me the complete folder path where you have placed FileWriteSample.java ? I really doubt that it does not match the package defined in it.
0
 
muktajindalCommented:
Your scenario is as follows :
- You have defined package as com.tests in FileWriteSample.java
- You have compiled FileWriteSample.java and got FileWriteSample.class
- Both these files are in C:\Java

You need to do the following :
- Create folder structure C:\Java\com\tests\
- Copy FileWriteSample.class to C:\Java\com\tests\
- Execute it using following command from C:\Java
java -cp C:\Java com.tests.FileWriteSample
0
 
MEINKIYAKAROONAuthor Commented:
This is excellent, this code worked as exactly I needed. Very well done.

Thanks a lot :)
0
 
MEINKIYAKAROONAuthor Commented:
Please close this question
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now