Skip to content

Instantly share code, notes, and snippets.

@skytreader
Last active January 2, 2016 18:48
Show Gist options
  • Save skytreader/8345791 to your computer and use it in GitHub Desktop.
Save skytreader/8345791 to your computer and use it in GitHub Desktop.
Java util snippets
/**
Parse an XML String. It may be worthwhile to cache the DocumentBuilder
object so we do not need to create an object everytime the method is
invoked.
@param xml
@return an org.w3c.dom.Document object
@referece http://stackoverflow.com/a/562207/777225
*/
public static Document loadXMLFromString(String xml) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
return builder.parse(is);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment