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

📄 processxml.java

📁 BEA WebLogic Server 8.1大全 = BEA webLogic server 8.1 unleashed (美) Mark Artiges等著 袁毅 ... [等] 译 eng
💻 JAVA
字号:
/* 
 * WebLogic Server Unleashed
 * 
 */
package com.wlsunleashed.xml.stream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import weblogic.xml.stream.Attribute;
import weblogic.xml.stream.AttributeIterator;
import weblogic.xml.stream.StartDocument;
import weblogic.xml.stream.StartElement;
import weblogic.xml.stream.XMLEvent;
import weblogic.xml.stream.XMLInputStream;
import weblogic.xml.stream.XMLInputStreamFactory;
import weblogic.xml.stream.XMLStreamException;
import weblogic.xml.stream.util.TypeFilter;


/**
 * This class Processes a given XML using WebLogic Streaming API
 * and dumps the contents on the screen
 * 
 * @version 1.0
 */
public class ProcessXML {
    /** The XML file to be processed */
    private String fileName = null;

    /**
     * Creates a new StreamRepresentation object.
     *
     * @param file The XML file to be processed
     *
     */
    public ProcessXML(String file) {
        fileName = file;
    }

    /**
     * The entry point into this class - pass the XML file as 
     * parameter
     *
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Usage: StreamRepresentation <XML file>.xml");
            System.exit(1);
        }

        try {
            ProcessXML xmlProcessor = new ProcessXML(args[0]);

            xmlProcessor.process();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Processes the given file
     *
     * @exceptions FileNotFoundException The file input was not found
     * @exceptions XMLStreamException Problem while parsing.
     */
    private void process()
                  throws FileNotFoundException, XMLStreamException {
        XMLInputStreamFactory factory = XMLInputStreamFactory.newInstance();
        TypeFilter aFilter = new TypeFilter(XMLEvent.START_DOCUMENT
                                            | XMLEvent.START_ELEMENT);

        XMLInputStream inputStream = factory.newInputStream(
                                             new FileInputStream(fileName), 
                                             aFilter);

        while (inputStream.hasNext()) {
            XMLEvent anEvent = inputStream.next();
            processEvent(anEvent);
        }
    }

    /**
     * Method processEvent.
     *
     * @param anEvent
     */
    private void processEvent(XMLEvent anEvent) {
        switch (anEvent.getType()) {
        case XMLEvent.START_DOCUMENT:

            StartDocument startDoc = (StartDocument) anEvent;
            System.out.println(" In Start Doc: ");
            System.out.println("   -- Name = " + startDoc.getName());
            System.out.println("   -- System Id = " + startDoc.getSystemId());

            break;

        case XMLEvent.START_ELEMENT:

            StartElement startElem = (StartElement) anEvent;
            System.out.println(" In Start Element: ");
            System.out.println("  -- name = " + startElem.getName());

            AttributeIterator attrs = startElem.getAttributes();

            while (attrs.hasNext()) {
                Attribute anAttr = attrs.next();
                System.out.println("  -- Attr name = " + anAttr.getName());
                System.out.println("  -- -- Attr value = " + anAttr.getValue());
            }

            break;

        default:
            System.out.println(
                    "If you see this message, the filter has been changed");

            break;
        }
    }
}

⌨️ 快捷键说明

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