Code example for DocumentBuilder

Methods: parse, setEntityResolver

0
  private static synchronized DocumentBuilder getDocBuilder() throws ParserConfigurationException {
    if (docBuilderFactory == null) {
      initDocBuilderFactory(); 
    } 
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    docBuilder.setEntityResolver(new EntityResolver() {
      public InputSource resolveEntity(String publicId, String systemId) {
        if ("-//Apple Computer//DTD PLIST 1.0//EN".equals(publicId) || // older publicId
            "-//Apple//DTD PLIST 1.0//EN".equals(publicId)) { // newer publicId
          // return a dummy, zero length DTD so we don't have to fetch 
          // it from the network. 
          return new InputSource(new ByteArrayInputStream(new byte[0]));
        } 
        return null; 
      } 
    }); 
    return docBuilder;
  } 
 
  /** 
   * Parses a XML property list file. 
   * 
   * @param f The XML property list file. 
   * @return The root object of the property list. This is usally a NSDictionary but can also be a 
   *         NSArray. 
   * @throws Exception When an error occurs during parsing. 
   * @see javax.xml.parsers.DocumentBuilder#parse(java.io.File) 
   */ 
  public static NSObject parse(File f) throws Exception {
    DocumentBuilder docBuilder = getDocBuilder();
 
    Document doc = docBuilder.parse(f);
 
    return parseDocument(doc);
  } 
 
  /** 
   * Parses a XML property list from a byte array. 
   * 
   * @param bytes The byte array containing the property list's data. 
   * @return The root object of the property list. This is usally a NSDictionary but can also be a 
   *         NSArray. 
   * @throws Exception When an error occurs during parsing. 
   */ 
  public static NSObject parse(final byte[] bytes) throws Exception {
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    return parse(bis);
  } 
 
  /** 
   * Parses a XML property list from an input stream. 
   * 
   * @param is The input stream pointing to the property list's data. 
   * @return The root object of the property list. This is usally a NSDictionary but can also be a 
   *         NSArray. 
   * @throws Exception When an error occurs during parsing. 
   * @see javax.xml.parsers.DocumentBuilder#parse(java.io.InputStream) 
   */ 
  public static NSObject parse(InputStream is) throws Exception {
    DocumentBuilder docBuilder = getDocBuilder();
 
    Document doc = docBuilder.parse(is);
 
    return parseDocument(doc);
  } 
 
  /**