更新标记名称及其值

时间:2014-12-01 07:48:24

标签: java android html jsoup replacewith

我正在尝试用更新的值替换html标记。我曾尝试过使用JSOUP,但还没办法解决问题。

功能

if (webText.contains("a href")) {
            // Parse it into jsoup
                        Document doc = Jsoup.parse(webText);
                        // Create an array to tackle every type individually as wrap can
                        // affect whole body types otherwises.
                        Element[] array = new Element[doc.select("a").size()];

                        for (int i = 0; i < doc.select("a").size(); i++) {
                            if (doc.select("a").get(i) != null) {
                                array[i] = doc.select("a").get(i);
                            }
                        }

                        for (int i = 0; i < array.length; i++) {
                            if (array[i].toString().contains("http")) {
                                Log.e("Link", array[i].toString());
                                Pattern p = Pattern.compile("href=\"(.*?)\"");
                                Matcher m = p.matcher(array[i].toString());
                                String url = null;
                                if (m.find()) {
                                    url = m.group(1); // this variable should contain the link URL
                                    Log.e("Link Value", url);
                                    array[i] = array[i].wrap("<a href='"+url+"' class='link'></a>");
                                }
                            }
                            else {
                                Log.e("Favourite", array[i].toString());
                                Pattern p = Pattern.compile("href=\"(.*?)\"");
                                Matcher m = p.matcher(array[i].toString());
                                String url = null;
                                if (m.find()) {
                                    url = m.group(1); // this variable should contain the link URL
                                    Log.e("Favourite Value", url);
                                    array[i] = array[i].wrap("<a href='"+url+"' class='favourite'></a>");
                                    //array[i] = array[i].replaceWithreplaceWith("","");
                                }
                            }

                        }

                        Element element = doc.body();
                        Log.e("From element html *************** ", " " + element.html());
                        String currentHtml = wrapImgWithCenter(element.html());
                        Log.e("currentHtml", currentHtml);
                        listOfElements = currentHtml;
        }

这个array[i] = array[i].wrap("<a href='"+url+"' class='favourite'></a>");基本上用新值包装现有标签。但我不希望这种情况发生。我想用以下内容完全替换标签:

"<a href='"+url+"' class='favourite'>+url+"</a>";

输入:

<html>
 <head></head>
 <body>
  <p dir="ltr"><a href="gYWMBi5XqN" class="favourite"></a><a href="gYWMBi5XqN"><font color="#009a49">Frank Frank</font></a> <a href="http://yahoo.co.in" class="link"></a><a href="http://yahoo.co.in"><font color="#0033cc">http://yahoo.co.in</font></a></p>
  <br />
  <br />
 </body>
</html>

预期输出:

<html>
 <head></head>
 <body>
  <p dir="ltr"><a href="gYWMBi5XqN" class="favourite"><font color="#009a49">Frank Frank</font></a> <a href="http://yahoo.co.in" class="link"><font color="#0033cc">http://yahoo.co.in</font></a></p>
  <br />
  <br />
 </body>
</html>

我尝试使用replaceWith但未成功。您仍然可以在上面提供的源代码中找到它。 请告诉我哪里出错了?我该怎么做才能更新标签?

P.S。:输入可能是变量,带有一些或多或少的标签。

1 个答案:

答案 0 :(得分:0)

您可以使用课程replaceWith的{​​{1}}方法。我已经清除了你的代码了一下。删除了阵列并尽可能使用提供的列表。此外,当您已经解析了html时,您不需要正则表达式来获取Element属性(或任何其他属性)。如果您需要进一步的帮助,请查看并通知我。

href

<强>输入

import org.jsoup.Jsoup;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

public class Main {

    public static void main(String[] args) throws Exception {

        String webText = 
                "<html>" + 
                        "<head></head>" + 
                        "<body>" +
                            "<p dir=\"ltr\">" +
                                "<a href=\"gYWMBi5XqN\" class=\"favourite\"></a>" +
                                "<a href=\"gYWMBi5XqN\"><font color=\"#009a49\">Frank Frank</font></a>" +
                                "<a href=\"http://yahoo.co.in\" class=\"link\"></a>" +
                                "<a href=\"http://yahoo.co.in\"><font color=\"#0033cc\">http://yahoo.co.in</font></a>" + 
                            "</p>" + 
                        "</body>" + 
                    "</html>";

        if (webText.contains("a href")) {
            // Parse it into jsoup
            Document doc = Jsoup.parse(webText);

            Elements links = doc.select("a");

            for (Element link : links) {
                if (link.attr("href").contains("http")) {
                    System.out.println("Link: " + link.toString());
                    String url = link.attr("href");
                    if (url != null) {
                        System.out.println("Link Value: " + url);
                        Attributes attributes = new Attributes();
                        attributes.put("href", url);
                        attributes.put("class", "link");
                        link.replaceWith(new Element(Tag.valueOf("a"), "", attributes).insertChildren(0, link.childNodes()));       
                    }
                } else {
                    System.out.println("Favourite: " + link.toString());
                    String url = link.attr("href");
                    if (url != null) {
                        System.out.println("Favourite Value: " + url);
                        Attributes attributes = new Attributes();
                        attributes.put("href", url);
                        attributes.put("class", "favourite");
                        link.replaceWith(new Element(Tag.valueOf("a"), "", attributes).insertChildren(0, link.childNodes()));      
                    }
                }
            }

            Element element = doc.body();
            System.out.println("From element html *************** "+ element.html());
        }
    }
}

<强>输出

<p dir="ltr">
    <a href="gYWMBi5XqN" class="favourite"></a>
    <a href="gYWMBi5XqN"><font color="#009a49">Frank Frank</font></a> 
    <a href="http://yahoo.co.in" class="link"></a>
    <a href="http://yahoo.co.in"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>

<强>输入

<p dir="ltr">
    <a href="gYWMBi5XqN" class="favourite"></a>
    <a href="gYWMBi5XqN" class="favourite"><font color="#009a49">Frank Frank</font></a> 
    <a href="http://yahoo.co.in" class="link"></a>
    <a href="http://yahoo.co.in" class="link"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>

<强>输出

<p dir="ltr">
    <a href="gYWMBi5XqN"><font color="#009a49">Frank Frank</font></a> 
    <a href="http://yahoo.co.in"><font color="#0033cc">http://yahoo.co.in</font></a>
</p>
相关问题