Question

I have a HTML document in which I need to update both text and src attribute of IMG tag. I am working in Java. I want to replace following strings in the HTML: DataName, DataText and DataIcon.

<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>

While I have suceeded to replace strings DataName and DataText, I havent succeed to replace DataIcon by my imageURL stored in the database as String. Checking the debug says that it just simply fails to search for DataIcon string. I am using HTMLparser and I have written following class to apply the problem:

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);
            }
        }
    }

The class has been applied in my application code in such way

        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;  

Can anybody help me? The whole problem is that it fails to search for DataIcon string in IMG tag. Thanks for your help.

Était-ce utile?

La solution

Your img tag isn't an StringNode. You need to override the visitTag(Tag tag) method and work on the Tag object.

Something like (not compiled)

public void visitTag(Tag tag) {
    if ("img".equals(tag.getTagName())) {
        if ("DataIcon".equals(tag.getAttribute("src"))) {
            tag.setAttribute("src", icon);
        }
    }        
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top