scrapeableFile

Changes to scrapeableFile

scrapeableFile.applyXPathExpression

void scrapeableFile.applyXPathExpression ( String xPathExpression ) (professional and enterprise editions only)

Description

Applies an XPath expression to the current HTML response. If tidying the response failed this method will also fail.

Parameters

  • xPathExpression A valid XPath expression.

Return Values

An XmlNode. See example for usage.

Change Log

Version Description
6.0.1a Available in Professional and Enterprise editions.

Examples

Apply an XPath Expression

outputNode( node, indent )
{
        // e.g., in the case of a <td> tag the getName call
        // will return the text "td".
        if( node.getName()!=null )
        {  
          openTag = new StringBuffer();
          openTag.append( indent + " <" + node.getName() );
          // The getAttributes method returns an ArrayList of
          // KeyValue objects.
          for( iter = node.getAttributes().iterator(); iter.hasNext(); )
          {
                attribute = iter.next();
            openTag.append( " " + attribute.getKey() + "=\"" + attribute.getValue() + "\"" );
          }
          openTag.append( ">" );
          session.log( openTag.toString() );
        }
               
        // e.g., in the case of <td>foo</td> the getValue method
        // call will return the text "foo".
        if( node.getValue()!=null )
        {
          session.log( indent + " " + node.getValue() );
        }
       
        // getChildNodes returns an ArrayList of XmlNode objects.
        for( iter = node.getChildNodes().iterator(); iter.hasNext(); )
        {
                childNode = iter.next();
                outputNode( childNode, indent + "--" );
        }
           
        if( node.getName()!=null )
        {
          session.log( indent + " </" + node.getName() + ">" );
        }
}

// Match all <td> tags.
node = scrapeableFile.applyXPathExpression( "//td" );

// Note that there is an equivalent sutil
// method call: sutil.applyXPathExpression( String content, String expression )
// If the content parameter doesn't contain a well-formed
// block of XML an exception will be thrown.

outputNode( node, "" );