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

📄 writexml.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 java.io.PrintWriter;

import weblogic.xml.stream.ElementFactory;
import weblogic.xml.stream.XMLInputStream;
import weblogic.xml.stream.XMLInputStreamFactory;
import weblogic.xml.stream.XMLOutputStream;
import weblogic.xml.stream.XMLOutputStreamFactory;
import weblogic.xml.stream.XMLStreamException;


/**
 * This class Writes an XML on the screen using the WebLogic Streaming API.
 * 
 * @version 1.0
 */
public class WriteXML {
    /** File name of an existing XML */
    private String fileName = null;

    /**
     * Creates a new WriteXML object.
     *
     * @param file File name of an existing XML
     */
    public WriteXML(String file) {
        fileName = file;
    }

    /**
     * An Entry point to the program. Pass an existing XML file, optionally.
     *
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        WriteXML xmlWriter = null;

        if (args.length == 1) {
            xmlWriter = new WriteXML(args[0]);
        } else {
            xmlWriter = new WriteXML(null);
        }

        try {
            xmlWriter.process();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Dump the XML onto the screen
     *
     * @exceptions FileNotFoundException When the existing XML file cannot be
     * accessed
     * @exceptions XMLStreamException XML Streaming errors
     */
    private void process()
                  throws FileNotFoundException, XMLStreamException {
        XMLOutputStreamFactory factory = XMLOutputStreamFactory.newInstance();
        XMLOutputStream outputStream = factory.newOutputStream(
                                               new PrintWriter(System.out, true));

        outputStream.add(ElementFactory.createStartElement("email"));
        outputStream.add(ElementFactory.createStartElement("to"));
        outputStream.add(ElementFactory.createAttribute("name", "John Doe"));
        outputStream.add(ElementFactory.createAttribute("id", "john@doe.com"));

        outputStream.add(ElementFactory.createEndElement("to"));
        outputStream.add(ElementFactory.createStartElement("subject"));
        outputStream.add(ElementFactory.createCharacterData("This is a test"));
        outputStream.add(ElementFactory.createEndElement("subject"));
        outputStream.add(ElementFactory.createStartElement("your_xml"));

        if (fileName != null) {
            XMLInputStreamFactory inpFactory = XMLInputStreamFactory.newInstance();

            XMLInputStream inputStream = inpFactory.newInputStream(
                                                 new FileInputStream(fileName));
            outputStream.add(inputStream);
        }

        outputStream.add(ElementFactory.createEndElement("your_xml"));
        outputStream.add(ElementFactory.createEndElement("email"));

        outputStream.close();
    }
}

⌨️ 快捷键说明

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