其值一起更新标签名 [英] Update a tag name along with its value

查看:244
本文介绍了其值一起更新标签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用更新的值来代替HTML标签。使用JSOUP我曾经试过,但不能工作了没有办法。

I am trying to replace html tags with updated values. I had tried using JSOUP but could not work out a way yet.

的功能

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;
        }

数组[我] =阵列[I] .wrap(&LT; A HREF ='+网址+'类='最喜欢的'&GT;&LT; / A&gt;中); 基本上与包装新值存在的标签。但我不希望这种情况发生。我想要的东西,如完全取代标签:

This array[i] = array[i].wrap("<a href='"+url+"' class='favourite'></a>"); is basically wrapping the existing tags with the new value. But I do not want that to happen. I want to replace the tags completely with something like:

"<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 尝试,但没有成功。你仍然可以找到它上面提供的源$ C ​​$ C注释掉。 请告诉我,我要去哪里错了?我应该怎么做来更新标签?

I have tried using replaceWith but was unsuccessful. You can still find it commented out in the source code provided above. Please tell me where am I going wrong? What should I do to update the tags?

P.S:输入可能与一些或多或少的标签变量。

推荐答案

您可以使用类的 replaceWith 方法元素。我已清除code一点点。删除了阵列和使用提供的名单尽可能。而且你不需要正则表达式来获得的href 属性(或者任何其他属性为此事),当你已经解析的HTML。检查出来,如果​​你需要进一步的协助通知我。

You can use the replaceWith method of class Element. I've cleared your code a little bit. Removed the arrays and used the provided lists wherever possible. Moreover you don't need regex to get the href attribute (or any other attribute for that matter) when you've already parsed the html. Check it out and inform me if you need further assistance.

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>

输出

<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>

这篇关于其值一起更新标签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