Java HTML / XML How to - Parse an html string for XML tool consume








Question

We would like to know how to parse an html string for XML tool consume.

Answer

import java.io.StringReader;
//w w w.j  av  a2 s .c  om
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

import org.xml.sax.InputSource;

public class Main {

  public static void main(String[] args) throws Exception {
    String docroot = "<div><i>items <b>sold</b></i></div>";
    XPath xxpath = XPathFactory.newInstance().newXPath();
    InputSource inputSource = new InputSource(new StringReader(docroot));
    String result = (String) xxpath.evaluate("//b", inputSource,
        XPathConstants.STRING);
    System.out.println(result);
  }

}