java - JSoup "wrap"每次都没有按预期工作

标签 java android image jsoup

我有一个 HTML 字符串,其中包含文本、图像或 map 图像。 HTML 是动态生成的。现在,只要有<img>标签,它应该被 <center> 包裹。标签。为了实现这一目标,我使用 JSoup 并成功地将其应用于静态图像和文本。

但是,每当我尝试发布 map 时,HTML 就会失去其结构。我不明白发生了什么事。法线图像和 map 图像都有 <img>标签。该方法如何给出不同的输出?

这是完成这项工作的方法:

public String wrapImgWithCenter(String html){
    Document doc = Jsoup.parse(html);
    doc.select("img").wrap("<center></center>");
    return doc.html();
}

原始 HTML,包含图像和包装前的 map 图像:

<p dir="ltr"><img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-4de5af73-68f1-401d-9feb-1dfde1373cff-file" /></p> 
<p dir="ltr"><a href="15.2993265,74.123996"><img src="http://maps.google.com/maps/api/staticmap?center=15.2993265,74.123996&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C15.2993265,74.123996" /></a><br /><br /></p> 
<p dir="ltr"><a href="22.572646,88.363895,-25.274398,133.775136"><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=2&size=960x540&markers=22.572646,88.363895%7C-25.274398,133.775136&path=color:0xff0000ff%7Cweight:5%7C22.572646,88.363895%7C-25.274398,133.775136&sensor=false" /></a><br /> </p>

包装后的结果

<p dir="ltr"> </p>
<center> 
 <img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-01516245-c773-4765-b542-ebecb964b255-file" /> 
</center>
<br />
<br />
<p></p> 
<p dir="ltr"> </p>
<center> 
 <a href="15.2993265,74.123996"><img src="http://maps.google.com/maps/api/staticmap?center=15.2993265,74.123996&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C15.2993265,74.123996" /></a> 
</center>
<p></p> 
<p dir="ltr"> </p>
<center> 
 <a href="22.572646,88.363895,-25.274398,133.775136"><img src="http://maps.google.com/maps/api/staticmap?center=22.572646,88.363895&zoom=2&size=960x540&markers=22.572646,88.363895%7C-25.274398,133.775136&path=color:0xff0000ff%7Cweight:5%7C22.572646,88.363895%7C-25.274398,133.775136&sensor=false" /></a> 
</center>
<br /> 
<p></p>

为了比较,

仅包含图像的有效输出:

<html>
 <head></head>
 <body>
  <p dir="ltr">
   <center>
    <img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-959467a6-f83f-44c6-b6fc-88ba4f49d900-file" />
   </center><br /></p> 
  <p dir="ltr">
   <center>
    <img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-46c38c96-c3b5-402e-a0b4-03209adf5203-file" />
   </center><br /></p> 
  <p dir="ltr">
   <center>
    <img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-626ec909-c65e-452c-a341-61a361584eba-file" />
   </center><br /> </p> 
 </body>
</html>

包含文本和图像的有效输出:

<html>
 <head></head>
 <body>
  <p dir="ltr">text </p> 
  <p dir="ltr">
   <center>
    <img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-11343a01-7cd2-4f9e-9f9a-025ec3feb828-file" />
   </center><br /> </p> 
 </body>
</html>

++++++++++++++++++++++++++++++++++++++

类中负责上述功能的方法:

