Example #1
0
  public static void main(String[] args) {

    try {

      // Find the implementation
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      factory.setNamespaceAware(true);
      DocumentBuilder builder = factory.newDocumentBuilder();
      DOMImplementation impl = builder.getDOMImplementation();

      // Create the document
      DocumentType svgDOCTYPE =
          impl.createDocumentType(
              "svg",
              "-//W3C//DTD SVG 1.0//EN",
              "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
      Document doc = impl.createDocument("http://www.w3.org/2000/svg", "svg", svgDOCTYPE);

      // Fill the document
      Node rootElement = doc.getDocumentElement();
      ProcessingInstruction xmlstylesheet =
          doc.createProcessingInstruction(
              "xml-stylesheet", "type=\"text/css\" href=\"standard.css\"");
      Comment comment = doc.createComment("An example from Chapter 10 of Processing XML with Java");
      doc.insertBefore(comment, rootElement);
      doc.insertBefore(xmlstylesheet, rootElement);
      Node desc = doc.createElementNS("http://www.w3.org/2000/svg", "desc");
      rootElement.appendChild(desc);
      Text descText = doc.createTextNode("An example from Processing XML with Java");
      desc.appendChild(descText);

      // Serialize the document onto System.out
      TransformerFactory xformFactory = TransformerFactory.newInstance();
      Transformer idTransform = xformFactory.newTransformer();
      Source input = new DOMSource(doc);
      Result output = new StreamResult(System.out);
      idTransform.transform(input, output);

    } catch (FactoryConfigurationError e) {
      System.out.println("Could not locate a JAXP factory class");
    } catch (ParserConfigurationException e) {
      System.out.println("Could not locate a JAXP DocumentBuilder class");
    } catch (DOMException e) {
      System.err.println(e);
    } catch (TransformerConfigurationException e) {
      System.err.println(e);
    } catch (TransformerException e) {
      System.err.println(e);
    }
  }
Example #2
0
  /**
   * Creates empty DOM Document using JAXP factoring. E.g.:
   *
   * <p>
   *
   * <pre>
   * Document doc = createDocument("book", null, null, null);
   * </pre>
   *
   * <p>creates new DOM of a well-formed document with root element named book.
   *
   * @param rootQName qualified name of root element. e.g. <code>myroot</code> or <code>ns:myroot
   *     </code>
   * @param namespaceURI URI of root element namespace or <code>null</code>
   * @param doctypePublicID public ID of DOCTYPE or <code>null</code>
   * @param doctypeSystemID system ID of DOCTYPE or <code>null</code> if no DOCTYPE required and
   *     doctypePublicID is also <code>null</code>
   * @throws DOMException if new DOM with passed parameters can not be created
   * @throws FactoryConfigurationError Application developers should never need to directly catch
   *     errors of this type.
   * @return new DOM Document
   */
  public static Document createDocument(
      String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID)
      throws DOMException {

    DOMImplementation impl = getDOMImplementation();

    if (doctypePublicID != null && doctypeSystemID == null) {
      throw new IllegalArgumentException(
          "System ID cannot be null if public ID specified. "); // NOI18N
    }

    DocumentType dtd = null;
    if (doctypeSystemID != null) {
      dtd = impl.createDocumentType(rootQName, doctypePublicID, doctypeSystemID);
    }

    return impl.createDocument(namespaceURI, rootQName, dtd);
  }