📄 xmldocument.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * XMLDocument.java * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand */package weka.core.xml;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileWriter;import java.io.InputStream;import java.io.OutputStream;import java.io.Reader;import java.io.Writer;import java.util.Vector;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;/** * This class offers some methods for generating, reading and writing * XML documents.<br> * It can only handle UTF-8. * * @see #PI * @author FracPete (fracpete at waikato dot ac dot nz) * @version $Revision 1.0$ */public class XMLDocument { /** the parsing instructions "<?xml version=\"1.0\" encoding=\"utf-8\"?>" * (may not show up in Javadoc due to tags!) */ public final static String PI = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; // DTD placeholders /** the DocType definition */ public final static String DTD_DOCTYPE = "DOCTYPE"; /** the Element definition */ public final static String DTD_ELEMENT = "ELEMENT"; /** the AttList definition */ public final static String DTD_ATTLIST = "ATTLIST"; /** the optional marker */ public final static String DTD_OPTIONAL = "?"; /** the at least one marker */ public final static String DTD_AT_LEAST_ONE = "+"; /** the zero or more marker */ public final static String DTD_ZERO_OR_MORE = "*"; /** the option separator */ public final static String DTD_SEPARATOR = "|"; /** the CDATA placeholder */ public final static String DTD_CDATA = "CDATA"; /** the ANY placeholder */ public final static String DTD_ANY = "ANY"; /** the #PCDATA placeholder */ public final static String DTD_PCDATA = "#PCDATA"; /** the #IMPLIED placeholder */ public final static String DTD_IMPLIED = "#IMPLIED"; /** the #REQUIRED placeholder */ public final static String DTD_REQUIRED = "#REQUIRED"; // often used attributes /** the "version" attribute */ public final static String ATT_VERSION = "version"; /** the "name" attribute */ public final static String ATT_NAME = "name"; // often used values /** the value "yes" */ public final static String VAL_YES = "yes"; /** the value "no" */ public final static String VAL_NO = "no"; // members /** the factory for DocumentBuilder */ protected DocumentBuilderFactory m_Factory = null; /** the instance of a DocumentBuilder */ protected DocumentBuilder m_Builder = null; /** whether to use a validating parser or not */ protected boolean m_Validating = false; /** the DOM document */ protected Document m_Document = null; /** the DOCTYPE node as String */ protected String m_DocType = null; /** the root node as String */ protected String m_RootNode = null; /** * initializes the factory with non-validating parser * * @throws Exception if the construction fails */ public XMLDocument() throws Exception { m_Factory = DocumentBuilderFactory.newInstance(); setDocType(null); setRootNode(null); setValidating(false); } /** * Creates a new instance of XMLDocument * @param xml the xml to parse (if "<?xml" is not found then it is considered a file) * @throws Exception if the construction of the DocumentBuilder fails * @see #setValidating(boolean) */ public XMLDocument(String xml) throws Exception { this(); read(xml); } /** * Creates a new instance of XMLDocument * @param file the XML file to parse * @throws Exception if the construction of the DocumentBuilder fails * @see #setValidating(boolean) */ public XMLDocument(File file) throws Exception { this(); read(file); } /** * Creates a new instance of XMLDocument * @param stream the XML stream to parse * @throws Exception if the construction of the DocumentBuilder fails * @see #setValidating(boolean) */ public XMLDocument(InputStream stream) throws Exception { this(); read(stream); } /** * Creates a new instance of XMLDocument * @param reader the XML reader to parse * @throws Exception if the construction of the DocumentBuilder fails * @see #setValidating(boolean) */ public XMLDocument(Reader reader) throws Exception { this(); read(reader); } /** * returns the DocumentBuilderFactory * @return the DocumentBuilderFactory */ public DocumentBuilderFactory getFactory() { return m_Factory; } /** * returns the DocumentBuilder * @return the DocumentBuilder */ public DocumentBuilder getBuilder() { return m_Builder; } /** * returns whether a validating parser is used * @return whether a validating parser is used */ public boolean getValidating() { return m_Validating; } /** * sets whether to use a validating parser or not.<br> * Note: this does clear the current DOM document! * @param validating whether to use a validating parser * @throws Exception if the instantiating of the DocumentBuilder fails */ public void setValidating(boolean validating) throws Exception { m_Validating = validating; m_Factory.setValidating(validating); m_Builder = m_Factory.newDocumentBuilder(); clear(); } /** * returns the parsed DOM document * @return the parsed DOM document */ public Document getDocument() { return m_Document; } /** * sets the DOM document to use * @param newDocument the DOM document to use */ public void setDocument(Document newDocument) { m_Document = newDocument; } /** * sets the DOCTYPE-String to use in the XML output. Performs NO checking! * if it is <code>null</code> the DOCTYPE is omitted. * * @param docType the DOCTYPE definition to use in XML output */ public void setDocType(String docType) { m_DocType = docType; } /** * returns the current DOCTYPE, can be <code>null</code> * * @return the current DOCTYPE definition, can be <code>null</code> */ public String getDocType() { return m_DocType; } /** * sets the root node to use in the XML output. Performs NO checking with DOCTYPE! * * @param rootNode the root node to use in the XML output */ public void setRootNode(String rootNode) { if (rootNode == null) m_RootNode = "root"; else m_RootNode = rootNode; } /** * returns the current root node * * @return the current root node */ public String getRootNode() { return m_RootNode; } /** * sets up an empty DOM document, with the current DOCTYPE and root node * * @see #setRootNode(String) * @see #setDocType(String) */ public void clear() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -