List of usage examples for org.w3c.dom Document getDoctype
public DocumentType getDoctype();
DocumentType
) associated with this document. From source file:DOMEdit.java
private static void outputHeading(Document doc) { System.out.print("<?xml version=\"1.0\""); DocumentType doctype = doc.getDoctype(); if (doctype != null) { if ((doctype.getPublicId() == null) && (doctype.getSystemId() == null)) { System.out.println(" standalone=\"yes\"?>"); } else {//from ww w . jav a2s . c o m System.out.println(" standalone=\"no\"?>"); } } else { System.out.println("?>"); } }
From source file:DOMCopy.java
private static void outputHeading(Document doc) { System.out.print("<?xml version=\"1.0\""); DocumentType doctype = doc.getDoctype(); if (doctype != null) { if ((doctype.getPublicId() == null) && (doctype.getSystemId() == null)) { System.out.println(" standalone=\"yes\"?>"); } else { System.out.println(" standalone=\"no\"?>"); }//from ww w .j av a 2 s. co m } else { System.out.println("?>"); } }
From source file:ca.uviccscu.lp.utils.Utils.java
public static String readOutDomTree(Document d) { StringBuilder sb = new StringBuilder(); Document xmlDoc = d; DocumentType doctype = xmlDoc.getDoctype(); if (doctype == null) { sb.append("\n"); sb.append("DOCTYPE is null"); sb.append("\n"); } else {//from w w w . j a v a 2 s. co m sb.append("DOCTYPE node:\n" + doctype.getInternalSubset()); sb.append("\n"); } sb.append("\nDocument body contents are:"); sb.append("\n"); listNodes(xmlDoc.getDocumentElement(), "", sb); // Root element & children return sb.toString(); }
From source file:Main.java
public static void saveDocument(Document dom, String file) throws TransformerException, IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dom.getDoctype().getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dom.getDoctype().getSystemId()); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(); FileOutputStream outputStream = null; try {/*from w w w . j a va2s . c o m*/ outputStream = new FileOutputStream(file); result.setOutputStream(outputStream); transformer.transform(source, result); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintStream</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * /*w w w . ja va 2 s . co m*/ * @param out The <CODE>PrintStream</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(Node) * @see #dump(PrintStream, Node) * @since TFP 1.0 */ private static void doDump(PrintStream out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
/** * Performs the actual recursive dumping of a DOM tree to a given * <CODE>PrintWriter</CODE>. Note that dump is intended to be a detailed * debugging aid rather than pretty to look at. * // w ww . j av a 2 s . c o m * @param out The <CODE>PrintWriter</CODE> to write to. * @param node The <CODE>Node</CODE> under consideration. * @param indent The level of indentation. * @see #dump(PrintWriter, Node) * @since TFP 1.0 */ private static void doDump(PrintWriter out, final Node node, int indent) { if (node != null) { for (int index = 0; index < indent; ++index) out.write(' '); switch (node.getNodeType()) { case Node.DOCUMENT_NODE: { Document document = (Document) node; out.println("DOCUMENT:"); doDump(out, document.getDoctype(), indent + 1); doDump(out, document.getDocumentElement(), indent + 1); break; } case Node.DOCUMENT_TYPE_NODE: { DocumentType type = (DocumentType) node; out.println("DOCTYPE: [" + "name=" + format(type.getName()) + "," + "publicId=" + format(type.getPublicId()) + "," + "systemId=" + format(type.getSystemId()) + "]"); break; } case Node.ELEMENT_NODE: { Element element = (Element) node; out.println("ELEMENT: [" + "ns=" + format(element.getNamespaceURI()) + "," + "name=" + format(element.getLocalName()) + "]"); NamedNodeMap attrs = element.getAttributes(); for (int index = 0; index < attrs.getLength(); ++index) doDump(out, attrs.item(index), indent + 1); for (Node child = element.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.ATTRIBUTE_NODE: { Attr attr = (Attr) node; out.println("ATTRIBUTE: [" + "ns=" + format(attr.getNamespaceURI()) + "," + "prefix=" + format(attr.getPrefix()) + "," + "name=" + format(attr.getLocalName()) + "," + "value=" + format(attr.getNodeValue()) + "]"); break; } case Node.TEXT_NODE: { Text text = (Text) node; out.println("TEXT: [" + format(text.getNodeValue()) + "]"); for (Node child = text.getFirstChild(); child != null;) { doDump(out, child, indent + 1); child = child.getNextSibling(); } break; } case Node.CDATA_SECTION_NODE: { CDATASection data = (CDATASection) node; out.println("CDATA: [" + format(data.getNodeValue()) + "]"); break; } case Node.COMMENT_NODE: { Comment comm = (Comment) node; out.println("COMMENT: [" + format(comm.getNodeValue()) + "]"); break; } default: out.println("UNKNOWN: [type=" + node.getNodeType() + "]"); break; } } }
From source file:Main.java
private static boolean updateXML(Document document, String path) throws TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // out put encoding. transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DocumentType type = document.getDoctype(); if (type != null) { System.out.println("doctype -->" + type.getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, type.getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, type.getSystemId()); }/*from w w w . jav a 2s . c o m*/ DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(path)); transformer.transform(source, result); transformer.reset(); return true; }
From source file:XMLUtil.java
public static void write(Document doc, OutputStream out) throws IOException { // XXX note that this may fail to write out namespaces correctly if the // document//from www. j av a 2s . c o m // is created with namespaces and no explicit prefixes; however no code in // this package is likely to be doing so try { Transformer t = TransformerFactory.newInstance().newTransformer(); DocumentType dt = doc.getDoctype(); if (dt != null) { String pub = dt.getPublicId(); if (pub != null) { t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub); } t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId()); } t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N t.setOutputProperty(OutputKeys.INDENT, "yes"); // NOI18N t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); // NOI18N Source source = new DOMSource(doc); Result result = new StreamResult(out); t.transform(source, result); } catch (Exception e) { throw (IOException) new IOException(e.toString()).initCause(e); } catch (TransformerFactoryConfigurationError e) { throw (IOException) new IOException(e.toString()).initCause(e); } }
From source file:Main.java
public static void writeXML(Document d, OutputStream os, String sysID) throws IOException { /**// ww w . j a v a 2s . c o m * To support i18n, we have to specify the encoding of * output writer to UTF-8 when we writing the XML. */ // PrintWriter out=new PrintWriter(os); PrintWriter out = new PrintWriter(new OutputStreamWriter(os, "UTF-8")); out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.println(); if (sysID != null) { out.println("<!DOCTYPE " + d.getDoctype().getName() + " SYSTEM \"" + sysID + "\">"); out.println(); } //d.getDocumentElement().normalize(); writeXMLwalkTree(d.getDocumentElement(), 0, out); out.flush(); }
From source file:Main.java
public static String getXml(Document doc) { DOMSource doms = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); String xml = null;/*from w ww .j a v a 2 s .c o m*/ try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "GB2312"); properties.setProperty(OutputKeys.METHOD, "xml");//! properties.setProperty(OutputKeys.VERSION, "1.0"); properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperties(properties); t.transform(doms, sr); String dtd = doc.getDoctype().getInternalSubset(); if ((null != dtd) && (dtd.length() > 0)) { dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n"; } ; xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd; xml += sw.toString(); } catch (TransformerConfigurationException tce) { //"Transformer Configuration Exception\n-----" } catch (TransformerException te) { //"Transformer Exception\n---------" } return xml; }