0

I am able to parse the HTML file and make the required changes, but I am not able to store it as an output html file, ie, the changes are visible in console but not in the html file. My requirement is to take a html file as an input, edit it and then store the html file with the new changes. So far, all I am able to do is display the changes in the console.

package html_editer;
import java.util.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.*;
import java.io.*;

public class editer
{
static String htmlLocation = "C:\\Desktop\\hello\\";               
static String fileName = "x";
static StringBuilder buildTmpHTML = new StringBuilder();
static StringBuilder buildHTML = new StringBuilder();
static String name = "Ankit";
static String address = "India";
static String phoneNumber = "1234567890";

public static void main(String[] args)
{

    readHTML(htmlLocation, fileName);
    modifyHTML();

    System.out.println(buildHTML.toString());

    buildTmpHTML.setLength(0);
    buildHTML.setLength(0);

    System.exit(0);
}

    public static void readHTML(String directory, String fileName)
{
    try
    {
BufferedReaderbr=newBufferedReader
(newFileReader(directory+fileName+".html"));

        String line;
        while((line = br.readLine()) != null)
        {
            buildTmpHTML.append(line);
        }
        br.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.exit(1);
    }
}


public static void modifyHTML()
{
    String htmld = buildTmpHTML.toString();
    Document doc = Jsoup.parse(htmld);

    final List<TextNode> nodesToChange = new ArrayList<TextNode>();

    NodeTraversor nd  = new NodeTraversor(new NodeVisitor() 
    {
      @Override
      public void tail(Node node, int depth) 
      {
        if (node instanceof TextNode) 
        {
          TextNode textNode = (TextNode) node;
          nodesToChange.add(textNode);
        }
      }

      @Override
      public void head(Node node, int depth) 
      {        
      }
    });

    nd.traverse(doc.body());

    for (TextNode textNode : nodesToChange) 
    {
      Node newNode = buildElementForText(textNode);
      textNode.replaceWith(newNode);
    }

    buildHTML.append(doc.html());
}

private static Node buildElementForText(TextNode textNode) 
  {
    String text = textNode.getWholeText();
    String[] words = text.trim().split(" ");
    Set<String> units = new HashSet<String>();
    for (String word : words) 
        units.add(word);

    String newText = text;
    for (String rpl : units) 
    {
        if(rpl.contains("Name"))
            newText = newText.replaceAll(rpl, "" + rpl + " " + name);
        if(rpl.contains("Address") || rpl.contains("Residence"))
            newText = newText.replaceAll(rpl, "" + rpl + " " + address);
        if(rpl.contains("Phone-Number") || rpl.contains("PhoneNumber"))
            newText = newText.replaceAll(rpl, ""+rpl+" "+phoneNumber);
    }
    return new DataNode(newText, textNode.baseUri());
  }}
Ankit Sahay
  • 41
  • 1
  • 8
  • "*but I am not able to store it as an output html file*" sorry, but which part of your code makes you think that this HTML should be stored in any file? – Pshemo Nov 07 '16 at 21:09
  • I don't know how to do it. I am able to display the changes in console, how do I make the same changes in the file itself? Even if it is not possible to change the original file, I can have a new html file as output as well. Changing the original file is not a must, but having an html file as output with the changes incorporated is a must requirement. – Ankit Sahay Nov 07 '16 at 21:12
  • Sorry, but are you asking "how to write text to file in Java"? There are a lot of questions about it here available, or any tutorial should contain part about it. Do you face any specific problem when you are trying to use solutions described there? – Pshemo Nov 07 '16 at 21:20
  • Yes, all I could find was the same thing, displaying changes in consoles. Can you suggest some post which has what I am looking for? Or excerpt of code which will do the trick? – Ankit Sahay Nov 07 '16 at 21:22
  • It is easy to find it on Stack Overflow using google with `site:stackoverflow.com` as target: https://www.google.pl/search?q=how+to+write+text+to+file+in+java+site:stackoverflow.com. – Pshemo Nov 07 '16 at 21:24
  • Googling for "java modify text file" also gives few interesting articles. – Pshemo Nov 07 '16 at 21:26

0 Answers0