PhpWiki - Searching For Data
Keywords: Searching For Data, PhpWiki

Searching for data is one of the most challenging tasks in a web page due to its seemingly unstructured (or badly structured) form. Complex searches are now possible with the parser in a simple to use API. Here's an example :

We are looking at a page which has the following html:

    <html>
    ...
    <body>
       <table>
         <tr>
           <td><font size="-1">Name:<b><i>John Doe</i></b></font></td>
           ..
         </tr>
         <tr>
           ..
         </tr>
       </table>
    </body>
    </html>

We'd like to extract the information corresponding to the field "Name". This is possible if we make use of the fact that the name appears two tags after "Name".

Code to achieve this would look like:

    Node nodes [] = parser.extractAllNodesThatAre(TableTag.class);
    // Get the first table found
    TableTag table = (TableTag)nodes[0];

    // Find the position of Name.
    StringNode [] stringNodes = table.digupStringNode("Name");
    StringNode name = stringNodes[0];

    // We assume that the first node that matched is the one we want. We
    // navigate to its parent, the column tag <td>
    CompositeTag td = name.getParent();

    // From the parent, we shall find out the position of "Name"
    int posOfName = td.findPositionOf(name);

    // Its easy now to navigate to John Doe, as we know it is 3 positions away
    Node expectedName = td.childAt(posOfName + 3);

You can move up the parent tree - e.g. when the data is in seperate columns,

    <html>
    ...
    <body>
       <table>
         <tr>
           <td><font size="-1">Name:</font></td>
           <td><font size="-1">John Doe</font></td>
         </tr>
         <tr>
           ..
         </tr>
       </table>
    </body>
    </html>

We'd like to perform the same search on "Name".

Code to achieve this would look like:

    Node nodes [] = parser.extractAllNodesThatAre(TableTag.class);
    // Get the first table found
    TableTag table = (TableTag)nodes[0];

    // Find the position of Name.
    StringNode [] stringNodes = table.digupStringNode("Name");

    // We assume that the first node that matched is the one we want. We
    // navigate to its parent (column <td>)
    CompositeTag td = stringNodes[0].getParent();

    // Navigate to its parent (row <tr>)
    CompositeTag tr = parentOfName.getParent();

    // From the parent, we shall find out the position of the column
    int columnNo = tr.findPositionOf(td);

    // Its easy now to navigate to John Doe, as we know it is in the next column
    TableColumn nextColumn = (TableColumn)tr.childAt(columnNo+1);

    // The name is the second item in the column tag
    Node expectedName = nextColumn.childAt(1);

| Page Execution took 0.261 seconds