⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlelement.java

📁 Nano的XML解析器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* XMLElement.java * * $Revision: 1.4 $ * $Date: 2002/03/24 10:27:59 $ * $Name: RELEASE_2_2_1 $ * * This file is part of NanoXML 2 Lite. * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved. * * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the * use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * *  1. The origin of this software must not be misrepresented; you must not *     claim that you wrote the original software. If you use this software in *     a product, an acknowledgment in the product documentation would be *     appreciated but is not required. * *  2. Altered source versions must be plainly marked as such, and must not be *     misrepresented as being the original software. * *  3. This notice may not be removed or altered from any source distribution. *****************************************************************************/package nanoxml;import java.io.ByteArrayOutputStream;import java.io.CharArrayReader;import java.io.IOException;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.StringReader;import java.io.Writer;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;/** * XMLElement is a representation of an XML object. The object is able to parse * XML code. * <P><DL> * <DT><B>Parsing XML Data</B></DT> * <DD> * You can parse XML data using the following code: * <UL><CODE> * XMLElement xml = new XMLElement();<BR> * FileReader reader = new FileReader("filename.xml");<BR> * xml.parseFromReader(reader); * </CODE></UL></DD></DL> * <DL><DT><B>Retrieving Attributes</B></DT> * <DD> * You can enumerate the attributes of an element using the method * {@link #enumerateAttributeNames() enumerateAttributeNames}. * The attribute values can be retrieved using the method * {@link #getStringAttribute(java.lang.String) getStringAttribute}. * The following example shows how to list the attributes of an element: * <UL><CODE> * XMLElement element = ...;<BR> * Enumeration enum = element.getAttributeNames();<BR> * while (enum.hasMoreElements()) {<BR> * &nbsp;&nbsp;&nbsp;&nbsp;String key = (String) enum.nextElement();<BR> * &nbsp;&nbsp;&nbsp;&nbsp;String value = element.getStringAttribute(key);<BR> * &nbsp;&nbsp;&nbsp;&nbsp;System.out.println(key + " = " + value);<BR> * } * </CODE></UL></DD></DL> * <DL><DT><B>Retrieving Child Elements</B></DT> * <DD> * You can enumerate the children of an element using * {@link #enumerateChildren() enumerateChildren}. * The number of child elements can be retrieved using * {@link #countChildren() countChildren}. * </DD></DL> * <DL><DT><B>Elements Containing Character Data</B></DT> * <DD> * If an elements contains character data, like in the following example: * <UL><CODE> * &lt;title&gt;The Title&lt;/title&gt; * </CODE></UL> * you can retrieve that data using the method * {@link #getContent() getContent}. * </DD></DL> * <DL><DT><B>Subclassing XMLElement</B></DT> * <DD> * When subclassing XMLElement, you need to override the method * {@link #createAnotherElement() createAnotherElement} * which has to return a new copy of the receiver. * </DD></DL> * <P> * * @see nanoxml.XMLParseException * * @author Marc De Scheemaecker *         &lt;<A href="mailto:cyberelf@mac.com">cyberelf@mac.com</A>&gt; * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $ */public class XMLElement{    /**     * Serialization serial version ID.     */    static final long serialVersionUID = 6685035139346394777L;    /**     * Major version of NanoXML. Classes with the same major and minor     * version are binary compatible. Classes with the same major version     * are source compatible. If the major version is different, you may     * need to modify the client source code.     *     * @see nanoxml.XMLElement#NANOXML_MINOR_VERSION     */    public static final int NANOXML_MAJOR_VERSION = 2;        /**     * Minor version of NanoXML. Classes with the same major and minor     * version are binary compatible. Classes with the same major version     * are source compatible. If the major version is different, you may     * need to modify the client source code.     *     * @see nanoxml.XMLElement#NANOXML_MAJOR_VERSION     */    public static final int NANOXML_MINOR_VERSION = 2;    /**     * The attributes given to the element.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>The field can be empty.     *     <li>The field is never <code>null</code>.     *     <li>The keys and the values are strings.     * </ul></dd></dl>     */    private Hashtable attributes;    /**     * Child elements of the element.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>The field can be empty.     *     <li>The field is never <code>null</code>.     *     <li>The elements are instances of <code>XMLElement</code>     *         or a subclass of <code>XMLElement</code>.     * </ul></dd></dl>     */    private Vector children;    /**     * The name of the element.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>The field is <code>null</code> iff the element is not     *         initialized by either parse or setName.     *     <li>If the field is not <code>null</code>, it's not empty.     *     <li>If the field is not <code>null</code>, it contains a valid     *         XML identifier.     * </ul></dd></dl>     */    private String name;    /**     * The #PCDATA content of the object.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>The field is <code>null</code> iff the element is not a     *         #PCDATA element.     *     <li>The field can be any string, including the empty string.     * </ul></dd></dl>     */    private String contents;    /**     * Conversion table for &amp;...; entities. The keys are the entity names     * without the &amp; and ; delimiters.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>The field is never <code>null</code>.     *     <li>The field always contains the following associations:     *         "lt"&nbsp;=&gt;&nbsp;"&lt;", "gt"&nbsp;=&gt;&nbsp;"&gt;",     *         "quot"&nbsp;=&gt;&nbsp;"\"", "apos"&nbsp;=&gt;&nbsp;"'",     *         "amp"&nbsp;=&gt;&nbsp;"&amp;"     *     <li>The keys are strings     *     <li>The values are char arrays     * </ul></dd></dl>     */    private Hashtable entities;    /**     * The line number where the element starts.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li><code>lineNr &gt= 0</code>     * </ul></dd></dl>     */    private int lineNr;    /**     * <code>true</code> if the case of the element and attribute names     * are case insensitive.     */    private boolean ignoreCase;    /**     * <code>true</code> if the leading and trailing whitespace of #PCDATA     * sections have to be ignored.     */    private boolean ignoreWhitespace;    /**     * Character read too much.     * This character provides push-back functionality to the input reader     * without having to use a PushbackReader.     * If there is no such character, this field is '\0'.     */    private char charReadTooMuch;    /**     * The reader provided by the caller of the parse method.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>The field is not <code>null</code> while the parse method     *         is running.     * </ul></dd></dl>     */    private Reader reader;    /**     * The current line number in the source content.     *     * <dl><dt><b>Invariants:</b></dt><dd>     * <ul><li>parserLineNr &gt; 0 while the parse method is running.     * </ul></dd></dl>     */    private int parserLineNr;    /**     * Creates and initializes a new XML element.     * Calling the construction is equivalent to:     * <ul><code>new XMLElement(new Hashtable(), false, true)     * </code></ul>     *     * <dl><dt><b>Postconditions:</b></dt><dd>     * <ul><li>countChildren() => 0     *     <li>enumerateChildren() => empty enumeration     *     <li>enumeratePropertyNames() => empty enumeration     *     <li>getChildren() => empty vector     *     <li>getContent() => ""     *     <li>getLineNr() => 0     *     <li>getName() => null     * </ul></dd></dl>     *     * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable)     *         XMLElement(Hashtable)     * @see nanoxml.XMLElement#XMLElement(boolean)     * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)     *         XMLElement(Hashtable, boolean)     */    public XMLElement()    {        this(new Hashtable(), false, true, true);    }        /**     * Creates and initializes a new XML element.     * Calling the construction is equivalent to:     * <ul><code>new XMLElement(entities, false, true)     * </code></ul>     *     * @param entities     *     The entity conversion table.     *     * </dl><dl><dt><b>Preconditions:</b></dt><dd>     * <ul><li><code>entities != null</code>     * </ul></dd></dl>     *     * <dl><dt><b>Postconditions:</b></dt><dd>     * <ul><li>countChildren() => 0     *     <li>enumerateChildren() => empty enumeration     *     <li>enumeratePropertyNames() => empty enumeration     *     <li>getChildren() => empty vector     *     <li>getContent() => ""     *     <li>getLineNr() => 0     *     <li>getName() => null     * </ul></dd></dl><dl>     *     * @see nanoxml.XMLElement#XMLElement()     * @see nanoxml.XMLElement#XMLElement(boolean)     * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)     *         XMLElement(Hashtable, boolean)     */    public XMLElement(Hashtable entities)    {        this(entities, false, true, true);    }    /**     * Creates and initializes a new XML element.     * Calling the construction is equivalent to:     * <ul><code>new XMLElement(new Hashtable(), skipLeadingWhitespace, true)     * </code></ul>     *     * @param skipLeadingWhitespace     *     <code>true</code> if leading and trailing whitespace in PCDATA     *     content has to be removed.     *     * </dl><dl><dt><b>Postconditions:</b></dt><dd>     * <ul><li>countChildren() => 0     *     <li>enumerateChildren() => empty enumeration     *     <li>enumeratePropertyNames() => empty enumeration     *     <li>getChildren() => empty vector     *     <li>getContent() => ""     *     <li>getLineNr() => 0     *     <li>getName() => null     * </ul></dd></dl><dl>     *     * @see nanoxml.XMLElement#XMLElement()     * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable)     *         XMLElement(Hashtable)     * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)     *         XMLElement(Hashtable, boolean)     */    public XMLElement(boolean skipLeadingWhitespace)    {        this(new Hashtable(), skipLeadingWhitespace, true, true);    }    /**     * Creates and initializes a new XML element.     * Calling the construction is equivalent to:     * <ul><code>new XMLElement(entities, skipLeadingWhitespace, true)     * </code></ul>     *     * @param entities     *     The entity conversion table.     * @param skipLeadingWhitespace     *     <code>true</code> if leading and trailing whitespace in PCDATA     *     content has to be removed.     *     * </dl><dl><dt><b>Preconditions:</b></dt><dd>     * <ul><li><code>entities != null</code>     * </ul></dd></dl>     *     * <dl><dt><b>Postconditions:</b></dt><dd>     * <ul><li>countChildren() => 0     *     <li>enumerateChildren() => empty enumeration     *     <li>enumeratePropertyNames() => empty enumeration     *     <li>getChildren() => empty vector     *     <li>getContent() => ""     *     <li>getLineNr() => 0     *     <li>getName() => null     * </ul></dd></dl><dl>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -