2013-03-28 231 views
0

我有一個HTML文檔,我需要更新IMG標籤的text和src屬性。我在Java工作。我想替換HTML中的以下字符串:DataName,DataText和DataIcon。替換Java中IMG標籤的src屬性

<body> 

<h1 align="center">DataName</h1> 

<div class="tabber"> 

    <div class="tabbertab"> 
     <h2>Info</h2> 
     <p>DataText</p> 
    </div> 


    <div class="tabbertab"> 
     <h2>Pictures</h2> 
     <div id="album"> 
      <ul class="gallery"> 
       <li><a href="#nogo" tabindex="1">1<img src=DataIcon alt="landscape image 1" title="landscape image 1" /></a></li> 
       <li><a href="#nogo" tabindex="1">2<img src="C:\thesis\100GreatP\eclipse_ws\test\data\pictures\1\pyramid2.jpg" alt="landscape image 2" title="landscape image 2" /></a></li> 
      </ul> 
     </div> 
    </div> 

    <div class="tabbertab"> 
     <h2>Video</h2> 

    </div> 

</div> 

雖然我suceeded替換字符串數據名稱和DataText,我還沒有成功通過存儲在數據庫中的字符串我IMAGEURL更換DataIcon。檢查調試說,它只是簡單地不能搜索DataIcon字符串。我使用的HTMLParser,我已經寫了下面的類應用問題:

public class MyNodeVisitor extends NodeVisitor { 
     String name; 
     String text; 
     String icon; 

     public MyNodeVisitor() { 

     } 

     public MyNodeVisitor(String IconPath, String Name, String Text){ 
      this.name = Name; 
      this.text = Text; 
      this.icon = IconPath; 
     } 

     public void visitStringNode (Text string) 
     { 
      if (string.getText().equals("DataName")) { 
       string.setText(name); 
      } 

      else if(string.getText().equals("DataIcon")){ 
        string.setText(icon); 

      } 
      else if (string.getText().equals("DataText")){ 
        string.setText(text); 
      } 
     } 
    } 

類已經在我的應用程序代碼以這樣的方式

 NodeList nl = new NodeList(); 
     String htmlString = null; 
     InputStream contentStream = null; 
     String textString = null;  
     String resultStr = getDatabaseAttribute(name,"DESCRIPTION"); 
     String resultStr2 = getDatabaseAttribute(name,"NAME"); 
     String resultStr3 = getDatabaseAttribute(name,"ICON_path"); 


     try 
     { 
      // Read the URL content into a String using the default encoding (UTF-8). 
     contentStream = WWIO.openFileOrResourceStream(BROWSER_BALLOON, this.getClass()); 
      htmlString = WWIO.readStreamToString(contentStream, null); 

     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      WWIO.closeStream(contentStream, resultStr); 
     } 


     try { 
      Parser parser = new Parser(htmlString); 
      nl = parser.parse(null); 

      nl.visitAllNodesWith(new MyNodeVisitor(resultStr3, resultStr2,resultStr)); 
      nl.toString(); 

     } catch (ParserException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     String output = nl.toHtml(); 

     return output; 

任何人可以幫助我得到了應用?整個問題是它無法在IMG標籤中搜索DataIcon字符串。謝謝你的幫助。

回答

1

您的img標記不是StringNode。您需要覆蓋visitTag(標記標記)方法並處理標記對象。

喜歡的東西(不編譯)

public void visitTag(Tag tag) { 
    if ("img".equals(tag.getTagName())) { 
     if ("DataIcon".equals(tag.getAttribute("src"))) { 
      tag.setAttribute("src", icon); 
     } 
    }   
} 
+0

感謝您的輸入!我已經修改了這個方法,因爲它沒有完全按照你的方式工作,第一個if語句是if(Tag isinstance of ImageTag)。不過,你幫了我很多,謝謝! – MichalB