📄 streamparser.java
字号:
package examples.xml.orderParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import weblogic.xml.stream.XMLInputStream;
import weblogic.xml.stream.XMLInputStreamFactory;
import weblogic.xml.stream.XMLEvent;
import weblogic.xml.stream.CharacterData;
import weblogic.xml.stream.StartElement;
import weblogic.xml.stream.XMLName;
import weblogic.xml.stream.XMLStreamException;
import weblogic.xml.stream.AttributeIterator;
import weblogic.xml.stream.Attribute;
/**
* Demonstrates the parsing of an XML document into
* business objects using the XMLInputStream API.
*
* @author Copyright (c) 2003 by BEA Systems, Inc. All Rights Reserved.
*/
public class StreamParser {
private OrderBO orderBO = null;
// Set to true for additional logging.
private static final boolean DEBUG = false;
public StreamParser() { }
/**
* <p>Creates a new XMLInputStream.</p>
*
* @param pName Name of XML document.
* @return XMLInputStream
* @exception XMLStreamException
* @exception FileNotFoundException
*/
public XMLInputStream getInputStream(String pName)
throws XMLStreamException, FileNotFoundException
{
log("getInputStream");
XMLInputStreamFactory factory = XMLInputStreamFactory.newInstance();
return factory.newInputStream(new FileInputStream(pName));
}
/**
* <p>Controls the flow of the XML document's overall parsing.</p>
*
* @param pStream XML input stream.
* @exception Exception
* @exception XMLStreamException
*/
public void parse(XMLInputStream pStream)
throws Exception, XMLStreamException
{
log("parse");
// Go to the first start element of the stream.
pStream.skip(XMLEvent.START_ELEMENT);
// Get the current element and move to the next element.
XMLEvent element = pStream.next();
// Validate the name and element type.
if (!element.getName().getLocalName().equals(OrderConstants.ORDER) && !element.isStartElement())
throw new Exception("Cannot handle this doc");
else {
StartElement se = (StartElement)element;
// Instantiate the order business object and set the order number.
orderBO = new OrderBO();
orderBO.setOrderNumber(getAttribute(se, OrderConstants.ORDER_NUMBER));
}
// Process the XML document looking for customer and item list elements.
while(pStream.skip(XMLEvent.START_ELEMENT)) {
XMLName name = pStream.peek().getName();
if (name.getLocalName().equals(OrderConstants.CUSTOMER)) {
processCustomer(pStream.getSubStream());
} else if (name.getLocalName().equals(OrderConstants.ITEM_LIST)) {
processItemList(pStream.getSubStream());
} else {
throw new Exception("Unknown XMLEvent "+name.getLocalName());
}
pStream.skipElement();
}
}
/**
* <p>Controls the flow of the customer parsing resulting
* in a customer business object.</p>
*
* @param pStream XML input stream.
* @exception XMLStreamException
*/
public void processCustomer(XMLInputStream pStream) throws XMLStreamException
{
log("processCustomer");
// Instantiate and load the customer buiness object.
// Iterate through the name and address elements
// extracting known attributes to the customer business object.
CustomerBO customerBO = new CustomerBO();
pStream.skip();
while(pStream.skip(XMLEvent.START_ELEMENT)) {
StartElement se = (StartElement)pStream.peek();
AttributeIterator attrs = (AttributeIterator)se.getAttributes();
while (attrs.hasNext()) {
Attribute attr = (Attribute)attrs.next();
log(attr.getName().getLocalName()+"="+attr.getValue());
if (attr.getName().getLocalName().equals(OrderConstants.FIRST_NAME))
customerBO.setFirstName(attr.getValue());
else if (attr.getName().getLocalName().equals(OrderConstants.LAST_NAME))
customerBO.setLastName(attr.getValue());
else if (attr.getName().getLocalName().equals(OrderConstants.STREET))
customerBO.setStreet(attr.getValue());
else if (attr.getName().getLocalName().equals(OrderConstants.CITY))
customerBO.setCity(attr.getValue());
else if (attr.getName().getLocalName().equals(OrderConstants.STATE))
customerBO.setState(attr.getValue());
else if (attr.getName().getLocalName().equals(OrderConstants.ZIP))
customerBO.setZip(attr.getValue());
}
pStream.skip();
}
// Set the customer business object into the order business object.
orderBO.setCustomer(customerBO);
}
/**
* <p>Controls the flow of items in an item list element.</p>
*
* @param pStream XML input stream.
* @exception XMLStreamException
*/
public void processItemList(XMLInputStream pStream) throws XMLStreamException
{
log("processItemList");
// Look for item start elements.
// Pass stream for item processing.
pStream.skip();
while(pStream.skip(XMLEvent.START_ELEMENT)) {
XMLName name = pStream.peek().getName();
log("name="+name.getLocalName());
if (name.getLocalName().equals(OrderConstants.ITEM))
processItem(pStream.getSubStream());
pStream.skip();
}
}
/**
* <p>Parses item data creating item business objects.</p>
*
* @param pStream XML input stream.
* @exception XMLStreamException
*/
public void processItem(XMLInputStream pStream) throws XMLStreamException
{
log("processItem");
// Instantiate new item business object.
ItemBO itemBO = new ItemBO();
// Extract and load item number attribute.
StartElement se = (StartElement)pStream.peek();
itemBO.setItemNumber(getAttribute(se, OrderConstants.ITEM_NUMBER));
// Extract known character data and load to item business object.
pStream.skip();
while(pStream.skip(XMLEvent.START_ELEMENT)) {
XMLName name = pStream.peek().getName();
if (name.getLocalName().equals(OrderConstants.DESCRIPTION)) {
pStream.skip(XMLEvent.CHARACTER_DATA);
CharacterData cd = (CharacterData)pStream.peek();
if (cd.hasContent()) {
log("content is "+cd.getContent());
itemBO.setDescription(cd.getContent());
}
} else if (name.getLocalName().equals(OrderConstants.QUANTITY)) {
pStream.skip(XMLEvent.CHARACTER_DATA);
CharacterData cd = (CharacterData)pStream.peek();
if (cd.hasContent()) {
log("content is "+cd.getContent());
itemBO.setQuantity(cd.getContent());
}
} else if (name.getLocalName().equals(OrderConstants.PRICE)) {
pStream.skip(XMLEvent.CHARACTER_DATA);
CharacterData cd = (CharacterData)pStream.peek();
if (cd.hasContent()) {
log("content is "+cd.getContent());
itemBO.setPrice(cd.getContent());
}
}
pStream.skip();
}
// Set the item business object into the order business object.
orderBO.setItem(itemBO);
}
/**
* <p>Extract known attribute value by name from a start element.</p>
*
* @param pStream XML input stream.
* @return String Value of attribute.
* @exception XMLStreamException
*/
public String getAttribute(StartElement pStartElement, String pStr) throws XMLStreamException
{
log("getAttribute");
String str = null;
// Get value for known attribute name.
AttributeIterator attrs = (AttributeIterator)pStartElement.getAttributes();
if (attrs.hasNext()) {
Attribute attr = (Attribute)attrs.next();
log(attr.getName().getLocalName()+"="+attr.getValue());
if (attr.getName().getLocalName().equals(pStr))
str = attr.getValue();
}
return str;
}
/**
* <p>Returns created order business object.</p>
*
* @return OrderBO
*/
public OrderBO getOrderBO() { return orderBO; }
// Utility logging.
private void log(String pStr) { if (DEBUG) System.out.println(pStr); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -