📄 streamrepresentation.java
字号:
/*
* WebLogic Server Unleashed
*
*/
package com.wlsunleashed.xml.xpath;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import weblogic.xml.stream.Attribute;
import weblogic.xml.stream.AttributeIterator;
import weblogic.xml.stream.EndElement;
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.xpath.StreamXPath;
import weblogic.xml.xpath.XPathException;
import weblogic.xml.xpath.XPathStreamFactory;
import weblogic.xml.xpath.XPathStreamObserver;
/**
* This class Processes a given XML using WebLogic Streaming API
* and dumps the contents on the screen
*
* @version 1.0
*/
public class StreamRepresentation {
/** The XML file to be processed */
private String fileName = null;
/**
* Creates a new StreamRepresentation object.
*
* @param file The XML file to be processed
*
*/
public StreamRepresentation(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 {
StreamRepresentation xmlProcessor = new StreamRepresentation(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, XPathException {
StreamXPath recipientsXPath = new StreamXPath("email/to");
XPathStreamFactory factory = new XPathStreamFactory();
factory.install(recipientsXPath,
new XPathStreamObserver() {
public void observe(XMLEvent event) {
System.out.println("Observing Event " + event.getName());
processEvent(event);
}
public void observeAttribute(StartElement e, Attribute a) {
System.out.println("Observing Attribute " + a.getName() + " on element " + e.getName());
}
public void observeNamespace(StartElement e, Attribute a) {
System.out.println("Observing Namespace " + a.getName() + " on element " + e.getName());
}
});
XMLInputStreamFactory streamFactory = XMLInputStreamFactory.newInstance();
XMLInputStream inputStream = streamFactory.newInputStream(
new FileInputStream(fileName));
XMLInputStream searchStream = factory.createStream(inputStream);
while (searchStream.hasNext()) {
XMLEvent anEvent = searchStream.next();
}
}
/**
* Method processEvent.
*
* @param anEvent
*/
private void processEvent(XMLEvent anEvent) {
switch(anEvent.getType()) {
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;
case XMLEvent.END_ELEMENT:
EndElement endElem = (EndElement) anEvent;
System.out.println(" In End Element: ");
System.out.println(" -- name = " + endElem.getName());
break;
default:
System.out.println(" type = " + anEvent.getTypeAsString());
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -