Code example for DocumentBuilder

Methods: parse, setEntityResolver, setErrorHandler

1
              } 
          } 
        InputSource input = new InputSource(in);
        input.setSystemId(systemId);
        DocumentBuilder builder = getDocumentBuilder();
        node = builder.parse(input);
        return new DOMSource(node, systemId);
      } 
    catch (IOException e)
      { 
        throw new TransformerException(e);
      } 
    catch (SAXException e)
      { 
        throw new TransformerException(e);
      } 
  } 
 
  URL resolveURL(String systemId, String base, String href)
    throws IOException 
  { 
    URL url = null;
    try 
      { 
        if (systemId != null)
          { 
            try 
              { 
                url = new URL(systemId);
              } 
            catch (MalformedURLException e)
              { 
                // Try building from base + href 
              } 
          } 
        if (url == null)
          { 
            if (base != null)
              { 
                URL baseURL = new URL(base);
                url = new URL(baseURL, href);
              } 
            else if (href != null)
              { 
                url = new URL(href);
              } 
            else 
              { 
                // See below 
                throw new MalformedURLException(systemId);
              } 
          } 
        return url;
      } 
    catch (MalformedURLException e)
      { 
        // Fall back to local filesystem 
        File file = null;
        if (href == null)
          { 
            href = systemId;
          } 
        if (base != null)
          { 
            int lsi = base.lastIndexOf(File.separatorChar);
            if (lsi != -1 && lsi < base.length() - 1)
              { 
                base = base.substring(0, lsi);
              } 
            File baseFile = new File(base);
            file = new File(baseFile, href);
          } 
        else if (href != null)
          { 
            file = new File(href);
          } 
        return (file == null) ? null : file.toURL();
      } 
  } 
 
  DocumentBuilder getDocumentBuilder()
    throws TransformerException 
  { 
    try 
      { 
        if (builder == null)
          { 
            DocumentBuilderFactory factory =
              DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setExpandEntityReferences(true);
            builder = factory.newDocumentBuilder();
          } 
        if (userResolver != null)
          { 
            builder.setEntityResolver(new URIResolverEntityResolver(userResolver));
          } 
        if (userListener != null)
          { 
            builder.setErrorHandler(new ErrorListenerErrorHandler(userListener));
          } 
        return builder;
      } 
    catch (Exception e)
      {