Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.84 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to do a search/replace on a file on the fly?
  2. private String xmlFile = "D:\mycomputer\extract.xml";
  3. File myXMLFile = new File(xmlFile);
  4.  
  5. // TODO
  6. // REPLACE ALL "<content>" in xmlFile with "<content><![CDATA["
  7. // REPLACE ALL "</content>" with "]]></content>"
  8.  
  9. SAXBuilder builder =  new SAXBuilder("org.apache.xerces.parsers.SAXParser");
  10.  
  11. document = builder.build(new File(myXMLFile));
  12.        
  13. String fileStr = FileUtils.readFileToString(myXMLFile);
  14. fileStr = fileStr.replaceAll("<content>","<content><![CDATA[");
  15. fileStr = fileStr.replaceAll("</content>","]]></content>");
  16. SAXBuilder builder =  new SAXBuilder("org.apache.xerces.parsers.SAXParser");
  17. document = builder.build(new ByteArrayInputStream(fileStr.getBytes()));
  18.        
  19. StringBuilder sb = new StringBuilder ();
  20. loadFIleContent (filePath, sb);
  21. document = builder.build (new StringReader (sb.toString ()));