Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Need help performing a find and replace on an XML file using Java

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help performing a find and replace on an XML file using Java

    I have an XML file that I'm trying to call the String replace() method on it's content after I read the content into a String Object. I tried this step a few different ways and some have been cleaner than others, but I don't think this step is causing the problem. The step seems to be occurring with the actual replace call. I'm iterating through a list of key/value pairs and replacing every key with the corresponding value, which works, but when it's complete the XML file is missing random text throughout the file. The crazy part is that the text that is missing, isn't anything that i'm trying to replace. I've been able to isolate a list of Strings/text that are being removed from the final result, the list is as follows:
    BSB
    BSTB
    MI
    ABCT
    Distribution
    Capture
    Manag

    Please HELP!!! I've been looking at this for two days now.

    I apologize if this is in the wrong thread but I'm new to the forum and wasn't sure where to post.

    Here is the method that i'm working with
    public static void findAndReplace(String goodXMLPath)
    	{
    		// Local Variables
    		Path path;
    		Charset charset = StandardCharsets.UTF_16;
    		String content, find, replace;
     
    		// Initialize
    		path = Paths.get(goodXMLPath);
     
    		try
    		{
    			content = new String(Files.readAllBytes(path), charset);
    			logger.debug("BEFORE: " + content);
    			for (Definition d : definitionsToReplace)
    			{
    				find = d.getGUID().replace("\"", "");
    				replace = d.getKeyGUID().replace("\"", "");
     
    				if (content.contains(find) || content.indexOf(find) != -1)
    					content = content.replace(find, replace);
    				else
    					logger.debug("Didn't find string: " + find);
     
    			}
    			logger.debug("AFTER: " + content);
    			Files.write(path, content.getBytes(charset));
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,159
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Need help performing a find and replace on an XML file using Java

    I recommend creating an SSCCE by hardcoding an xml String and the text to replace, that way we can run it and see exactly what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help performing a find and replace on an XML file using Java

    I tried to do what you recommended but it didn't produce the same results when I hardcoded the xml in my code. I'm trying to replace this "8145c4b4-e5f1-473d-b720-e0bdac23a914" with something like "e0f6ab53-2936-4c2b-86bd-f2bc6dfd165d" do you think the series of characters combined with the xml tags/special characters can be causing the problem with the replace?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,159
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Need help performing a find and replace on an XML file using Java

    Quote Originally Posted by atignola View Post
    do you think the series of characters combined with the xml tags/special characters can be causing the problem with the replace?
    Sorry, but without seeing a real example String and set of replacements that result in the undesired behavior, anything I say would be a complete guess.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help performing a find and replace on an XML file using Java

    Can i send you the actual XML file?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,159
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

    Default Re: Need help performing a find and replace on an XML file using Java

    Your best chance for getting help is to post an SSCCE, that way other people can help (and get help for similar problems).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Need help performing a find and replace on an XML file using Java

    Quote Originally Posted by atignola View Post
    Can i send you ...
    Post all relevant code/comments/questions on the forum for all to see for the best chance of assistance

Similar Threads

  1. JAVA = Replace a line in a text file
    By JGW in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 16th, 2013, 04:46 AM
  2. java: performing modification in file.
    By basha2013 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 8th, 2013, 10:41 PM
  3. Loop to find and replace multiple instances of a char
    By Olympaphibian89 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 25th, 2012, 04:11 PM
  4. convert excel to xml and read the input from xml file
    By rahulruns in forum Object Oriented Programming
    Replies: 5
    Last Post: April 3rd, 2012, 11:13 AM
  5. Reading XML File using DOMParser and have problem with accessing xml
    By optiMystic23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2012, 01:22 PM