本文整理汇总了Java中javax.xml.stream.events.XMLEvent.asStartElement方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEvent.asStartElement方法的具体用法?Java XMLEvent.asStartElement怎么用?Java XMLEvent.asStartElement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.stream.events.XMLEvent
的用法示例。
在下文中一共展示了XMLEvent.asStartElement方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addRdfIdValues
import javax.xml.stream.events.XMLEvent; //导入方法依赖的package包/类
private void addRdfIdValues(InputStream is, Set<String> rdfIdValues) throws XMLStreamException {
// memoize RDF ID values of the document
XMLEventReader eventReader = xmlStaxContext.inputFactory.createXMLEventReader(is);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
Iterator it = startElement.getAttributes();
while (it.hasNext()) {
Attribute attribute = (Attribute) it.next();
QName name = attribute.getName();
if (RDF_ID.equals(name)) {
rdfIdValues.add(attribute.getValue());
}
}
}
}
eventReader.close();
}
示例2: readXMLByStAX
import javax.xml.stream.events.XMLEvent; //导入方法依赖的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);
}
示例3: add
import javax.xml.stream.events.XMLEvent; //导入方法依赖的package包/类
@Override
public void add(XMLEvent event) throws XMLStreamException {
XMLEvent newEvent = null;
if (event.isStartElement()) {
StartElement startElement = event.asStartElement();
newEvent = anonymizeStartElement(startElement);
} else if (event.isCharacters()) {
Characters characters = event.asCharacters();
newEvent = anonymizeCharacters(characters);
}
super.add(newEvent != null ? newEvent : event);
}
示例4: unmarshal
import javax.xml.stream.events.XMLEvent; //导入方法依赖的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;
}
示例5: unmarshalNodeContent
import javax.xml.stream.events.XMLEvent; //导入方法依赖的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();
}
示例6: testEventReader
import javax.xml.stream.events.XMLEvent; //导入方法依赖的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());
}
}
示例7: set
import javax.xml.stream.events.XMLEvent; //导入方法依赖的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 ();
}
}
}
示例8: readTestResults
import javax.xml.stream.events.XMLEvent; //导入方法依赖的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;
}
示例9: parse
import javax.xml.stream.events.XMLEvent; //导入方法依赖的package包/类
public void parse(XMLEventReader input) throws XMLStreamException, XNIException {
XMLEvent currentEvent = input.peek();
if (currentEvent != null) {
int eventType = currentEvent.getEventType();
if (eventType != XMLStreamConstants.START_DOCUMENT &&
eventType != XMLStreamConstants.START_ELEMENT) {
throw new XMLStreamException();
}
fLocationWrapper.setLocation(currentEvent.getLocation());
fSchemaDOMParser.startDocument(fLocationWrapper, null, fNamespaceContext, null);
loop: while (input.hasNext()) {
currentEvent = input.nextEvent();
eventType = currentEvent.getEventType();
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
++fDepth;
StartElement start = currentEvent.asStartElement();
fillQName(fElementQName, start.getName());
fLocationWrapper.setLocation(start.getLocation());
fNamespaceContext.setNamespaceContext(start.getNamespaceContext());
fillXMLAttributes(start);
fillDeclaredPrefixes(start);
addNamespaceDeclarations();
fNamespaceContext.pushContext();
fSchemaDOMParser.startElement(fElementQName, fAttributes, null);
break;
case XMLStreamConstants.END_ELEMENT:
EndElement end = currentEvent.asEndElement();
fillQName(fElementQName, end.getName());
fillDeclaredPrefixes(end);
fLocationWrapper.setLocation(end.getLocation());
fSchemaDOMParser.endElement(fElementQName, null);
fNamespaceContext.popContext();
--fDepth;
if (fDepth <= 0) {
break loop;
}
break;
case XMLStreamConstants.CHARACTERS:
sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
break;
case XMLStreamConstants.SPACE:
sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), true);
break;
case XMLStreamConstants.CDATA:
fSchemaDOMParser.startCDATA(null);
sendCharactersToSchemaParser(currentEvent.asCharacters().getData(), false);
fSchemaDOMParser.endCDATA(null);
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
ProcessingInstruction pi = (ProcessingInstruction)currentEvent;
fillProcessingInstruction(pi.getData());
fSchemaDOMParser.processingInstruction(pi.getTarget(), fTempString, null);
break;
case XMLStreamConstants.DTD:
/* There shouldn't be a DTD in the schema */
break;
case XMLStreamConstants.ENTITY_REFERENCE:
/* Not needed for schemas */
break;
case XMLStreamConstants.COMMENT:
/* No point in sending comments */
break;
case XMLStreamConstants.START_DOCUMENT:
fDepth++;
/* We automatically call startDocument before the loop */
break;
case XMLStreamConstants.END_DOCUMENT:
/* We automatically call endDocument after the loop */
break;
}
}
fLocationWrapper.setLocation(null);
fNamespaceContext.setNamespaceContext(null);
fSchemaDOMParser.endDocument(null);
}
}