📄 xmldocument.java
字号:
newDocument(getDocType(), getRootNode()); } /** * creates a new Document with the given information * * @param docType the DOCTYPE definition (no checking happens!), can be null * @param rootNode the name of the root node (must correspond to the one * given in <code>docType</code>) * @return returns the just created DOM document for convenience */ public Document newDocument(String docType, String rootNode) { m_Document = getBuilder().newDocument(); m_Document.appendChild(m_Document.createElement(rootNode)); setDocType(docType); return getDocument(); } /** * parses the given XML string (can be XML or a filename) and returns a * DOM Document * @param xml the xml to parse (if "<?xml" is not found then it is considered a file) * @return the parsed DOM document * @throws Exception if something goes wrong with the parsing */ public Document read(String xml) throws Exception { if (xml.toLowerCase().indexOf("<?xml") > -1) return read(new ByteArrayInputStream(xml.getBytes())); else return read(new File(xml)); } /** * parses the given file and returns a DOM document * @param file the XML file to parse * @return the parsed DOM document * @throws Exception if something goes wrong with the parsing */ public Document read(File file) throws Exception { m_Document = getBuilder().parse(file); return getDocument(); } /** * parses the given stream and returns a DOM document * @param stream the XML stream to parse * @return the parsed DOM document * @throws Exception if something goes wrong with the parsing */ public Document read(InputStream stream) throws Exception { m_Document = getBuilder().parse(stream); return getDocument(); } /** * parses the given reader and returns a DOM document * @param reader the XML reader to parse * @return the parsed DOM document * @throws Exception if something goes wrong with the parsing */ public Document read(Reader reader) throws Exception { m_Document = getBuilder().parse(new InputSource(reader)); return getDocument(); } /** * writes the current DOM document into the given file * @param file the filename to write to * @throws Exception if something goes wrong with the parsing */ public void write(String file) throws Exception { write(new File(file)); } /** * writes the current DOM document into the given file * @param file the filename to write to * @throws Exception if something goes wrong with the parsing */ public void write(File file) throws Exception { write(new BufferedWriter(new FileWriter(file))); } /** * writes the current DOM document into the given stream * @param stream the filename to write to * @throws Exception if something goes wrong with the parsing */ public void write(OutputStream stream) throws Exception { String xml; xml = toString(); stream.write(xml.getBytes(), 0, xml.length()); stream.flush(); } /** * writes the current DOM document into the given writer * @param writer the filename to write to * @throws Exception if something goes wrong with the parsing */ public void write(Writer writer) throws Exception { writer.write(toString()); writer.flush(); } /** * returns all non tag-children from the given node * * @param parent the node to get the children from * @return a vector containing all the non-text children */ public static Vector getChildTags(Node parent) { return getChildTags(parent, ""); } /** * returns all non tag-children from the given node * * @param parent the node to get the children from * @param name the name of the tags to return, "" for all * @return a vector containing all the non-text children */ public static Vector getChildTags(Node parent, String name) { Vector result; int i; NodeList list; result = new Vector(); list = parent.getChildNodes(); for (i = 0; i < list.getLength(); i++) { if (!(list.item(i) instanceof Element)) continue; // only tags with a certain name? if (name.length() != 0) { if (!((Element) list.item(i)).getTagName().equals(name)) continue; } result.add(list.item(i)); } return result; } /** * returns the text between the opening and closing tag of a node * (performs a <code>trim()</code> on the result) * * @param node the node to get the text from * @return the content of the given node */ public static String getContent(Element node) { NodeList list; Node item; int i; String result; result = ""; list = node.getChildNodes(); for (i = 0; i < list.getLength(); i++) { item = list.item(i); if (item.getNodeType() == Node.TEXT_NODE) result += item.getNodeValue(); } return result.trim(); } /** * turns the given node into a XML-stringbuffer according to the depth * @param buf the stringbuffer so far * @param parent the current node * @param depth the current depth * @return the new XML-stringbuffer */ protected StringBuffer toString(StringBuffer buf, Node parent, int depth) { NodeList list; Node node; int i; int n; String indent; NamedNodeMap atts; // build indent indent = ""; for (i = 0; i < depth; i++) indent += " "; if (parent.getNodeType() == Node.TEXT_NODE) { if (!parent.getNodeValue().trim().equals("")) buf.append(indent + parent.getNodeValue().trim() + "\n"); } else if (parent.getNodeType() == Node.COMMENT_NODE) { buf.append(indent + "<!--" + parent.getNodeValue() + "-->\n"); } else { buf.append(indent + "<" + parent.getNodeName()); // attributes? if (parent.hasAttributes()) { atts = parent.getAttributes(); for (n = 0; n < atts.getLength(); n++) { node = atts.item(n); buf.append(" " + node.getNodeName() + "=\"" + node.getNodeValue() + "\""); } } // children? if (parent.hasChildNodes()) { list = parent.getChildNodes(); // just a text node? if ( (list.getLength() == 1) && (list.item(0).getNodeType() == Node.TEXT_NODE) ) { buf.append(">"); buf.append(list.item(0).getNodeValue().trim()); buf.append("</" + parent.getNodeName() + ">\n"); } else { buf.append(">\n"); for (n = 0; n < list.getLength(); n++) { node = list.item(n); toString(buf, node, depth + 1); } buf.append(indent + "</" + parent.getNodeName() + ">\n"); } } else { buf.append("/>\n"); } } return buf; } /** * prints the current DOM document to standard out */ public void print() { System.out.println(toString()); } /** * returns the current DOM document as XML-string * @return the document as XML-string representation * @throws Exception if anything goes wrong initializing the parsing */ public String toString() { String header; header = PI + "\n\n"; if (getDocType() != null) header += getDocType() + "\n\n"; return toString(new StringBuffer(header), getDocument().getDocumentElement(), 0).toString(); } /** * for testing only. takes the name of an XML file as first arg, reads that * file, prints it to stdout and if a second filename is given, writes the * parsed document to that again. */ public static void main(String[] args) throws Exception { XMLDocument doc; if (args.length > 0) { doc = new XMLDocument(); // read doc.read(args[0]); // print to stdout doc.print(); // output? if (args.length > 1) { doc.write(args[1]); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -