Change href value in android to local one using JSoup












0















How can I change the value of href in HTML code of some page stored locally in Android?



 private void changeHrefsToLocal(String pageName) throws IOException {

File input = new File(appContext.getFilesDir(), pageName);
Document savedDoc = Jsoup.parse(input, "UTF-8");
Elements links = savedDoc.select("a[href]");

String href;

for(Element link : links){

href = appContext.getFilesDir() + "/" + link.attr("abs:href").replace(INDEX_URL, "") + ".html";
link.attr("href", href);

}
}


Logs:



11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="http://test.pl/kontakt">Kontakt</a>

11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="/data/data/com.pokropek.ernest.protectedofflinewebviewer/files/kontakt.html">Kontakt</a>


If I print the link to the logs before and after the method runs, there's the expected change, although there's no change in the local .html file, which is the main problem.










share|improve this question























  • Welcome. How are you printing those logs? Your code doesn't include the part that you actually save your changes back to the file. Maybe you miss that?

    – Bruno Berisso
    Nov 23 '18 at 18:29











  • Thanks Bruno, that's a really good point. I indeed missed writing those changes to the file. As I understand, the link.attr(...) doesn't impact the file I'm working on, since it is a different document/set of links copied from the document? If that's so, how can I commit those changes to the local file?

    – Ernest Pokropek
    Nov 23 '18 at 18:37













  • My pleasure :) Maybe you should check the docs for JSoup on how to save a Document back to a file? I know nothing about JSoup, sorry

    – Bruno Berisso
    Nov 23 '18 at 18:41
















0















How can I change the value of href in HTML code of some page stored locally in Android?



 private void changeHrefsToLocal(String pageName) throws IOException {

File input = new File(appContext.getFilesDir(), pageName);
Document savedDoc = Jsoup.parse(input, "UTF-8");
Elements links = savedDoc.select("a[href]");

String href;

for(Element link : links){

href = appContext.getFilesDir() + "/" + link.attr("abs:href").replace(INDEX_URL, "") + ".html";
link.attr("href", href);

}
}


Logs:



11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="http://test.pl/kontakt">Kontakt</a>

11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="/data/data/com.pokropek.ernest.protectedofflinewebviewer/files/kontakt.html">Kontakt</a>


If I print the link to the logs before and after the method runs, there's the expected change, although there's no change in the local .html file, which is the main problem.










share|improve this question























  • Welcome. How are you printing those logs? Your code doesn't include the part that you actually save your changes back to the file. Maybe you miss that?

    – Bruno Berisso
    Nov 23 '18 at 18:29











  • Thanks Bruno, that's a really good point. I indeed missed writing those changes to the file. As I understand, the link.attr(...) doesn't impact the file I'm working on, since it is a different document/set of links copied from the document? If that's so, how can I commit those changes to the local file?

    – Ernest Pokropek
    Nov 23 '18 at 18:37













  • My pleasure :) Maybe you should check the docs for JSoup on how to save a Document back to a file? I know nothing about JSoup, sorry

    – Bruno Berisso
    Nov 23 '18 at 18:41














0












0








0








How can I change the value of href in HTML code of some page stored locally in Android?



 private void changeHrefsToLocal(String pageName) throws IOException {

File input = new File(appContext.getFilesDir(), pageName);
Document savedDoc = Jsoup.parse(input, "UTF-8");
Elements links = savedDoc.select("a[href]");

String href;

for(Element link : links){

href = appContext.getFilesDir() + "/" + link.attr("abs:href").replace(INDEX_URL, "") + ".html";
link.attr("href", href);

}
}


Logs:



11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="http://test.pl/kontakt">Kontakt</a>

11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="/data/data/com.pokropek.ernest.protectedofflinewebviewer/files/kontakt.html">Kontakt</a>


If I print the link to the logs before and after the method runs, there's the expected change, although there's no change in the local .html file, which is the main problem.










share|improve this question














How can I change the value of href in HTML code of some page stored locally in Android?



 private void changeHrefsToLocal(String pageName) throws IOException {

File input = new File(appContext.getFilesDir(), pageName);
Document savedDoc = Jsoup.parse(input, "UTF-8");
Elements links = savedDoc.select("a[href]");

String href;

for(Element link : links){

href = appContext.getFilesDir() + "/" + link.attr("abs:href").replace(INDEX_URL, "") + ".html";
link.attr("href", href);

}
}


Logs:



11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="http://test.pl/kontakt">Kontakt</a>

11-23 18:40:33.837 10380-10397/com.pokropek.ernest.protectedofflinewebviewer E/testing: <a href="/data/data/com.pokropek.ernest.protectedofflinewebviewer/files/kontakt.html">Kontakt</a>


If I print the link to the logs before and after the method runs, there's the expected change, although there's no change in the local .html file, which is the main problem.







java android html jsoup






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 17:58









Ernest PokropekErnest Pokropek

11




11













  • Welcome. How are you printing those logs? Your code doesn't include the part that you actually save your changes back to the file. Maybe you miss that?

    – Bruno Berisso
    Nov 23 '18 at 18:29











  • Thanks Bruno, that's a really good point. I indeed missed writing those changes to the file. As I understand, the link.attr(...) doesn't impact the file I'm working on, since it is a different document/set of links copied from the document? If that's so, how can I commit those changes to the local file?

    – Ernest Pokropek
    Nov 23 '18 at 18:37













  • My pleasure :) Maybe you should check the docs for JSoup on how to save a Document back to a file? I know nothing about JSoup, sorry

    – Bruno Berisso
    Nov 23 '18 at 18:41



















  • Welcome. How are you printing those logs? Your code doesn't include the part that you actually save your changes back to the file. Maybe you miss that?

    – Bruno Berisso
    Nov 23 '18 at 18:29











  • Thanks Bruno, that's a really good point. I indeed missed writing those changes to the file. As I understand, the link.attr(...) doesn't impact the file I'm working on, since it is a different document/set of links copied from the document? If that's so, how can I commit those changes to the local file?

    – Ernest Pokropek
    Nov 23 '18 at 18:37













  • My pleasure :) Maybe you should check the docs for JSoup on how to save a Document back to a file? I know nothing about JSoup, sorry

    – Bruno Berisso
    Nov 23 '18 at 18:41

















Welcome. How are you printing those logs? Your code doesn't include the part that you actually save your changes back to the file. Maybe you miss that?

– Bruno Berisso
Nov 23 '18 at 18:29





Welcome. How are you printing those logs? Your code doesn't include the part that you actually save your changes back to the file. Maybe you miss that?

– Bruno Berisso
Nov 23 '18 at 18:29













Thanks Bruno, that's a really good point. I indeed missed writing those changes to the file. As I understand, the link.attr(...) doesn't impact the file I'm working on, since it is a different document/set of links copied from the document? If that's so, how can I commit those changes to the local file?

– Ernest Pokropek
Nov 23 '18 at 18:37







Thanks Bruno, that's a really good point. I indeed missed writing those changes to the file. As I understand, the link.attr(...) doesn't impact the file I'm working on, since it is a different document/set of links copied from the document? If that's so, how can I commit those changes to the local file?

– Ernest Pokropek
Nov 23 '18 at 18:37















My pleasure :) Maybe you should check the docs for JSoup on how to save a Document back to a file? I know nothing about JSoup, sorry

– Bruno Berisso
Nov 23 '18 at 18:41





My pleasure :) Maybe you should check the docs for JSoup on how to save a Document back to a file? I know nothing about JSoup, sorry

– Bruno Berisso
Nov 23 '18 at 18:41












1 Answer
1






active

oldest

votes


















0














Its simple just save the edited html file back.



