本文整理匯總了Java中javax.xml.stream.XMLInputFactory.createXMLEventReader方法的典型用法代碼示例。如果您正苦於以下問題:Java XMLInputFactory.createXMLEventReader方法的具體用法?Java XMLInputFactory.createXMLEventReader怎麽用?Java XMLInputFactory.createXMLEventReader使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.stream.XMLInputFactory
的用法示例。
在下文中一共展示了XMLInputFactory.createXMLEventReader方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testXMLStreamWriter
import javax.xml.stream.XMLInputFactory; //導入方法依賴的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: testUnmarshall
import javax.xml.stream.XMLInputFactory; //導入方法依賴的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"));
}
示例3: testXpathUnmarshaller
import javax.xml.stream.XMLInputFactory; //導入方法依賴的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);
}
示例4: eventReaderTest
import javax.xml.stream.XMLInputFactory; //導入方法依賴的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: getXMLEventReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的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;
}
示例6: test0
import javax.xml.stream.XMLInputFactory; //導入方法依賴的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());
}
}
示例7: readXMLByStAX
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
public static void readXMLByStAX() throws XMLStreamException, FileNotFoundException {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(new FileInputStream("test.xml"));//StaxDemo.class.getResourceAsStream("test.xml")
XMLEvent event;
StringBuffer parsingResult = new StringBuffer();
while (reader.hasNext()) {
event = reader.nextEvent();
if (event.isStartElement()) {
StartElement se = event.asStartElement();
parsingResult.append("<");
parsingResult.append(se.getName());
if (se.getName().getLocalPart().equals("catalog")) {
parsingResult.append("id=");
parsingResult.append(se.getAttributeByName(new QName("id")).getValue());
parsingResult.append("");
}
parsingResult.append(">");
} else if (event.isCharacters()) {
parsingResult.append(event.asCharacters().getData());
} else if (event.isEndElement()) {
parsingResult.append("</");
parsingResult.append(event.asEndElement().getName());
parsingResult.append(">");
}
}
System.out.println(parsingResult);
}
示例8: runReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
public void runReader(XMLInputFactory factory, int offset) throws XMLStreamException {
StringReader stringReader = new StringReader(createXMLDocument(offset));
XMLEventReader reader = factory.createXMLEventReader(stringReader);
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
System.out.println("Event Type: " + event.getEventType());
}
}
示例9: testUnmarshall
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/**
* Test method for SelectResponseUnmarshaller
*/
@Test
public final void testUnmarshall() throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(DomainMetadataResultUnmarshallerTest.class
.getResourceAsStream("SelectResponse.xml"));
StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader);
SelectResponse result = new SelectResponseUnmarshaller().unmarshall(unmarshallerContext);
assertTrue(!result.items().isEmpty());
assertTrue(result.items().size() == 2);
assertTrue(((Item) result.items().get(0)).name().equals("ItemOne"));
assertTrue(((Item) result.items().get(1)).name().equals("ItemTwo"));
}
示例10: testUnmarshall
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/**
* Test method for ListDomainsResponseUnmarshaller
*/
@Test
public final void testUnmarshall() throws Exception {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(DomainMetadataResultUnmarshallerTest.class
.getResourceAsStream("ListDomainsResponse.xml"));
StaxUnmarshallerContext unmarshallerContext = new StaxUnmarshallerContext(eventReader);
ListDomainsResponse result = new ListDomainsResponseUnmarshaller()
.unmarshall(unmarshallerContext);
assertTrue(!result.domainNames().isEmpty());
assertTrue(result.domainNames().size() == 2);
assertTrue(result.domainNames().get(0).equals("DomainOne"));
assertTrue(result.domainNames().get(1).equals("DomainTwo"));
}
示例11: iterator
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/** Returns an Iterator for iterating through the domain objects unmarshalled from the source XML.
* @return an Iterator for iterating through the domain objects unmarshalled from the source XML.
* @throws XMLStreamException if any error occurs during XML parsing.
*/
public Iterator iterator() throws XMLStreamException {
if (m_xer != null)
throw new UnsupportedOperationException("Only one iterator can be obtained from this instance");
XMLInputFactory factory = XMLInputFactory.newInstance();
m_xer = factory.createXMLEventReader(m_xml);
return new XmlIterator();
}
示例12: test2
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
/**
* Verifies that the initial event of an XMLEventReader instance is
* START_DOCUMENT. XMLEventReader depends on XMLStreamReader.
*
* @param xml the xml input
* @param type1 the type of the 1st event
* @param type2 the type of the 2nd event
* @throws Exception if the test fails to run properly
*/
@Test(dataProvider = "xmls")
public static void test2(String xml, int type1, int type2) throws Exception {
XMLInputFactory factory = XMLInputFactory.newFactory();
XMLEventReader reader = factory.createXMLEventReader(new StringReader(xml));
int type1stEvent = reader.nextEvent().getEventType();
int type2ndEvent = reader.nextEvent().getEventType();
System.out.println("First event: " + type1stEvent);
System.out.println("2nd event: " + type2ndEvent);
Assert.assertEquals(type1, type1stEvent);
Assert.assertEquals(type2, type2ndEvent);
}
示例13: testEventReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test(dataProvider = "xml-data")
public void testEventReader(String xml, int chunkSize, int expectedNumOfChunks, boolean withinLimit) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", true);
if (chunkSize > 0) {
xif.setProperty(CDATA_CHUNK_SIZE, chunkSize);
}
XMLEventReader reader = xif.createXMLEventReader(new StringReader(xml));
StringBuilder cdata = new StringBuilder();
int numOfChunks = 0;
boolean isWithinLimit = true;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
//All text events get reported as Characters events
if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
Characters cdataEvent = (Characters) event;
if (cdataEvent.isCData()) {
String text = cdataEvent.getData();
numOfChunks++;
if (text.length() > chunkSize) {
isWithinLimit = false;
}
debugPrint("\nCDATA: " + text.length());
cdata.append(text);
}
}
}
debugPrint("CData in single chunk:" + cdata.toString().length());
Assert.assertEquals(numOfChunks, expectedNumOfChunks);
Assert.assertEquals(isWithinLimit, withinLimit);
}
示例14: getEventReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
private XMLEventReader getEventReader(XMLInputFactory inputFactory, int input) throws Exception {
XMLEventReader er = null;
if (input == ENTITY_INTERNAL_ONLY) {
er = inputFactory.createXMLEventReader(new StringReader(XML));
} else if (input == ENTITY_EXTERNAL_ONLY) {
er = inputFactory.createXMLEventReader(new StringReader(XML1));
} else {
File file = new File(this.getClass().getResource(_file).getFile());
FileInputStream inputStream = new FileInputStream(file);
// XMLStreamReader r = xif.createXMLStreamReader(inputStream);
er = inputFactory.createXMLEventReader(inputStream);
}
return er;
}
示例15: testEventReader
import javax.xml.stream.XMLInputFactory; //導入方法依賴的package包/類
@Test
public void testEventReader() {
try {
XMLInputFactory ifac = XMLInputFactory.newInstance();
XMLEventReader read = ifac.createXMLEventReader(this.getClass().getResource(INPUT_FILE).toExternalForm(),
this.getClass().getResourceAsStream(INPUT_FILE));
while (read.hasNext()) {
XMLEvent event = read.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
if (startElement.getName().getLocalPart().equals("bookurn")) {
Iterator iterator = startElement.getNamespaces();
int count = 0;
while (iterator.hasNext()) {
iterator.next();
count++;
}
Assert.assertTrue(count == 2, "Two namespaces are expected for <bookurn> ");
Iterator attributes = startElement.getAttributes();
count = 0;
while (attributes.hasNext()) {
iterator.next();
count++;
}
Assert.assertTrue(count == 0, "Zero attributes are expected for <bookurn> ");
}
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Exception occured: " + e.getMessage());
}
}