2011-12-13 13 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>; 

है और मैं jsoup के साथ पहली लिंक मिल और एक पाठ के साथ बदलने

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

मैं केवल elem.html("foo") के साथ भीतर एचटीएमएल जगह ले सकता है या मुद्रित करने के लिए outerHtml चाहते 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> 

मैं अभी भी एक यूआरएल तत्व की सामग्री का आदान-प्रदान करने के लिए सटीक रास्ता पता नहीं है।

I'd, लेना पसंद परिणाम के रूप में:

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

मैं href inbetween है कि कैसे मिटा coul'd?

+0

जिस चीज को मैं सही नहीं कर रहा था वह गलत तत्व को काम करने के लिए प्राप्त करना था। सही तत्व का चयन करने के लिए अब तक का सबसे अच्छा तरीका, सभी तत्वों पर लूप करना है। एक नेस्टेड लूप मदद करेगा अगर अभी भी सटीक नहीं मिल रहा है। उदाहरण के लिए: (तत्व elem: contElems) { अगर (elem.id()! = "") System.out.println (elem.id()); \t तत्व contElems1 = contElems.select ("href, a [href], एक [डेटा-href], लिंक [href], एक [शीर्षक]"); (तत्व elem1: contElems1) के लिए // (या/और) { // यहाँ कोड रखो} कोड यहाँ } –

+0

डाल मुझे पता है कि कैसे ऊपर टिप्पणी में एक लाइन ब्रेक डाल करने के लिए हो रही है नहीं कर रहा हूँ ... –

संबंधित मुद्दे