|
Play Framework/Scala example source code file (XML.java)
The XML.java Play Framework example source code/* * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com> */ package play.libs; import java.io.IOException; import java.io.InputStream; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * XML utilities. */ public class XML { /** * Parse an XML string as DOM. */ public static Document fromString(String xml) { try { return fromInputStream( new ByteArrayInputStream(xml.getBytes("utf-8")), "utf-8" ); } catch(UnsupportedEncodingException e) { throw new RuntimeException(e); } } /** * Parse an InputStream as DOM. */ public static Document fromInputStream(InputStream in, String encoding) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(in); is.setEncoding(encoding); return builder.parse(is); } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } } Other Play Framework source code examplesHere is a short list of links related to this Play Framework XML.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.