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

📄 saxtreevalidator.java

📁 全面的展示了SAX解析XML文件的强大威力
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                             String qName, Attributes atts)
        throws SAXException {

        DefaultMutableTreeNode element = 
            new DefaultMutableTreeNode("Element: " + localName);
        current.add(element);
        current = element;

        // Determine namespace
        if (namespaceURI.length() > 0) {
            String prefix = 
                (String)namespaceMappings.get(namespaceURI);
            if (prefix.equals("")) {
                prefix = "[None]";
            }
            DefaultMutableTreeNode namespace =
                new DefaultMutableTreeNode("Namespace: prefix = '" +
                    prefix + "', URI = '" + namespaceURI + "'");
            current.add(namespace);
        }

        // Process attributes
        for (int i=0; i<atts.getLength(); i++) {
            DefaultMutableTreeNode attribute =
                new DefaultMutableTreeNode("Attribute (name = '" +  atts.getLocalName(i) + 
                                           "', value = '" +
                                           atts.getValue(i) + "')");
            String attURI = atts.getURI(i);
            if (attURI.length() > 0) {
                String attPrefix = 
                    (String)namespaceMappings.get(namespaceURI);
                if (attPrefix.equals("")) {
                    attPrefix = "[None]";
                }
                DefaultMutableTreeNode attNamespace =
                    new DefaultMutableTreeNode("Namespace: prefix = '" +
                        attPrefix + "', URI = '" + attURI + "'");
                attribute.add(attNamespace);            
            }
            current.add(attribute);
        }
    }

    /**
     * <p>
     *   Indicates the end of an element
     *     (<code>&lt;/[element name]&gt;</code>) is reached. Note that
     *     the parser does not distinguish between empty
     *     elements and non-empty elements, so this occurs uniformly.
     * </p>
     *
     * @param namespaceURI <code>String</code> URI of namespace this
     *                element is associated with
     * @param localName <code>String</code> name of element without prefix
     * @param qName <code>String</code> name of element in XML 1.0 form
     * @throws <code>SAXException</code> when things go wrong
     */
    public void endElement(String namespaceURI, String localName,
                           String qName)
        throws SAXException {

        // Walk back up the tree
        current = (DefaultMutableTreeNode)current.getParent();
    }

    /**
     * <p>
     *   This reports character data (within an element).
     * </p>
     *
     * @param ch <code>char[]</code> character array with character data
     * @param start <code>int</code> index in array where data starts.
     * @param length <code>int</code> index in array where data ends.
     * @throws <code>SAXException</code> when things go wrong
     */ public void characters(char[] ch, int start, int length)
        throws SAXException {

        String s = new String(ch, start, length);
        DefaultMutableTreeNode data =
            new DefaultMutableTreeNode("Character Data: '" + s + "'");
        current.add(data);
    }

    /**
     * <p>
     * This reports whitespace that can be ignored in the
     * originating document. This is typically invoked only when
     * validation is ocurring in the parsing process.
     * </p>
     *
     * @param ch <code>char[]</code> character array with character data
     * @param start <code>int</code> index in array where data starts.
     * @param end <code>int</code> index in array where data ends.
     * @throws <code>SAXException</code> when things go wrong
     */
    public void ignorableWhitespace(char[] ch, int start, int length)
        throws SAXException {
        // This is ignorable, so don't display it
    }

    /**
     * <p>
     *   This reports an entity that is skipped by the parser. This
     *     should only occur for non-validating parsers, and then is still
     *     implementation-dependent behavior.
     * </p>
     *
     * @param name <code>String</code> name of entity being skipped
     * @throws <code>SAXException</code> when things go wrong
     */
    public void skippedEntity(String name) throws SAXException {
        DefaultMutableTreeNode skipped =
            new DefaultMutableTreeNode("Skipped Entity: '" + name + "'");
        current.add(skipped);
    }
}

/**
 * <b><code>JValidatorErrorHandler</code></b> implements the SAX
 *   <code>ErrorHandler</code> interface and defines callback
 *   behavior for the SAX callbacks associated with an XML
 *   document's warnings and errors.
 */
class JValidatorErrorHandler implements ErrorHandler {

    /**
     * <p>
     * This will report a warning that has occurred; this indicates
     *   that while no XML rules were "broken", something appears
     *   to be incorrect or missing.
     * </p>
     *
     * @param exception <code>SAXParseException</code> that occurred.
     * @throws <code>SAXException</code> when things go wrong 
     */
    public void warning(SAXParseException exception)
        throws SAXException {
        System.out.println("**Parsing Warning**\n" +
                           "  Line:    " + 
                              exception.getLineNumber() + "\n" +
                           "  URI:     " + 
                              exception.getSystemId() + "\n" +
                           "  Message: " + 
                              exception.getMessage());        
        throw new SAXException("Warning encountered");
    }