Here is the sample code



    File input = new File(appContext.getFilesDir(), pageName);
Document savedDoc = null;
try {
savedDoc = Jsoup.parse(input, "UTF-8");
} catch (Exception e) {
Log.e("Error ", e.toString());
}

Elements links = savedDoc.select("a[href]");

for(Element link : links){
link.attr("href", "changed value");//change the value of href attribute here
}

//save the updated/edited html file
PrintWriter writer;
try {
writer = new PrintWriter(input,"UTF-8");
writer.write(savedDoc.html() ) ;
writer.flush();
writer.close();
} catch (Exception e) {
Log.e("Error ", e.toString());
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451156%2fchange-href-value-in-android-to-local-one-using-jsoup%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Its simple just save the edited html file back.



    Here is the sample code



        File input = new File(appContext.getFilesDir(), pageName);
    Document savedDoc = null;
    try {
    savedDoc = Jsoup.parse(input, "UTF-8");
    } catch (Exception e) {
    Log.e("Error ", e.toString());
    }

    Elements links = savedDoc.select("a[href]");

    for(Element link : links){
    link.attr("href", "changed value");//change the value of href attribute here
    }

    //save the updated/edited html file
    PrintWriter writer;
    try {
    writer = new PrintWriter(input,"UTF-8");
    writer.write(savedDoc.html() ) ;
    writer.flush();
    writer.close();
    } catch (Exception e) {
    Log.e("Error ", e.toString());
    }





    share|improve this answer




























      0














      Its simple just save the edited html file back.



      Here is the sample code



          File input = new File(appContext.getFilesDir(), pageName);
      Document savedDoc = null;
      try {
      savedDoc = Jsoup.parse(input, "UTF-8");
      } catch (Exception e) {
      Log.e("Error ", e.toString());
      }

      Elements links = savedDoc.select("a[href]");

      for(Element link : links){
      link.attr("href", "changed value");//change the value of href attribute here
      }

      //save the updated/edited html file
      PrintWriter writer;
      try {
      writer = new PrintWriter(input,"UTF-8");
      writer.write(savedDoc.html() ) ;
      writer.flush();
      writer.close();
      } catch (Exception e) {
      Log.e("Error ", e.toString());
      }





      share|improve this answer


























        0












        0








        0







        Its simple just save the edited html file back.



        Here is the sample code



            File input = new File(appContext.getFilesDir(), pageName);
        Document savedDoc = null;
        try {
        savedDoc = Jsoup.parse(input, "UTF-8");
        } catch (Exception e) {
        Log.e("Error ", e.toString());
        }

        Elements links = savedDoc.select("a[href]");

        for(Element link : links){
        link.attr("href", "changed value");//change the value of href attribute here
        }

        //save the updated/edited html file
        PrintWriter writer;
        try {
        writer = new PrintWriter(input,"UTF-8");
        writer.write(savedDoc.html() ) ;
        writer.flush();
        writer.close();
        } catch (Exception e) {
        Log.e("Error ", e.toString());
        }





        share|improve this answer













        Its simple just save the edited html file back.



        Here is the sample code



            File input = new File(appContext.getFilesDir(), pageName);
        Document savedDoc = null;
        try {
        savedDoc = Jsoup.parse(input, "UTF-8");
        } catch (Exception e) {
        Log.e("Error ", e.toString());
        }

        Elements links = savedDoc.select("a[href]");

        for(Element link : links){
        link.attr("href", "changed value");//change the value of href attribute here
        }

        //save the updated/edited html file
        PrintWriter writer;
        try {
        writer = new PrintWriter(input,"UTF-8");
        writer.write(savedDoc.html() ) ;
        writer.flush();
        writer.close();
        } catch (Exception e) {
        Log.e("Error ", e.toString());
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 4 at 10:08









        WandoWando

        214




        214






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451156%2fchange-href-value-in-android-to-local-one-using-jsoup%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Determine an Integral..