Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I detect text wiithin the spantags

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to get the "child nodes" of the span node, which are "text" nodes, and append their "node value" together.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks got it working

Tony
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic