📄 writexml.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 + -