Extract single quoted html attributes using xpath

95 Views Asked by At

I want to extract the values of single quoted html attributes using Xpath. I have used JTidy to clean the html doc and my code looks like this:

try {
    String data = string.toString();
    InputStream input = new ByteArrayInputStream(data.getBytes());
    Document document = new Tidy().parseDOM(input, null);

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();
    XPathExpression expr = xPath.compile("//a[@class='swatch-2011-link']/@color");

    Object evaluate = expr.evaluate(document, XPathConstants.NODESET);
    NodeList list = (NodeList) evaluate;
    System.out.println(list.getLength());
    for (int i = 0; i < list.getLength(); i++) {
        String name = list.item(i).getNodeValue();
        System.out.println(name);
    }
}
catch (XPathExpressionException e) {
    e.printStackTrace();
}

<a class="swatch-2011-link" 
   style='background:url(somelink); background-size:26px 26px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=http://media.plussizetech.com/womanwithin/zs/0037_19561_zs_2835.jpg, sizingMethod=scale)' mainimageUrl='http://media.plussizetech.com/womanwithin/mc/0037_19561_mc_2835.jpg?wid=271&amp;hei=388&amp;qlt=95&amp;op_sharpen=1' colorName='WILD LIME WHITE'/>
0

There are 0 best solutions below