Skip to content

XPath is not always the best option

Sometimes as a programmer you get to a point where you have to choose HOW to do your stuff. You have plenty of options. In Java almost everything is done before. So there is no need to it your self. But consider this, would it be worth while to use a library when you could have hacked down the same functionality in about half the time? I am going to consider what to use when you need to retrieve the contents of a XML element.

Skip XPath if you just want the contents of a single predefined HTML element. Especially if you want the pure HTML contained within that element. I have found it very difficult to work with the XPath when I want to extract the contents of a template file. So, I made my own function to retrieve the contents of a body tag as follows:

private static String getBodyContents(String html) {
         int start = html.indexOf("<body");
         if(start>-1){
               start = html.indexOf(">", start);
               if(start>-1){
                      start += 1;
                      int end = html.indexOf("</body>", start);
                      if(end>-1)
                            return html.substring(start, end);
               }
         }
         return html;
}

This simple Java method does not require alot of integration to get up and running. Just copy into your project and change it a little to suit your needs. It is things like this that does not require any license whatsoever…

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*