ManticMoo.COM All Articles Jeff's Articles
Jeffrey P. Bigham

Instantiating a new node with htmlparser

Jeffrey P. Bigham

Related Ads

Below, I show how to create a new span node using the htmlparser interface. A common error is forgetting to set the end tag. One would think that this would be automatic for tags that require it, but, as opposed to other HTML libraries, there might be advantages in htmlparser not forcing users to produce perfect HTML.


TagNode toCreate = new Span();
toCreate.setAttribute("id", "my_span", '"');
NodeList nl = new NodeList();
nl.add(new TextNode("Text in the middle."));
toCreate.setChildren(nl);
toCreate.setEndTag(new Span());

System.out.println(toCreate.toHtml());

This results in the following:


Result: <SPAN id="my_span">Text in the middle.<SPAN>

Jeffrey P. Bigham
ManticMoo.COM All Articles Jeff's Articles