Code example for InputSource

Methods: setCharacterStream

0
  public Document XMLfromString(String xml){
    Document doc = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    try { 
      DocumentBuilder db = dbf.newDocumentBuilder();
      InputSource is = new InputSource();
      is.setCharacterStream(new StringReader(xml));
      doc = db.parse(is);
 
    } catch (ParserConfigurationException e) {
      System.out.println("XML parse error: " + e.getMessage());
      return null; 
    } catch (SAXException e) {
      System.out.println("Wrong XML file structure: " + e.getMessage());
      return null; 
    } catch (IOException e) {
      System.out.println("I/O exeption: " + e.getMessage());
      return null; 
    } 
    return doc;
  }