2011-12-13 4 views
5

나는 다음과 같은 HTML요소를 바꾸려면 어떻게해야합니까?

<html> 
<head> 
    <title>test</title> 

</head> 
<body> 
    <table> 
    <caption>table title and/or explanatory text</caption> 
    <thead> 
    <tr> 
    <th>header</th> 
</tr> 
</thead> 
<tbody> 
     <tr> 
     <td id=\"test\" width=\"272\"></td> 
</tr> 
</tbody> 
</table> 
<a href=\"http://www.google.fi\" style=\"color:black\">Test link</a> 
<a href=\"http://www.google.fi\"><img src=\"http://www.google.se/images/nav_logo95.png\" /></a>" 
</body> 
</html>; 

을 그리고 나는 outerHTML에를 jsoup와 첫 번째 링크를 찾아

Element elem = page.select("a[href=" + link.getUrl() + "]:contains(" + link.getName() + ")").first(); 

가 난 단지 elem.html("foo")와 내부 HTML을 대체 할 수있는 텍스트로 교체하거나 인쇄 할 elem.outerHtml()

아무도 내가 이것을 달성 할 수있는 방법을 알고 있습니까?

답변

13

답변을 찾았습니다!

TextNode text = new TextNode("foo", ""); 
elem.replaceWith(text); 
0

당신은 당신이 작업 할 요소를 발견하면, 당신은 여기에 설명 된대로 명령을 적용 할 수 있습니다 : http://jsoup.org/cookbook/modifying-data/set-html

내가 바로 가져올 수 없습니다.

elemento.prepend("<a href='www.test.com'>");  
elemento.html("Roberto C. Santos.");     
elemento.append("</a>"); 
elemento.wrap("<a href='www.test.com'> </a>"); 

하지만이 무엇입니까 : 나는 이것을 시도하고

<td><a href="www.test.com"> <a style="" target="_self" title="" href="http://ivv.veveivv.vvzenes.com.br/mao/ara/ccacao" data-mce-href="resolveuid/5cc1c7c8c9efcacaaeedec22a9c69a54" class="internal-link">Roberto C. Santos.</a></a></td> 
    </tr> 

나는 여전히 URL 요소의 내용을 교환하는 정확한 방법을 알고하지는.

더 낫다고는 결과로, 갖고 싶은 :

<a href='www.test.com'> Roberto C. Santos.</a>" 

어떻게 inbetween입니다 href를 삭제 coul'd?

+0

내가 옳지 않은 일은 잘못된 요소를 사용하는 것이 었습니다. 지금까지 올바른 요소를 선택하는 가장 좋은 방법은 모든 요소를 ​​반복하는 것입니다. 중첩 된 루프는 여전히 정확한 루프를 찾지 못해 도움이됩니다. 예를 들면 다음과 같습니다. for (Element elem : contElems) { if (elem.id()! = "") System.out.println (elem.id()); \t 요소 contElems1 = contElems.select ("href, a [href], a [data-href], 링크 [href], a [title]"); (요소 elem1 : contElems1)에 대한 는 // (및/또는) { // 여기 코드를 삽입} I 위의 주석에 줄 바꿈을 넣어하는 방법을 알게하고 있지 않다 여기 } –

+0

코드를 넣어 ... –

관련 문제