本文整理汇总了Java中javax.xml.stream.XMLEventReader.hasNext方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEventReader.hasNext方法的具体用法?Java XMLEventReader.hasNext怎么用?Java XMLEventReader.hasNext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.XMLEventReader
的用法示例。
在下文中一共展示了XMLEventReader.hasNext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
}
示例2: 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
}
}
}
}
示例3: readXMLByStAX
import javax.xml.stream.XMLEventReader; //导入方法依赖的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);
}
示例4: handleObject
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
private Object handleObject(XMLEventReader parser,
XMLEvent startEvent,
ExtensionRegistry extensionRegistry,
Message.Builder builder,
FieldDescriptor field,
ExtensionRegistry.ExtensionInfo extension) throws XMLStreamException {
Message.Builder subBuilder = createSubBuilder(builder, field, extension);
XMLEvent event = startEvent;
int depth = 0; // initialize to 0
do {
if (event.isStartElement()) {
depth++; // we're inside the element
mergeField(parser, event,
extensionRegistry, subBuilder);
XMLEvent nextEvent = parser.nextTag();
if (nextEvent.isEndElement()) {
depth--;
// if we're back under the top level obj, and there is another close, we're done.
if (depth <= 0 && parser.peek().isEndElement()) {
break;
}
} else if (nextEvent.isStartElement()) {
depth++;
}
} else {
// something is potentially wrong..
break;
}
} while (parser.hasNext() && (event = parser.nextTag()) != null);
return subBuilder.build();
}
示例5: test
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
@Test
public void test() {
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLEventReader reader = xif.createXMLEventReader(this.getClass().getResourceAsStream("xml11.xml.data"));
while (reader.hasNext())
reader.next();
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
示例6: testEventReader
import javax.xml.stream.XMLEventReader; //导入方法依赖的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);
}
示例7: runReader
import javax.xml.stream.XMLEventReader; //导入方法依赖的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());
}
}
示例8: unmarshalModel
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
/**
* See {@link PolicyModelUnmarshaller#unmarshalModel(Object) base method documentation}.
*/
public PolicySourceModel unmarshalModel(final Object storage) throws PolicyException {
final XMLEventReader reader = createXMLEventReader(storage);
PolicySourceModel model = null;
loop:
while (reader.hasNext()) {
try {
final XMLEvent event = reader.peek();
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.COMMENT:
reader.nextEvent();
break; // skipping the comments and start document events
case XMLStreamConstants.CHARACTERS:
processCharacters(ModelNode.Type.POLICY, event.asCharacters(), null);
// we advance the reader only if there is no exception thrown from
// the processCharacters(...) call. Otherwise we don't modify the stream
reader.nextEvent();
break;
case XMLStreamConstants.START_ELEMENT:
if (NamespaceVersion.resolveAsToken(event.asStartElement().getName()) == XmlToken.Policy) {
StartElement rootElement = reader.nextEvent().asStartElement();
model = initializeNewModel(rootElement);
unmarshalNodeContent(model.getNamespaceVersion(), model.getRootNode(), rootElement.getName(), reader);
break loop;
} else {
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0048_POLICY_ELEMENT_EXPECTED_FIRST()));
}
default:
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0048_POLICY_ELEMENT_EXPECTED_FIRST()));
}
} catch (XMLStreamException e) {
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e));
}
}
return model;
}
示例9: unmarshalNodeContent
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
private String unmarshalNodeContent(final NamespaceVersion nsVersion, final ModelNode node, final QName nodeElementName, final XMLEventReader reader) throws PolicyException {
StringBuilder valueBuffer = null;
loop:
while (reader.hasNext()) {
try {
final XMLEvent xmlParserEvent = reader.nextEvent();
switch (xmlParserEvent.getEventType()) {
case XMLStreamConstants.COMMENT:
break; // skipping the comments
case XMLStreamConstants.CHARACTERS:
valueBuffer = processCharacters(node.getType(), xmlParserEvent.asCharacters(), valueBuffer);
break;
case XMLStreamConstants.END_ELEMENT:
checkEndTagName(nodeElementName, xmlParserEvent.asEndElement());
break loop; // data exctraction for currently processed policy node is done
case XMLStreamConstants.START_ELEMENT:
final StartElement childElement = xmlParserEvent.asStartElement();
ModelNode childNode = addNewChildNode(nsVersion, node, childElement);
String value = unmarshalNodeContent(nsVersion, childNode, childElement.getName(), reader);
if (childNode.isDomainSpecific()) {
parseAssertionData(nsVersion, value, childNode, childElement);
}
break;
default:
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0011_UNABLE_TO_UNMARSHALL_POLICY_XML_ELEM_EXPECTED()));
}
} catch (XMLStreamException e) {
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0068_FAILED_TO_UNMARSHALL_POLICY_EXPRESSION(), e));
}
}
return (valueBuffer == null) ? null : valueBuffer.toString().trim();
}
示例10: checkOutput
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
private void checkOutput(String output) throws Exception {
InputStream in = new FileInputStream(output);
XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
while (reader.hasNext()) {
reader.next();
}
reader.close();
}
示例11: writeEvents
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
private void writeEvents(XMLEventReader reader, XMLEventWriter writer) throws XMLStreamException {
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
writer.add(event);
}
reader.close();
writer.close();
}
示例12: unmarshal
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
private Map<URI, Policy> unmarshal(final XMLEventReader reader, final StartElement parentElement) throws PolicyException {
XMLEvent event = null;
while (reader.hasNext()) {
try {
event = reader.peek();
switch (event.getEventType()) {
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.COMMENT:
reader.nextEvent();
break;
case XMLStreamConstants.CHARACTERS:
processCharacters(event.asCharacters(), parentElement, map);
reader.nextEvent();
break;
case XMLStreamConstants.END_ELEMENT:
processEndTag(event.asEndElement(), parentElement);
reader.nextEvent();
return map;
case XMLStreamConstants.START_ELEMENT:
final StartElement element = event.asStartElement();
processStartTag(element, parentElement, reader, map);
break;
case XMLStreamConstants.END_DOCUMENT:
return map;
default:
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0087_UNKNOWN_EVENT(event)));
}
} catch (XMLStreamException e) {
final Location location = event == null ? null : event.getLocation();
throw LOGGER.logSevereException(new PolicyException(LocalizationMessages.WSP_0088_FAILED_PARSE(location)), e);
}
}
return map;
}
示例13: testMerge
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
/**
* Inspired by CR 6245284 Sun Stax /sjsxp.jar does not behave properly
* during merge of xml files.
*/
@Test
public void testMerge() {
try {
// Create the XML input factory
XMLInputFactory factory = XMLInputFactory.newInstance();
// Create XML event reader 1
InputStream inputStream1 = new FileInputStream(new File(XMLEventWriterTest.class.getResource("merge-1.xml").toURI()));
XMLEventReader r1 = factory.createXMLEventReader(inputStream1);
// Create XML event reader 2
InputStream inputStream2 = new FileInputStream(new File(XMLEventWriterTest.class.getResource("merge-2.xml").toURI()));
XMLEventReader r2 = factory.createXMLEventReader(inputStream2);
// Create the output factory
XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
// Create XML event writer
XMLEventWriter xmlw = xmlof.createXMLEventWriter(System.out);
// Read to first <product> element in document 1
// and output to result document
QName bName = new QName("b");
while (r1.hasNext()) {
// Read event to be written to result document
XMLEvent event = r1.nextEvent();
if (event.getEventType() == XMLEvent.END_ELEMENT) {
// Start element - stop at <product> element
QName name = event.asEndElement().getName();
if (name.equals(bName)) {
QName zName = new QName("z");
boolean isZr = false;
while (r2.hasNext()) {
// Read event to be written to result document
XMLEvent event2 = r2.nextEvent();
// Output event
if (event2.getEventType() == XMLEvent.START_ELEMENT && event2.asStartElement().getName().equals(zName)) {
isZr = true;
}
if (xmlw != null && isZr) {
xmlw.add(event2);
}
// stop adding events after </z>
// i.e. do not write END_DOCUMENT :)
if (isZr && event2.getEventType() == XMLEvent.END_ELEMENT && event2.asEndElement().getName().equals(zName)) {
isZr = false;
}
}
xmlw.flush();
}
}
// Output event
if (xmlw != null) {
xmlw.add(event);
}
}
// Read to first <product> element in document 1
// without writing to result document
xmlw.close();
// expected success
} catch (Exception ex) {
ex.printStackTrace();
Assert.fail(ex.toString());
}
}
示例14: set
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
public void set ( final String expression, final int targetElement, final String value ) throws IOException, XMLStreamException
{
int pos = 0;
final Stack<String> ele = new Stack<> ();
final Pattern pattern = Pattern.compile ( expression );
final XMLEventReader reader;
Modification mod = null;
final XMLInputFactory xmlinf = XMLInputFactory.newInstance ();
try (final FileInputStream is = new FileInputStream ( this.inputFile ))
{
reader = xmlinf.createXMLEventReader ( is );
try
{
while ( reader.hasNext () )
{
final XMLEvent event = (XMLEvent)reader.next ();
if ( event instanceof StartElement )
{
final StartElement startEvent = event.asStartElement ();
ele.push ( startEvent.getName ().getLocalPart () );
}
else if ( event instanceof EndElement )
{
final EndElement endEvent = event.asEndElement ();
final String en = ele.pop ();
if ( !endEvent.getName ().getLocalPart ().equals ( en ) )
{
throw new IllegalStateException ( "Element error: " + en + " / " + endEvent.getName () );
}
}
boolean inMatch = mod != null;
final boolean match = isMatch ( ele, pattern );
if ( inMatch && !match )
{
// end
if ( pos == targetElement )
{
this.mods.add ( mod );
}
pos++;
mod = null;
}
else if ( !inMatch && match )
{
// starting
mod = new Modification ();
mod.start = event.getLocation ().getCharacterOffset ();
mod.data = value;
}
else if ( inMatch && match )
{
mod.end = event.getLocation ().getCharacterOffset () - 2;
}
inMatch = match;
}
}
finally
{
reader.close ();
}
}
}
示例15: readTestResults
import javax.xml.stream.XMLEventReader; //导入方法依赖的package包/类
private static Collection<TestResult> readTestResults(XMLEventReader eventReader) throws XMLStreamException {
final Set<TestResult> testResults = new HashSet<>();
TestResult currentTestResult = null;
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
final StartElement startElement = event.asStartElement();
if ("testcase".equalsIgnoreCase(startElement.getName().getLocalPart())) {
// Read attributes
String name = null, classname = null;
Status status = null;
final Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
final Attribute attribute = attributes.next();
if ("classname".equalsIgnoreCase(attribute.getName().toString())) {
classname = attribute.getValue();
}
if ("name".equalsIgnoreCase(attribute.getName().toString())) {
name = attribute.getValue();
}
}
currentTestResult = new TestResult(classname, name, Status.PASSED);
}
if ("failure".equalsIgnoreCase(startElement.getName().getLocalPart())) {
currentTestResult.setStatus(Status.FAILURE);
}
if ("error".equalsIgnoreCase(startElement.getName().getLocalPart())) {
currentTestResult.setStatus(Status.ERROR);
}
if ("skipped".equalsIgnoreCase(startElement.getName().getLocalPart())) {
currentTestResult.setStatus(Status.SKIPPED);
}
if ("rerunFailure".equalsIgnoreCase(startElement.getName().getLocalPart())) {
currentTestResult.setStatus(Status.RE_RUN_FAILURE);
}
}
if (event.isEndElement()) {
final EndElement endElementElement = event.asEndElement();
if ("testcase".equalsIgnoreCase(endElementElement.getName().getLocalPart())) {
testResults.add(currentTestResult);
}
}
}
return testResults;
}