Results 1 to 2 of 2
Thread: Replace String
- 07-28-2009, 02:13 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 1
- Rep Power
- 0
Replace String
Hey Guys,
I got a method to replace a file source in an xml file.
Ive read the xml file earlier and got 2 arrays, 1 containing the original names, and 1 containing the new names (and path). position [0] or the first array has the old value of position[0] from the other array.
I've got this code:
But it just doesnt work :(Java Code:String importPath = getServletContext().getRealPath(IMPORT_DIR); String completeFilePath = importPath+"/"+d.getFileName(); File tempFile = new File(completeFilePath); fi.write(tempFile); // Convert the file to a string String content = FileUtils.readFileToString(tempFile); //Remove FM Mess. int begin = content.indexOf("<Document", 0); content = content.substring(begin); if(_originalArray.length != 0){ for(int i=0;i < _originalArray.length;i++){ if(_originalArray[i] != null){ String originalString = "..\\010_images\\" + _originalArray[i]; String replaceString = "/library/" + _c3Array[i]; int placeOfOriginal = content.indexOf(originalString); String content1 = content.substring(0,placeOfOriginal); String content2 = content.substring(placeOfOriginal + originalString.length()); content = content1 + replaceString + content2; } } }
Its a servlet so I dont have a console to see what goes wrong.
The first array containts:
the second array contains:Java Code:Test.doc_0.jpg
Just 1 field per array, because the XML just containts 1 image (for testing purpose).Java Code:2648.jpg
The XML i try it on looks like this:
I highlighted the part that needs to be replaced.Java Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE Document SYSTEM "..//_app//gdsxml.dtd"> <Document origin="Test.doc" runningTab="01" documentType="chapter" measurement="points" added="false" changed="false" deleted="false" relocated="false" color="54,95,145" fontFamily="Cambria" fontSize="14" pageSize="A4" lineSpacing="16.10"> <Block level="1"><Title keepWithPrevious="yes" keepWithNext="yes" added="false" autonum="no" changed="false" deleted="false" relocated="false"> <Weight weightType="bold">Test</Weight> </Title><Text alignment="left" keepWithPrevious="no" keepWithNext="no" hyphenation="yes" added="false" autonum="no" changed="false" deleted="false" relocated="false" /> <Anchor added="false" changed="false" deleted="false" relocated="false"> <Object file="[COLOR="Red"]..\010_images\Test.doc_0.jpg[/COLOR]" height="0.00pt" width="0.00pt" impsize="0.00cm 0.00cm" align="acenter" angle="0" position="below" xoffset="0cm" yoffset="0cm" fileSize="33552" added="false" changed="false" deleted="false" relocated="false" /></Anchor></Block></Document>
I tried to save the result and the System.in to a file but that also didnt work :(
Hope that anyone can help me.
i tried content.replace before but that also didnt work.
Thank you in advance!
---------
Found the solution:
Java Code:if(_originalArray.length != 0){ for(int i=0;i < _originalArray.length;i++){ if(_originalArray[i] != null){ String originalString = "..\\010_images\\" + _originalArray[i]; String replaceString = "library/objects/" + _c3Array[i]; int placeOfOriginal = content.indexOf(originalString); String content1 = content.substring(0,placeOfOriginal); String content2 = content.substring(placeOfOriginal + originalString.length()); content = content1 + replaceString + content2; } } }Last edited by Raeghin; 07-28-2009 at 03:45 PM. Reason: Fixed
- 07-28-2009, 03:58 PM #2
Similar Threads
-
replace a string using Text i/o
By PureAwesomeness in forum New To JavaReplies: 9Last Post: 07-06-2009, 02:38 AM -
[SOLVED] how to replace exact string in java
By pankaj_salwan in forum New To JavaReplies: 22Last Post: 07-08-2008, 09:28 AM -
string replace problem
By soni in forum Advanced JavaReplies: 8Last Post: 07-06-2008, 01:21 AM -
Find and replace ( in a String
By hamish10101 in forum New To JavaReplies: 6Last Post: 01-17-2008, 05:51 AM -
String replace method
By venkata.tarigopula in forum Advanced JavaReplies: 1Last Post: 07-10-2007, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks