本文整理汇总了Java中javax.xml.stream.events.XMLEvent类的典型用法代码示例。如果您正苦于以下问题:Java XMLEvent类的具体用法?Java XMLEvent怎么用?Java XMLEvent使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XMLEvent类属于javax.xml.stream.events包,在下文中一共展示了XMLEvent类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateStartDocument_DOMWriter
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
/**
* @bug 8139584
* Verifies that the resulting XML contains the standalone setting.
*/
@Test
public void testCreateStartDocument_DOMWriter()
throws ParserConfigurationException, XMLStreamException {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
XMLEventWriter eventWriter = xof.createXMLEventWriter(new DOMResult(doc));
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEvent event = eventFactory.createStartDocument("iso-8859-15", "1.0", true);
eventWriter.add(event);
eventWriter.flush();
Assert.assertEquals(doc.getXmlEncoding(), "iso-8859-15");
Assert.assertEquals(doc.getXmlVersion(), "1.0");
Assert.assertTrue(doc.getXmlStandalone());
}
示例2: getAttributeValue
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
/**
* @param namespaceURI
* @param localName
* @return
*/
public String getAttributeValue(String namespaceURI, String localName) {
//State should be either START_ELEMENT or ATTRIBUTE
if( fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
XMLAttributesImpl attributes = fScanner.getAttributeIterator();
if (namespaceURI == null) { //sjsxp issue 70
return attributes.getValue(attributes.getIndexByLocalName(localName)) ;
} else {
return fScanner.getAttributeIterator().getValue(
namespaceURI.length() == 0 ? null : namespaceURI, localName) ;
}
} else{
throw new java.lang.IllegalStateException("Current state is not among the states "
+ getEventTypeString(XMLEvent.START_ELEMENT) + " , "
+ getEventTypeString(XMLEvent.ATTRIBUTE)
+ "valid for getAttributeValue()") ;
}
}
示例3: fromXml
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
public static TsoFrequencySecurityIndex fromXml(String contingencyId, XMLStreamReader xmlsr) throws XMLStreamException {
String text = null;
while (xmlsr.hasNext()) {
int eventType = xmlsr.next();
switch (eventType) {
case XMLEvent.CHARACTERS:
text = xmlsr.getText();
break;
case XMLEvent.END_ELEMENT:
if ("freq-out-count".equals(xmlsr.getLocalName())) {
return new TsoFrequencySecurityIndex(contingencyId, Integer.parseInt(text));
}
break;
default:
break;
}
}
throw new AssertionError("freq-out-count element not found");
}
示例4: eventReaderTest
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
public void eventReaderTest(XMLInputFactory xif) {
XMLEventReader eventReader = null;
try {
eventReader = xif.createXMLEventReader(this.getClass().getResourceAsStream(INPUT_FILE));
XMLEventReader filteredEventReader = xif.createFilteredReader(eventReader, new EventFilter() {
public boolean accept(XMLEvent event) {
if (!event.isStartElement()) {
return false;
}
QName elementQName = event.asStartElement().getName();
if ((elementQName.getLocalPart().equals(ATTACHMENT_NAME.getLocalPart()) || elementQName.getLocalPart().equals("Attachment"))
&& elementQName.getNamespaceURI().equals(VF_GENERIC_TT_NAMESPACE)) {
return true;
}
return false;
}
});
if (filteredEventReader.hasNext()) {
System.out.println("containsAttachments() returns true");
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
if (eventReader != null) {
try {
eventReader.close();
} catch (XMLStreamException xse) {
// Ignored by intention
}
}
}
}
示例5: nextTag
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
@Override
public final XMLEvent nextTag() throws XMLStreamException {
XMLEvent event = this.nextEvent();
while ((event.isCharacters() && event.asCharacters().isWhiteSpace())
|| event.isProcessingInstruction()
|| event.getEventType() == XMLStreamConstants.COMMENT) {
event = this.nextEvent();
}
if (!event.isStartElement() && event.isEndElement()) {
throw new XMLStreamException("Unexpected event type '" + XMLStreamConstantsUtils.getEventName(event.getEventType()) + "' encountered. Found event: " + event, event.getLocation());
}
return event;
}
示例6: reset
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
public void reset(PropertyManager propertyManager) {
super.reset(propertyManager);
// other settings
fDoctypeName = null;
fDoctypePublicId = null;
fDoctypeSystemId = null;
fSeenDoctypeDecl = false;
fNamespaceContext.reset();
fSupportDTD = ((Boolean)propertyManager.getProperty(XMLInputFactory.SUPPORT_DTD)).booleanValue();
// xerces features
fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue();
setScannerState(XMLEvent.START_DOCUMENT);
setDriver(fXMLDeclDriver);
fSeenInternalSubset = false;
if(fDTDScanner != null){
((XMLDTDScannerImpl)fDTDScanner).reset(propertyManager);
}
fEndPos = 0;
fStartPos = 0;
if(fDTDDecl != null){
fDTDDecl.clear();
}
}
示例7: toDom4jDocument
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
private Document toDom4jDocument(XMLEventReader staxEventReader, Origin origin) {
STAXEventReader dom4jStaxEventReader = new STAXEventReader();
try {
// the dom4j converter class is touchy about comments (aka, comments make it implode)
// so wrap the event stream in a filtering stream to filter out comment events
staxEventReader = new FilteringXMLEventReader( staxEventReader ) {
@Override
protected XMLEvent filterEvent(XMLEvent event, boolean peek) {
return event.getEventType() == XMLStreamConstants.COMMENT
? null
: event;
}
};
return dom4jStaxEventReader.readDocument( staxEventReader );
}
catch (XMLStreamException e) {
throw new InvalidMappingException( "Unable to read StAX source as dom4j Document for processing", origin, e );
}
}
示例8: getElementText
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
public String getElementText() throws XMLStreamException
{
StringBuffer buffer = new StringBuffer();
XMLEvent e = nextEvent();
if (!e.isStartElement())
throw new XMLStreamException(
CommonResourceBundle.getInstance().getString("message.mustBeOnSTART_ELEMENT"));
while(hasNext()) {
e = nextEvent();
if(e.isStartElement())
throw new XMLStreamException(
CommonResourceBundle.getInstance().getString("message.getElementTextExpectTextOnly"));
if(e.isCharacters())
buffer.append(((Characters) e).getData());
if(e.isEndElement())
return buffer.toString();
}
throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.END_ELEMENTnotFound"));
}
示例9: readText
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
/**
* Returns the text contents of the current element being parsed.
*
* @return The text contents of the current element being parsed.
* @throws XMLStreamException
*/
public String readText() throws XMLStreamException {
if (isInsideResponseHeader()) {
return getHeader(currentHeader);
}
if (currentEvent.isAttribute()) {
Attribute attribute = (Attribute)currentEvent;
return attribute.getValue();
}
StringBuilder sb = new StringBuilder();
while (true) {
XMLEvent event = eventReader.peek();
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
eventReader.nextEvent();
sb.append(event.asCharacters().getData());
} else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
return sb.toString();
} else {
throw new RuntimeException("Encountered unexpected event: " + event.toString());
}
}
}
示例10: handleStartDocument
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
private void handleStartDocument(final XMLEvent event) throws SAXException {
_sax.setDocumentLocator(new Locator2() {
public int getColumnNumber() {
return event.getLocation().getColumnNumber();
}
public int getLineNumber() {
return event.getLocation().getLineNumber();
}
public String getPublicId() {
return event.getLocation().getPublicId();
}
public String getSystemId() {
return event.getLocation().getSystemId();
}
public String getXMLVersion(){
return version;
}
public String getEncoding(){
return encoding;
}
});
_sax.startDocument();
}
示例11: testLocation
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
@Test
public void testLocation() {
String XML = "<?xml version='1.0' ?>" + "<!DOCTYPE root [\n" + "<!ENTITY intEnt 'internal'>\n" + "<!ENTITY extParsedEnt SYSTEM 'url:dummy'>\n"
+ "<!NOTATION notation PUBLIC 'notation-public-id'>\n" + "<!NOTATION notation2 SYSTEM 'url:dummy'>\n"
+ "<!ENTITY extUnparsedEnt SYSTEM 'url:dummy2' NDATA notation>\n" + "]>\n" + "<root />";
try {
XMLEventReader er = getReader(XML);
XMLEvent evt = er.nextEvent(); // StartDocument
Location loc1 = evt.getLocation();
System.out.println("Location 1: " + loc1.getLineNumber() + "," + loc1.getColumnNumber());
evt = er.nextEvent(); // DTD
// loc1 should not change so its line number should still be 1
Assert.assertTrue(loc1.getLineNumber() == 1);
Location loc2 = evt.getLocation();
System.out.println("Location 2: " + loc2.getLineNumber() + "," + loc2.getColumnNumber());
evt = er.nextEvent(); // root
System.out.println("Location 1: " + loc1.getLineNumber() + "," + loc1.getColumnNumber());
Assert.assertTrue(loc1.getLineNumber() == 1);
Assert.assertTrue(loc2.getLineNumber() == 7);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
示例12: testCR6419687
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
/**
* Test: 6419687 NPE in XMLEventWriterImpl.
*/
@Test
public void testCR6419687() {
try {
InputStream in = getClass().getResourceAsStream("ReaderToWriterTest.wsdl");
OutputStream out = new FileOutputStream(USER_DIR + "ReaderToWriterTest-out.xml");
XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
writer.add(event);
}
reader.close();
writer.close();
} catch (XMLStreamException xmlStreamException) {
xmlStreamException.printStackTrace();
Assert.fail(xmlStreamException.toString());
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
Assert.fail(fileNotFoundException.toString());
}
}
示例13: getAttributeValue
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
/**
* @param namespaceURI
* @param localName
* @return
*/
public String getAttributeValue(String namespaceURI, String localName) {
//State should be either START_ELEMENT or ATTRIBUTE
if (fEventType == XMLEvent.START_ELEMENT || fEventType == XMLEvent.ATTRIBUTE) {
XMLAttributesImpl attributes = fScanner.getAttributeIterator();
if (namespaceURI == null) { //sjsxp issue 70
return attributes.getValue(attributes.getIndexByLocalName(localName));
} else {
return fScanner.getAttributeIterator().getValue(
namespaceURI.length() == 0 ? null : namespaceURI, localName);
}
} else {
throw new java.lang.IllegalStateException("Current state is not among the states "
+ getEventTypeString(XMLEvent.START_ELEMENT) + " , "
+ getEventTypeString(XMLEvent.ATTRIBUTE)
+ "valid for getAttributeValue()");
}
}
示例14: switchToXML11Scanner
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
private void switchToXML11Scanner() throws IOException{
int oldEntityDepth = fScanner.fEntityDepth;
com.sun.org.apache.xerces.internal.xni.NamespaceContext oldNamespaceContext = fScanner.fNamespaceContext;
fScanner = new XML11NSDocumentScannerImpl();
//get the new scanner state to old scanner's previous state
fScanner.reset(fPropertyManager);
fScanner.setPropertyManager(fPropertyManager);
fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ;
fEntityManager.fCurrentEntity.mayReadChunks = true;
fScanner.setScannerState(XMLEvent.START_DOCUMENT);
fScanner.fEntityDepth = oldEntityDepth;
fScanner.fNamespaceContext = oldNamespaceContext;
fEventType = fScanner.next();
}
示例15: getCharacterData
import javax.xml.stream.events.XMLEvent; //导入依赖的package包/类
/**
* Reads the data from the {@link XMLEvent} and returns it. CData
* will be handled special.
* @param event {@link XMLEvent}
* @param eventReader Reader for Reading {@link XMLEvent}
* @return Character data from element
* @throws XMLStreamException
*/
private String getCharacterData(XMLEvent event, final XMLEventReader eventReader) throws XMLStreamException {
String result = "";
event = eventReader.nextEvent();
if (event instanceof Characters) {
result = event.asCharacters().getData();
}
return result;
}