Results 1 to 2 of 2

Thread: Replace String

  1. #1
    Raeghin is offline Member
    Join Date
    Jul 2009
    Posts
    1
    Rep Power
    0

    Unhappy 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:

    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;
                                                                  
       }
      }
    }
    But it just doesnt work :(
    Its a servlet so I dont have a console to see what goes wrong.

    The first array containts:

    Java Code:
    Test.doc_0.jpg
    the second array contains:

    Java Code:
    2648.jpg
    Just 1 field per array, because the XML just containts 1 image (for testing purpose).

    The XML i try it on looks like this:

    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 highlighted the part that needs to be replaced.

    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

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    837
    Rep Power
    6

    Default

    Hi ,
    Before doing indexOf just trim() and afterwards use indexof and try once.

    -Regards
    Ramya
    Ramya:cool:

Similar Threads

  1. replace a string using Text i/o
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 07-06-2009, 02:38 AM
  2. [SOLVED] how to replace exact string in java
    By pankaj_salwan in forum New To Java
    Replies: 22
    Last Post: 07-08-2008, 09:28 AM
  3. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 01:21 AM
  4. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •