wood burning stoves*
The moose likes Testing and the fly likes How do I detect text wiithin the spantags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "How do I detect text wiithin the spantags" Watch "How do I detect text wiithin the spantags" New topic
Author

How do I detect text wiithin the spantags

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 576
I have the following HTML
<span class="aaa">row is locked</span>

I want to extract that from the response. the following code will print out the response content but will not print out anything between the span tags.

private void printElementsWithAttributes(WebResponse webresponse) throws Exception
{
String [] elementNames = webresponse.getElementNames();
for(int index = 0; index < elementNames.length; index++)
{
HTMLElement [] htmlelements = webresponse.getElementsWithName(elementNames[index]);
for(int jindex = 0; jindex < htmlelements.length; jindex++)
{
System.out.print(htmlelements[jindex].getName()+" | ");
System.out.print(htmlelements[jindex].getTagName()+" | ");
System.out.print(htmlelements[jindex].getText());
}

}
}

Thanks for any help

Tony
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 576
LOL going round in circles on this. I have changed my code and am able to pick up most of the html ecept the text bit.

Given <span class="x10">Data locked</span>

The following code will pick up

Node Name span

Nodes in the Map 1

node name class

node value xq

private void printDocumentModel(WebResponse webresponse) throws Exception
{
Node node = webresponse.getDOM();
printNodeLists(node);

}

private void printNodeLists(Node node) throws Exception
{
NodeList nl = node.getChildNodes();
for(int index= 0; index < nl.getLength(); index++)
{
if(nl.item(index).getNodeName().equals("span"))
{
System.out.println("Node Name "+nl.item(index).getNodeName());
NamedNodeMap nnm = nl.item(index).getAttributes();
System.out.println("Nodes in the Map "+nnm.getLength());
Node childnode = nnm.item(0);
System.out.println(" node name "+childnode.getNodeName());
System.out.println(" node value "+childnode.getNodeValue());

}
printNodeLists(nl.item(index));
}

}

But for the I have no idae how to pick up the text Data locked. There is no method that says right you have the tag here is the value between the tags.

Thanks for any help

Tony
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You need to get the "child nodes" of the span node, which are "text" nodes, and append their "node value" together.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 576
Thanks got it working

Tony
 
GeeCON Prague 2014
 
subject: How do I detect text wiithin the spantags