    /**
     * <p>
     * This will report an error that has occurred; this indicates
     *   that a rule was broken, typically in validation, but that
     *   parsing can reasonably continue.
     * </p>
     *
     * @param exception <code>SAXParseException</code> that occurred.
     * @throws <code>SAXException</code> when things go wrong 
     */
    public void error(SAXParseException exception)
        throws SAXException {
        System.out.println("**Parsing Error**\n" +
                           "  Line:    " + 
                              exception.getLineNumber() + "\n" +
                           "  URI:     " + 
                              exception.getSystemId() + "\n" +
                           "  Message: " + 
                              exception.getMessage());
        throw new SAXException("Error encountered");
    }

    /**
     * <p>
     * This will report a fatal error that has occurred; this indicates
     *   that a rule has been broken that makes continued parsing either
     *   impossible or an almost certain waste of time.
     * </p>
     *
     * @param exception <code>SAXParseException</code> that occurred.
     * @throws <code>SAXException</code> when things go wrong 
     */
    public void fatalError(SAXParseException exception)
        throws SAXException {
        System.out.println("**Parsing Fatal Error**\n" +
                           "  Line:    " + 
                              exception.getLineNumber() + "\n" +
                           "  URI:     " + 
                              exception.getSystemId() + "\n" +
                           "  Message: " + 
                              exception.getMessage());        
        throw new SAXException("Fatal Error encountered");
    }
}
//Demo file: book.xml
/*
<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "DTD/JavaXML.dtd">
<!-- Java and XML Contents -->
<book xmlns="http://www.oreilly.com/javaxml2" xmlns:ora="http://www.oreilly.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oreilly.com/javaxml2 XSD/contents.xsd 
                          http://www.oreilly.com XSD/contents-ora.xsd">
  <title ora:series="Java">Java and XML</title>
  <!-- Chapter List -->
  <contents>
    <chapter title="Introduction" number="1">
      <topic name="XML Matters"/>
      <topic name="What's Important"/>
      <topic name="The Essentials"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Nuts and Bolts" number="2">
      <topic name="The Basics"/>
      <topic name="Constraints"/>
      <topic name="Transformations"/>
      <topic name="And More..."/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="SAX" number="3">
      <topic name="Getting Prepared"/>
      <topic name="SAX Readers"/>
      <topic name="Content Handlers"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Advanced SAX" number="4">
      <topic name="Properties and Features"/>
      <topic name="More Handlers"/>
      <topic name="Filters and Writers"/>
      <topic name="Even More Handlers"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="DOM" number="5">
      <topic name="The Document Object Model"/>
      <topic name="Serialization"/>
      <topic name="Mutability"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Advanced DOM" number="6">
      <topic name="DOM and Mutation"/>
      <topic name="Namespaces and DOM Level 2"/>
      <topic name="DOM and HTML"/>
      <topic name="DOM Level 3"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="JDOM" number="7">
      <topic name="The Basics"/>
      <topic name="PropsToXML"/>
      <topic name="XMLProperties"/>
      <topic name="Is JDOM a Standard?"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Advanced JDOM" number="8">
      <topic name="The Whole Ball of Wax"/>
      <topic name="JDOM and Factories"/>
      <topic name="Wrappers and Decorators"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="JAXP" number="9">
      <topic name="API or Abstraction?"/>
      <topic name="JAXP 1.0"/>
      <topic name="JAXP 1.1"/>
      <topic name="Gotcha!"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Web Publishing Frameworks" number="10">
      <topic name="Selecting a Framework"/>
      <topic name="Installation"/>
      <topic name="Using a Publishing Framework"/>
      <topic name="XSP"/>
      <topic name="Cocoon 2.0 and Beyond"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="XML-RPC" number="11">
      <topic name="RPC Versus RMI"/>
      <topic name="Saying Hello"/>
      <topic name="The Real World"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="SOAP" number="12">
      <topic name="Starting Out"/>
      <topic name="Setting Up"/>
      <topic name="Getting Dirty"/>
      <topic name="Going Further"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Web Services" number="13">
      <topic name="Web Services"/>
      <topic name="UDDI"/>
      <topic name="WSDL"/>
      <topic name="Putting It All Together"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Content Syndication" number="14">
      <topic name="The Foobar Public Library"/>
      <topic name="mytechbooks.comI"/>
      <topic name="Push Versus Pull"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="XML Data Binding" number="15">
      <topic name="First Principles"/>
      <topic name="Castor"/>
      <topic name="Zeus"/>
      <topic name="JAXB"/>
      <topic name="What&apos;s Next?"/>
    </chapter>
    <chapter title="Looking Forward" number="16">
      <topic name="XLink"/>
      <topic name="XPointer"/>
      <topic name="XML Schema Bindings"/>
      <topic name="And the Rest..."/>
      <topic name="What&apos;s Next?"/>
    </chapter>
  </contents>
  <ora:copyright>&OReillyCopyright;</ora:copyright>
</book>


*/ 

⌨️ 快捷键说明

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