HTML结构变化的输入 [英] HTML structure varying on input
问题描述
我有一个包含动态HTML的字符串。该HTML可以包含静态图像,地图,文本,链接,等等。你可以看看此链接。
I have a string which contains dynamic HTML. The HTML can contain static image, maps, texts, links, etc. You can take a look at this link.
在回答这个问题时我有文本和链接的工作( A HREF
)。但是,如果HTML包含图像或地图,它的故障和HTML是没有得到产生预期。
The answer to this question is working when I am having text and links (a href
). But, if the html contains images or maps, its malfunctioning and the html is not getting generated as expected.
这是我创造来完成这项工作的方法是:
The methods which I have created to do the job are:
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>", "");
// Logic to add center tag before image
// Document doc = Jsoup.parse(webText);
// Elements imgs = doc.select("img");
// for (Element img : imgs) {
// img.attr("src", "images/" + img.attr("src")); // or whatever
// }
//
// doc.outerHtml(); // returns the modified HTML
//Determine link and favourite types to add favourite a class around it.
// 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) {
// wrapping inner href with a tag attributes
Elements link = doc.select("a");
String linkHref = link.attr("href");
Log.e("linkHref",linkHref);
array[i] = array[i].wrap("<a class=" + favourite + " href='"+linkHref+"'></a>");
}
}
// Log.e("From doc.body html *************** ", " " + doc.body());
Element element = doc.body();
Log.e("From element html *************** ", " " + element.html());
//changes to update html ahref
String currentHtml = element.html();
String newHtml = currentHtml.substring(0,currentHtml.indexOf("<a href")+1)+currentHtml.substring(currentHtml.indexOf("font"),currentHtml.indexOf("</a>"))+currentHtml.substring(currentHtml.indexOf("</a>")+4,currentHtml.length());
listOfElements = newHtml;
//refactoring html
listOfElements = wrapImgWithCenter(listOfElements);
//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("&", "&");
} 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("&", "&");
}
} else {
// standard photo
Element ele = doc.body();
ele.select("img").get(i);
listOfElements = ele.html();
}
}
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);
}
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){
finish();
//Conflict was here from html merge.
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>");
}
Log.e("Wrapping", doc.html());
return doc.html();
}
您必须阅读问题中的链接,了解输入和输出。
You have to read the question in the link to understand the input and the output.
其他的输出图像和链接,供大家参考:
Other output with image and links for your reference:
<html>
<head></head>
<body>
<p dir="ltr">
<center>
<img src="http://files.parsetfss.com/bcff7108-cbce-4ab8-b5d1-1f82827e6519/tfss-9fca384a-2f7b-4632-a585-65c78f40842a-file" />
</center><br /> <a href="LixWQfueLU"><font color="#009a49">Rohit Lalwani</font></a><br /> <a href="45.5033204,-99.8865083">
<center>
<img src="http://maps.google.com/maps/api/staticmap?center=45.5033204,-99.8865083&zoom=15&size=960x540&sensor=false&markers=color:blue%7Clabel:!%7C45.5033204,-99.8865083" />
</center></a><br /> </p>
<br />
<br />
</body>
</html>
在那里,你可以看到类=最爱
在的href
标记丢失。这是我需要纠正。请建议我该怎么做。
There you can see that the class="favourite"
in the href
tag is missing. This is what I need to rectify. Please suggest me what to do.
推荐答案
读你原来的问题我看就可以达到你想要的是这样的:
Reading your original question I see that you can achieve what you want this way:
- 您有一个锚(
a.favorite
) - 您必须选择自己的孙子(
字体
在这种特殊情况下,但它可能是一个IMG
或什么) - 您删除原始锚的孩子
- 然后追加孙子作为一个新的孩子!
- You have an anchor (
a.favorite
) - You have to pick his grandchild (
font
in this particular case, but it could be animg
or whatever) - You delete the children of the original anchor
- and then you append the grandchildren as a new child!.
这听起来很复杂,但它是很容易的,在这里你是一个code例如:
This may sound complicated but it is very easy, here you are a code example:
String html ="<a class=\"favourite\" href=\"LixWQfueLU\"><a href=\"LixWQfueLU\"><font color=\"#009a49\">Rohit Lalwani</font></a></a>";
Document doc = Jsoup.parse(html);
//The original anchor
Element afav = doc.select(".favourite").first();
//The grandchild
Element select = doc.select("font").first();
afav.remove();
afav.appendChild(select);
System.out.println(afav);
输出:
<a class="favourite" href="LixWQfueLU"><font color="#009a49">Rohit Lalwani</font></a>
希望它能帮助!
Hope it helps!
这篇关于HTML结构变化的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!