Menu

How to extract tags from <A>

Help
2002-03-15
2002-03-16
  • Gordon deudney

    Gordon deudney - 2002-03-15

    I was wondering how to extract an image tag and img properties from within an anchor tag?

    I know HTMLlinkTag has a linkData() which returns an enumeration, I have tried to convert that to a tag
    but I have had no luck. 
    example of what I am trying to parse

    <a href="test"><img src="test"> </a>

    I want to get the image tag info.

    I appreciate any help.

     
    • Somik Raha

      Somik Raha - 2002-03-16

      Hi Gordon
         Here's how you can do it - after you recieve the HTMLTag, get its linkData() enumeration, and then downcast to HTMLImageTag.
         e.g.
      HTMLParser parser = new HTMLParser("http://www.someurl.com");

      HTMLNode node,node2;
      HTMLLinkTag linkTag;
      HTMLImageTag imageTag;
      for (Enumeration e= parser.elements();e.hasMoreElements();) {
          node = (HTMLNode)e.nextElement();
          if (node instanceof HTMLLinkTag) {
              linkTag = (HTMLLinkTag)node;
              for (Enumeration e2=linkTag.linkData();e2.hasMoreElements();) {
                   node2 = (HTMLNode)e2.nextElement();
                   if (node2 instanceof HTMLImageNode) {
                      imageTag = (HTMLImageTag)node2;
                      System.out.println("Image loc = "+imageTag.getImageLocation());
                   }
              }
          }
      }

       
      • Somik Raha

        Somik Raha - 2002-03-16

        Hmm... code formatting was lost - let me try again
        <pre>
        HTMLNode node,node2;
        HTMLLinkTag linkTag;
        HTMLImageTag imageTag;
        for (Enumeration e= parser.elements();e.hasMoreElements();) {
           node = (HTMLNode)e.nextElement();
           if (node instanceof HTMLLinkTag) {
              linkTag = (HTMLLinkTag)node;
              for (Enumeration e2=linkTag.linkData();e2.hasMoreElements();) {
                 node2 = (HTMLNode)e2.nextElement();
                 if (node2 instanceof HTMLImageNode) {
                    imageTag = (HTMLImageTag)node2;
                    System.out.println("Image loc ="+imageTag.getImageLocation());
                 }
               }
           }
        }
        </pre>

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.