private void createHtmlWeb(){

        String listOfElements = "null"; // normally found if
                                        // webTextcontains.maps.google.com
        Toast.makeText(getApplicationContext(), "" + mainEditText.getHeight(), Toast.LENGTH_SHORT).show();
        ParseObject postObject = new ParseObject("Post");
        Spannable s = mainEditText.getText();
        String webText = Html.toHtml(s);
        webText = webText.replaceAll("(</?(?:b|i|u)>)\\1+", "$1").replaceAll("</(b|i|u)><\\1>", "");
        // refactoring html
        webText = wrapImgWithCenter(webText);
        // Determine link and favourite types to add favourite a class around
        // it.
        if (webText.contains("a href")) {
            String favourite = "favourite";
            // 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++) {
                // we don't want to wrap link types. Common part links have is
                // http. Should update for somethng more secure.
                if (array[i].toString().contains("http") == false) {
                    array[i] = array[i].wrap("<a class=" + favourite + "></a>");
                }

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

        // First need to do a check of the code if iti s a google maps image
        if (webText.contains("maps.google.com")) {
            Document doc = Jsoup.parse(webText); // Parse it into jsoup

            for (int i = 0; i < doc.select("img").size(); i++) {
                if (doc.select("img").get(i).toString().contains("maps.google.com")) {
                    // Get all numbers + full stops + get all numbers
                    Pattern noImage = Pattern.compile("(\\-?\\d+(\\.\\d+)?),(\\-?\\d+(\\.\\d+))+%7C(\\-?\\d+(\\.\\d+)?),(\\-?\\d+(\\.\\d+))");
                    // Gets the URL SRC basically.. almost.. lets try it
                    Matcher matcherer = noImage.matcher(doc.select("img").get(i).toString());

                    // Have two options - multi route or single route
                    if (matcherer.find() == true) {
                        for (int j = 0; j < matcherer.groupCount(); j++) {
                            latitude_to = Double.parseDouble(matcherer.group(1));
                            longitude_to = Double.parseDouble(matcherer.group(3));
                            latitude_from = Double.parseDouble(matcherer.group(5));
                            longitude_from = Double.parseDouble(matcherer.group(7));
                        }

                        String coOrds = "" + latitude_to + "," + longitude_to + "," + latitude_from + "," + longitude_from;
                        Element ele = doc.body();
                        ele.select("img").get(i).wrap("<a href=" + coOrds + "></a>");
                        listOfElements = ele.html();
                        listOfElements = listOfElements.replace("&amp;", "&");

                    } else if (matcherer.find() == false) {
                        noImage = Pattern.compile("(\\-?\\d+(\\.\\d+)?),\\s*(\\-?\\d+(\\.\\d+)?)");
                        matcherer = noImage.matcher(doc.select("img").get(i).toString());

                        Toast.makeText(getApplicationContext(), "Regex Count:" + matcherer.groupCount(), Toast.LENGTH_LONG).show();
                        if (matcherer.find()) {
                            for (int j = 0; j < matcherer.groupCount(); j++) {
                                latitude = Double.parseDouble(matcherer.group(1));
                                parseGeoPoint.setLatitude(latitude);
                                longitude = Double.parseDouble(matcherer.group(3));
                                parseGeoPoint.setLongitude(longitude);
                            }
                        }

                        String coOrds = "" + latitude + "," + longitude;

                        Element ele = doc.body();
                        ele.select("img").get(i).wrap("<a href=" + coOrds + "></a>");
                        listOfElements = ele.html();
                        listOfElements = listOfElements.replace("&amp;", "&");

                    }

                } else {
                    // standard photo
                    Element ele = doc.body();
                    ele.select("img").get(i);
                    listOfElements = ele.html();

                }

            }
            // Put new value in htmlContent
            postObject.put("htmlContent", listOfElements);

        } else {
            postObject.put("htmlContent", webText);
        }

        mainEditText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout(){
                // TODO Auto-generated method stub
                Rect r = new Rect();
                mainEditText.getWindowVisibleDisplayFrame(r);

                // int screenHeight = mainEditText.getRootView().getHeight();
                // int heightDifference = screenHeight - (r.bottom - r.top);
            }
        });

        // See if a trip exists
        if (finalTrip != null) {
        }

        // Want to put the location in the location section
        // if parsegeoPoint != null -- old information
        if (latitude != -10000 && longitude != -10000) {
            // Toast.makeText(getApplicationContext(),
            // "Adding in location co-ods: " + latitude + " : " + longitude ,
            // Toast.LENGTH_SHORT).show();
            postObject.put("location", parseGeoPoint);
        }
        postObject.put("type", Post.PostType.HTML.getPostVal());
        postObject.put("user", ParseObject.createWithoutData("_User", user.getObjectId()));

        // Transfer these details
        Intent i = new Intent(getApplicationContext(), WriteStoryAnimation.class);
        i.putExtra("listOfElements", listOfElements);
        i.putExtra("webText", webText);
        i.putExtra("finalTrip", finalTrip);
        i.putExtra("latitude", latitude);
        i.putExtra("longitude", longitude);

        if (mainEditText.length() > 0) {
            startActivity(i);
        } else {
            Toast.makeText(getApplicationContext(), "Your story is empty", Toast.LENGTH_SHORT).show();
        }

        // finish();
        // Toast.makeText(getApplicationContext(), "EditText Sie: " + height +
        // " : " + desiredHeight, Toast.LENGTH_LONG).show();

    }

    // method to refactor html
    public String wrapImgWithCenter(String html){
         Document doc = Jsoup.parse(html);
         //adding center tag before images
            doc.select("img").wrap("<center></center>");
            //adding gap after last p tag
            for (int i =0; i<= 1; i++) {
            doc.select("p").last().after("<br>");
            }

            return doc.html();
    }

最佳答案

我已经解决了这个问题。 Fonkap 的评论是正确的,某些东西正在改变我的输出。我刚刚更改了调用 wrapImgWithCenter() 的位置。

我刚刚更改了 createHtmlWeb() 方法的最后一个,并执行了以下操作:

Log.e("listOfElements", listOfElements);
            //refactoring html
            listOfElements = wrapImgWithCenter(listOfElements);
            // Put new value in htmlContent
            postObject.put("htmlContent", listOfElements);

        } else {
            //refactoring html
            webText = wrapImgWithCenter(webText);
            postObject.put("htmlContent", webText);
        }

现在输出符合要求。

关于java - JSoup "wrap"每次都没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26669012/

相关文章:

java - 适用于 Android 的 SQLite 分组项目和可扩展 ListView

javascript - 如何使用 OnClick 更改图像?

java - Java中对单个输入求和和反转的方法?

java - convertAndSend 与 Send To 注释

java - 在编辑开始时将 JCombobox 中的选定索引设置为 jtable 中的单元格编辑器

android - 没有播放服务的 GoogleCloudMessaging

Java:[全局方法访问]单实例枚举 vs 按需对象构造 vs 静态

java - Http Post 和网络延迟

java - 用于图片文件格式转换的开源 Java/Groovy 库?

java - 为什么我的照片不再显示了