Index154

Sequential XML Attribute Replacer

May 14th, 2021 (edited)
688
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package xmlAttributeReplacer;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Scanner;
  10.  
  11. public class AttributeReplacer {
  12.  
  13.     // Global variables
  14.     static String path = "C:\\User\\Documents\\";
  15.     static String filename = "entity names.txt";
  16.     static String filename2 = "entities2.xml";
  17.     static String attribute = "name";
  18.    
  19.     public static void main(String[] args) throws IOException {
  20.        
  21.         // Read the list of values
  22.         // Create array
  23.         List<String> values1 = new ArrayList<String>();
  24.        
  25.         //Start the scanner
  26.         File f = new File(path + "Translated files\\" + filename);
  27.         Scanner s = new Scanner(f);
  28.        
  29.         //Go through the lines and save them into the array
  30.         while(s.hasNext()) {
  31.             String line = s.nextLine();
  32.             values1.add(line);
  33.         }
  34.         s.close();
  35.         String[] values = values1.toArray(new String[0]);
  36.        
  37.         // Read the main file
  38.         // Create string
  39.         String mainFile = "";
  40.        
  41.         //Start the scanner
  42.         File f2 = new File(path + "Raw files\\" + filename2);
  43.         Scanner s2 = new Scanner(f2);
  44.        
  45.         //Go through the lines and add them to the string
  46.         while(s2.hasNext()) {
  47.             String line = s2.nextLine();
  48.             mainFile = mainFile + "\n" + line;
  49.         }
  50.         s2.close();
  51.        
  52.        
  53.         // Replace all ocurrences of the regex pattern in the main file with the elements of the array using substitution so that the
  54.         // same ocurrences aren't matched more than once
  55.         for(int i = 0; i < values.length; i++) {
  56.             values[i] = values[i].replace("$", "\\$");  // This is a fix for dollar signs which otherwise cause problems
  57.             mainFile = mainFile.replaceFirst(attribute + "=\".*?\"", attribute + "!!=\"" + values[i] + "\"");
  58.         }
  59.         // Remove the substitutes
  60.         for(int i = 0; i < values.length; i++) {
  61.             mainFile = mainFile.replaceFirst(attribute + "!!", attribute);
  62.         }
  63.        
  64.         // Save to file in output folder
  65.         // Initiate the FileWriter with the designated file path
  66.         FileWriter f3 = new FileWriter(path + "Output\\" + filename2);
  67.         BufferedWriter bW = new BufferedWriter(f3);
  68.         bW.write(mainFile);
  69.         bW.close();
  70.        
  71.         System.out.println("Done!");
  72.  
  73.     }
  74.  
  75. }
RAW Paste Data