本文整理汇总了Java中javax.xml.stream.XMLEventReader类的典型用法代码示例。如果您正苦于以下问题:Java XMLEventReader类的具体用法?Java XMLEventReader怎么用?Java XMLEventReader使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
XMLEventReader类属于javax.xml.stream包,在下文中一共展示了XMLEventReader类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testXMLStreamWriter
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
/**
* Test XMLStreamWriter parsing a file with an external entity reference.
*/
@Test
public void testXMLStreamWriter() {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(System.out);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String file = getClass().getResource("XMLEventWriterTest.xml").getPath();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StreamSource(new File(file)));
// adds the event to the consumer.
eventWriter.add(eventReader);
eventWriter.flush();
eventWriter.close();
// expected success
} catch (Exception exception) {
exception.printStackTrace();
Assert.fail(exception.toString());
}
}
示例2: AtomParser
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
public AtomParser(final ContentSource contentSource, final InputStream inputStream) {
super(contentSource, inputStream);
availableSetters = new String [] {"setTitle", "setUpdated", "setLink", "setContent", "setPublished"};
try {
startElementMethods.put(TAG_FEED, ObjectUtil.assertNotNull(getDeclaredMethod("createParsingElement", String.class, XMLEvent.class, XMLEventReader.class), TAG_FEED));
startElementMethods.put(TAG_ENTRY, ObjectUtil.assertNotNull(getDeclaredMethod("pushElementIntoMap", String.class, XMLEvent.class, XMLEventReader.class), TAG_ENTRY));
// used for the feed and entry
startElementMethods.put(TAG_UPDATED, ObjectUtil.assertNotNull(getDeclaredMethod("parseStandardElement", String.class, XMLEvent.class, XMLEventReader.class), TAG_UPDATED));
// used only in entry
startElementMethods.put(TAG_TITLE, ObjectUtil.assertNotNull(getDeclaredMethod("parseStandardElement", String.class, XMLEvent.class, XMLEventReader.class), TAG_TITLE));
startElementMethods.put(TAG_LINK, ObjectUtil.assertNotNull(getDeclaredMethod("parseStandardElement", String.class, XMLEvent.class, XMLEventReader.class), TAG_LINK));
startElementMethods.put(TAG_CONTENT, ObjectUtil.assertNotNull(getDeclaredMethod("parseStandardElement", String.class, XMLEvent.class, XMLEventReader.class), TAG_CONTENT));
startElementMethods.put(TAG_PUBLISHED, ObjectUtil.assertNotNull(getDeclaredMethod("parseStandardElement", String.class, XMLEvent.class, XMLEventReader.class), TAG_PUBLISHED));
endElementMehods.put(TAG_ENTRY, ObjectUtil.assertNotNull(getDeclaredMethod("popElementFromStack"), TAG_ENTRY));
specialMethods.put(TAG_ENTRY, ObjectUtil.assertNotNull(getDeclaredMethod("createItemObject"), TAG_ENTRY));
} catch (final SecurityException e) {
LOGGER.error("Failed creating {} because", getClass(), e );
}
}
示例3: testEventReader2
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
@Test
public void testEventReader2() {
try {
// Now advance underlying reader and then call peek on event reader
XMLStreamReader s1 = inputFactory.createXMLStreamReader(new java.io.StringReader(xml));
Assert.assertEquals(s1.getEventType(), XMLStreamConstants.START_DOCUMENT);
s1.next();
s1.next(); // advance to <TITLE>
Assert.assertTrue(s1.getLocalName().equals("TITLE"));
XMLEventReader e3 = inputFactory.createXMLEventReader(s1);
Assert.assertEquals(e3.peek().getEventType(), XMLStreamConstants.START_ELEMENT);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
示例4: test
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
@Test
public void test() {
String xmlData = "<?xml version=\"1.0\"?><Test>Hello</Test>";
try {
XMLEventReader xmlReader = XMLInputFactory.newInstance().createXMLEventReader(new ByteArrayInputStream(xmlData.getBytes()));
XMLEvent event = xmlReader.nextEvent();
System.out.println(event.getClass());
// xmlReader.peek(); // error in both cases with/without peek()
event = xmlReader.nextTag(); // nextEvent() would work fine
// nextTag() forgets to set fLastEvent
System.out.println(event.getClass());
String text = xmlReader.getElementText();
System.out.println(text);
} catch (XMLStreamException e) {
Assert.fail(e.getMessage());
}
}
示例5: unmarshal
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
public JaxbRoot unmarshal(InputStream stream, Origin origin) {
try {
XMLEventReader staxReader = staxFactory().createXMLEventReader( stream );
try {
return unmarshal( staxReader, origin );
}
finally {
try {
staxReader.close();
}
catch ( Exception ignore ) {
}
}
}
catch ( XMLStreamException e ) {
throw new MappingException( "Unable to create stax reader", e, origin );
}
}
示例6: unmarshal
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
public JaxbHibernateConfiguration unmarshal(InputStream stream, Origin origin) {
try {
XMLEventReader staxReader = staxFactory().createXMLEventReader( stream );
try {
return unmarshal( staxReader, origin );
}
finally {
try {
staxReader.close();
}
catch ( Exception ignore ) {
}
}
}
catch ( XMLStreamException e ) {
throw new MappingException( "Unable to create stax reader", e, origin );
}
}
示例7: testCR6419687
import javax.xml.stream.XMLEventReader; //导入依赖的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());
}
}
示例8: toDom4jDocument
import javax.xml.stream.XMLEventReader; //导入依赖的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 );
}
}
示例9: createXMLEventReader
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
/**
* Method checks if the storage type is supported and transforms it to XMLEventReader instance which is then returned.
* Throws PolicyException if the transformation is not succesfull or if the storage type is not supported.
*
* @param storage An XMLEventReader instance.
* @return The storage cast to an XMLEventReader.
* @throws PolicyException If the XMLEventReader cast failed.
*/
private XMLEventReader createXMLEventReader(final Object storage)
throws PolicyException {
if (storage instanceof XMLEventReader) {
return (XMLEventReader) storage;
}
else if (!(storage instanceof Reader)) {
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0022_STORAGE_TYPE_NOT_SUPPORTED(storage.getClass().getName())));
}
try {
return XMLInputFactory.newInstance().createXMLEventReader((Reader) storage);
} catch (XMLStreamException e) {
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0014_UNABLE_TO_INSTANTIATE_READER_FOR_STORAGE(), e));
}
}
示例10: parse
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
public TubelineFeature parse(XMLEventReader reader) throws WebServiceException {
try {
final StartElement element = reader.nextEvent().asStartElement();
boolean attributeEnabled = true;
final Iterator iterator = element.getAttributes();
while (iterator.hasNext()) {
final Attribute nextAttribute = (Attribute) iterator.next();
final QName attributeName = nextAttribute.getName();
if (ENABLED_ATTRIBUTE_NAME.equals(attributeName)) {
attributeEnabled = ParserUtil.parseBooleanValue(nextAttribute.getValue());
} else if (NAME_ATTRIBUTE_NAME.equals(attributeName)) {
// TODO use name attribute
} else {
// TODO logging message
throw LOGGER.logSevereException(new WebServiceException("Unexpected attribute"));
}
}
return parseFactories(attributeEnabled, element, reader);
} catch (XMLStreamException e) {
throw LOGGER.logSevereException(new WebServiceException("Failed to unmarshal XML document", e));
}
}
示例11: eventReaderTest
import javax.xml.stream.XMLEventReader; //导入依赖的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
}
}
}
}
示例12: getXMLEventReader
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
private XMLEventReader getXMLEventReader(final String filename) {
XMLInputFactory xmlif = null;
XMLEventReader xmlr = null;
try {
xmlif = XMLInputFactory.newInstance();
xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
// FileInputStream fis = new FileInputStream(filename);
FileInputStream fis = new FileInputStream(new File(ValidatorTest.class.getResource(filename).toURI()));
xmlr = xmlif.createXMLEventReader(filename, fis);
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail("Exception : " + ex.getMessage());
}
return xmlr;
}
示例13: test0
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
@Test
public void test0() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(getClass().getResource("sgml.xml").toString(), getClass().getResourceAsStream("sgml.xml"));
XMLEventReader xer = xif.createXMLEventReader(xsr);
Assert.assertTrue(xer.peek().getEventType() == XMLEvent.START_DOCUMENT);
Assert.assertTrue(xer.peek() == xer.nextEvent());
xsr.close();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
示例14: testUnmarshall
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
/**
* Test method for GetAttributesResponseUnmarshaller
*/
@Test
public final void testUnmarshall() throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(DomainMetadataResultUnmarshallerTest.class
.getResourceAsStream("GetAttributesResponse.xml"));
StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader);
GetAttributesResponse result = new GetAttributesResponseUnmarshaller()
.unmarshall(unmarshallerContext);
assertTrue(!result.attributes().isEmpty());
assertTrue(result.attributes().size() == 2);
assertTrue(result.attributes().get(0).name().equals("Color"));
assertTrue(result.attributes().get(0).value().equals("Blue"));
assertTrue(result.attributes().get(1).name().equals("Price"));
assertTrue(result.attributes().get(1).value().equals("$2.50"));
}
示例15: testXpathUnmarshaller
import javax.xml.stream.XMLEventReader; //导入依赖的package包/类
/**
* Test method for DomainMetadataResultXpathUnmarshaller
*/
@Test
public final void testXpathUnmarshaller() throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(DomainMetadataResultUnmarshallerTest.class
.getResourceAsStream("DomainMetadataResponse.xml"));
StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader);
DomainMetadataResponse result = new DomainMetadataResponseUnmarshaller()
.unmarshall(unmarshallerContext);
assertTrue(result.itemCount() == 25);
assertTrue(result.itemNamesSizeBytes() == 12345);
assertTrue(result.attributeNameCount() == 20);
assertTrue(result.attributeNamesSizeBytes() == 2345);
assertTrue(result.attributeValueCount() == 25);
assertTrue(result.attributeValuesSizeBytes() == 1234);
assertTrue(result.timestamp() == 5555);
}