javax.xml.stream.XMLStreamReader.nextTag()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(19.9k)|赞(0)|评价(0)|浏览(83)

本文整理了Java中javax.xml.stream.XMLStreamReader.nextTag()方法的一些代码示例,展示了XMLStreamReader.nextTag()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XMLStreamReader.nextTag()方法的具体详情如下:
包路径:javax.xml.stream.XMLStreamReader
类名称:XMLStreamReader
方法名:nextTag

XMLStreamReader.nextTag介绍

[英]Skips any white space (isWhiteSpace() returns true), COMMENT, or PROCESSING_INSTRUCTION, until a START_ELEMENT or END_ELEMENT is reached. If other than white space characters, COMMENT, PROCESSING_INSTRUCTION, START_ELEMENT, END_ELEMENT are encountered, an exception is thrown. This method should be used when processing element-only content separated by white space.
Precondition: none
Postcondition: the current event is START_ELEMENT or END_ELEMENT and cursor may have moved over any whitespace event.
Essentially it does the following (implementations are free to optimize but must do equivalent processing):

int eventType = next(); 
// skip whitespace 
while((eventType == XMLStreamConstants.CHARACTERS  
&& isWhiteSpace()) 
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())  
// skip whitespace 
|| eventType == XMLStreamConstants.SPACE 
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION 
|| eventType == XMLStreamConstants.COMMENT) { 
eventType = next(); 
} 
if (eventType != XMLStreamConstants.START_ELEMENT  
&& eventType != XMLStreamConstants.END_ELEMENT) { 
throw new String XMLStreamException( 
"expected start or end tag", getLocation()); 
} 
return eventType;

[中]跳过任何空白(isWhiteSpace()返回true、注释或处理_指令,直到到达开始_元素或结束_元素。如果遇到除空白字符、注释、处理_指令、开始_元素、结束_元素以外的其他字符,则会引发异常。当处理由空白分隔的纯元素内容时,应使用此方法。
前提条件:无
后置条件:当前事件是START_元素或END_元素,光标可能已移动到任何空白事件上。
从本质上讲,它可以做到以下几点(实现可以自由优化,但必须进行等效的处理):

int eventType = next(); 
// skip whitespace 
while((eventType == XMLStreamConstants.CHARACTERS  
&& isWhiteSpace()) 
|| (eventType == XMLStreamConstants.CDATA && isWhiteSpace())  
// skip whitespace 
|| eventType == XMLStreamConstants.SPACE 
|| eventType == XMLStreamConstants.PROCESSING_INSTRUCTION 
|| eventType == XMLStreamConstants.COMMENT) { 
eventType = next(); 
} 
if (eventType != XMLStreamConstants.START_ELEMENT  
&& eventType != XMLStreamConstants.END_ELEMENT) { 
throw new String XMLStreamException( 
"expected start or end tag", getLocation()); 
} 
return eventType;

代码示例

代码示例来源:origin: graphhopper/graphhopper

protected void readFileHeader(XMLStreamReader parser) throws XMLStreamException {
  int event = parser.getEventType();
  while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("osm")) {
    event = parser.nextTag();
  }
}

代码示例来源:origin: graphhopper/graphhopper

private static void readTags(ReaderElement re, XMLStreamReader parser) throws XMLStreamException {
  int event = parser.getEventType();
  while (event != XMLStreamConstants.END_DOCUMENT && parser.getLocalName().equals("tag")) {
    if (event == XMLStreamConstants.START_ELEMENT) {
      // read tag
      String key = parser.getAttributeValue(null, "k");
      String value = parser.getAttributeValue(null, "v");
      // ignore tags with empty values
      if (value != null && value.length() > 0)
        re.setTag(key, value);
    }
    event = parser.nextTag();
  }
}

代码示例来源:origin: graphhopper/graphhopper

public static ReaderNode createNode(long id, XMLStreamReader parser) throws XMLStreamException {
  ReaderNode node = new ReaderNode(id,
      Double.parseDouble(parser.getAttributeValue(null, "lat")),
      Double.parseDouble(parser.getAttributeValue(null, "lon")));
  parser.nextTag();
  readTags(node, parser);
  return node;
}

代码示例来源:origin: wildfly/wildfly

private void parseAttribute(final XMLStreamReader streamReader, final Attributes attributes) throws XMLStreamException, RealmUnavailableException {
  String name = null;
  String value = null;
  final int attributeCount = streamReader.getAttributeCount();
  for (int i = 0; i < attributeCount; i++) {
    String namespace = streamReader.getAttributeNamespace(i);
    if (namespace != null && !namespace.equals("")) {
      throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), this.name);
    }
    if ("name".equals(streamReader.getAttributeLocalName(i))) {
      name = streamReader.getAttributeValue(i);
    } else if ("value".equals(streamReader.getAttributeLocalName(i))) {
      value = streamReader.getAttributeValue(i);
    } else {
      throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), this.name);
    }
  }
  if (name == null) {
    throw ElytronMessages.log.fileSystemRealmMissingAttribute("name", path, streamReader.getLocation().getLineNumber(), this.name);
  }
  if (value == null) {
    throw ElytronMessages.log.fileSystemRealmMissingAttribute("value", path, streamReader.getLocation().getLineNumber(), this.name);
  }
  attributes.addLast(name, value);
  if (streamReader.nextTag() != END_ELEMENT) {
    throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), this.name);
  }
}

代码示例来源:origin: org.teiid/teiid-client

public static PlanNode fromXml(String planString) throws XMLStreamException {
  XMLInputFactory inputFactory = XMLType.getXmlInputFactory();
  XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(planString));
  while (reader.hasNext()&& (reader.nextTag() != XMLStreamConstants.END_ELEMENT)) {
    String element = reader.getLocalName();
    if (element.equals("node")) { //$NON-NLS-1$
      Properties props = getAttributes(reader);
      PlanNode planNode = new PlanNode(props.getProperty("name"));//$NON-NLS-1$
      planNode.setParent(null);
      buildNode(reader, planNode);
      return planNode;
    }
    throw new XMLStreamException(JDBCPlugin.Util.gs("unexpected_element", reader.getName(), "node"),reader.getLocation());//$NON-NLS-1$ //$NON-NLS-2$
  }
  return null;
}

代码示例来源:origin: wildfly/wildfly

private LoadedIdentity parseIdentity(final XMLStreamReader streamReader, final boolean skipCredentials, final boolean skipAttributes) throws RealmUnavailableException, XMLStreamException {
  final int tag = streamReader.nextTag();
  if (tag != START_ELEMENT || ! validNamespace(streamReader.getNamespaceURI()) || ! "identity".equals(streamReader.getLocalName())) {
    throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);
  }
  return parseIdentityContents(streamReader, skipCredentials, skipAttributes);
}

代码示例来源:origin: org.wildfly.core/wildfly-server

private static void parseNoContent(final XMLStreamReader reader) throws XMLStreamException {
  while (reader.hasNext()) {
    switch (reader.nextTag()) {
      case XMLStreamConstants.END_ELEMENT: {
        return;
      }
      default: {
        throw unexpectedContent(reader);
      }
    }
  }
  throw endOfDocument(reader.getLocation());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void readStAXSource() throws Exception {
  MockHttpInputMessage inputMessage = new MockHttpInputMessage(BODY.getBytes("UTF-8"));
  inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
  StAXSource result = (StAXSource) converter.read(StAXSource.class, inputMessage);
  XMLStreamReader streamReader = result.getXMLStreamReader();
  assertTrue(streamReader.hasNext());
  streamReader.nextTag();
  String s = streamReader.getLocalName();
  assertEquals("root", s);
  s = streamReader.getElementText();
  assertEquals("Hello World", s);
  streamReader.close();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void partial() throws Exception {
  XMLInputFactory inputFactory = XMLInputFactory.newInstance();
  XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(CONTENT));
  streamReader.nextTag();  // skip to root
  assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "root"),
      streamReader.getName());
  streamReader.nextTag();  // skip to child
  assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "child"),
      streamReader.getName());
  StaxStreamXMLReader xmlReader = new StaxStreamXMLReader(streamReader);
  ContentHandler contentHandler = mock(ContentHandler.class);
  xmlReader.setContentHandler(contentHandler);
  xmlReader.parse(new InputSource());
  verify(contentHandler).setDocumentLocator(any(Locator.class));
  verify(contentHandler).startDocument();
  verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
  verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
  verify(contentHandler).endDocument();
}

代码示例来源:origin: org.wildfly.core/wildfly-server

private static Set<String> parseSet(final XMLStreamReader reader) throws XMLStreamException {
  final Set<String> set = new HashSet<String>();
  // xsd:choice
  while (reader.hasNext()) {
    switch (reader.nextTag()) {
      case END_ELEMENT: {
        return set;
      }
      case START_ELEMENT: {
        switch (Element.of(reader.getName())) {
          case PATH:
            parsePathName(reader, set);
            break;
        }
      }
    }
  }
  return set;
}

代码示例来源:origin: org.jboss.osgi.repository/jbosgi-repository-core

private void readDirectiveElement(XMLStreamReader reader, Map<String, String> directives) throws XMLStreamException {
    String name = reader.getAttributeValue(null, Attribute.NAME.toString());
    String value = reader.getAttributeValue(null, Attribute.VALUE.toString());
    directives.put(name, value);
    while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
    }
  }
}

代码示例来源:origin: wildfly/wildfly

WorkManagerSecurity security = null;
while (reader.hasNext()) {
  switch (reader.nextTag()) {
    case END_ELEMENT: {
      if (Activation.Tag.forName(reader.getLocalName()) == Activation.Tag.WORKMANAGER) {
        return new WorkManagerImpl(security);
      } else {
        if (Activation.Tag.forName(reader.getLocalName()) == Activation.Tag.UNKNOWN) {
          throw new ParserException(bundle.unexpectedEndTag(reader.getLocalName()));

代码示例来源:origin: stackoverflow.com

resultsBuff.append("\nEdited at line " + delta.getRevised().getPosition() + "\n");
     for (Object line : delta.getRevised().getLines())
       {
       String xmlLineEdit = line.toString();//save 'original' xml line into string
     XMLInputFactory xif = XMLInputFactory.newFactory();   
     XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xmlLineEdit));//put xml into parser
     xsr.nextTag(); // Advance to svg element
     int attributeCount = xsr.getAttributeCount();//get the number of attributes
     array1 = new String[attributeCount];
     for(int x=0; x<attributeCount; x++) //for each attribute
       {
     //StringBuilder stringBuilder = new StringBuilder();//might remove
     array1[x]= xsr.getAttributeLocalName(x) + "=\"" + xsr.getAttributeValue(x) + "\"";//add attributes and values to an array
       }
     Arrays.sort(array1);//sort the array alphabetically

代码示例来源:origin: org.jboss.metadata/jboss-metadata-appclient

public ApplicationClientMetaData parse(XMLStreamReader reader, PropertyReplacer propertyReplacer) throws XMLStreamException {
  reader.require(START_DOCUMENT, null, null);
  // Read until the first start element
  while (reader.hasNext() && reader.next() != START_ELEMENT) {}
  final ApplicationClientMetaData appClientMetadata = new ApplicationClientMetaData();
  // Handle attributes and set them in the EjbJarMetaData
  final int count = reader.getAttributeCount();
  for (int i = 0; i < count; i++) {
    if (attributeHasNamespace(reader, i)) {
      continue;
    }
    processAttribute(appClientMetadata, reader, i);
  }
  appClientMetadata.setDescriptionGroup(new DescriptionGroupMetaData());
  appClientMetadata.setEnvironmentRefsGroupMetaData(new AppClientEnvironmentRefsGroupMetaData());
  // parse and create metadata out of the elements
  while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
    processElement(appClientMetadata, reader, propertyReplacer);
  }
  return appClientMetadata;
}

代码示例来源:origin: stackoverflow.com

public class Main {

  public static void main(String args[]) {
    try{
      String raw = "<?xml version='1.0' encoding='iso-8859-1'?><ResDoc><resume canonversion='2' dateversion='2' present='735453' xml:space='preserve'>Jack - A Web ResumeHello,My name is Jack.<statements><personal>This website is all about my career, work experience and skill set. Unlike a traditional resume (available here in Word and PDF) my Web Resume is interactive. Feel free to run a query, a great way to see if I would be a good fit for you and your company, or click on any of the blue links for more details and work samples.Just like my career and capabilities, this site is always growing as I add more examples of my work, and even some blog entries or documentation/ tutorials. You will see this site grow as I do!</personal></statements>I want to thank you for stopping by!Resume<experience>Current employer\t2013\tCurrent\t<title>Systems Administrator</title>\t<description>Windows &amp; Altiris administration, SEP &amp; Credant Encryption Management and Policies,EMR Software Dev company\t2012\t2013\tHelpesk Technician II\tOffice 365,Data-Center migration, Corporate image creation\\ configuration, Domain administrationCommunications\t2011\t2012\tSr. Helpesk Technician\tPrimarily managed IT support issues, requests and tickets from local station &amp; east coast.</description><job id='1'><employer>University\t2008</employer>\t2011\t<title>Desktop Technician/ Analyst\tDesktop Support</title>, <description>managing tickets &amp; walk-ins. I.T. support for professors &amp; faculty..</description></job><job id='2'><employer>Emergence Enterprises LLC</employer>\t2006\t<description>2008\tManaging Member &amp; Technology Consultant\t1099 Contractor in City1 and City 2 areas. Computer consultation and support.Hightlights of IT Skills Don't be shy! Please, click the links for work samples!Technical Support\tSystems Deployment Configuration &amp; Upgrading\tScripting packaging and AutomationPowerShell\tVBScript\tBatchAutoIT\tSQL\tSymantec Management Platform AdministrationPatches &amp; Updates\tTraining &amp; Mentoring\tAV management and deploymentVPN\tProxy and Firewall\tDisk Encryption policy and deploymentLAN/WAN Administration\tInventory Solutions\tand More!</description></job></experience>ReferencesRef_1\tSystems Engineer II\tnationwide companyRef_2\tTechnology &amp; Systems Manager\tUniversityRef_3\tInfrastructure Project Manager\tLocal company</resume><skillrollup version='1'>  <canonskill experience='1' expidrefs='2' idrefs='2' name='automation'>    <variant>Automation</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='consulting'>    <variant>Consultant</variant>    <variant>consultation</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='cryptography'>    <variant>Encryption</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='firewalls'>    <variant>Firewall</variant>  </canonskill>  <canonskill experience='1' name='imaging'>    <variant>image</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='inventory management'>    <variant>Inventory</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='lan'>    <variant>LAN</variant>  </canonskill>  <canonskill experience='1' expidrefs='1,2' idrefs='1,2' name='management'>    <variant>Managing</variant>    <variant>managing</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='mentoring'>    <variant>Mentoring</variant>  </canonskill>  <canonskill experience='1' name='microsoft office'>    <variant>Office</variant>  </canonskill>  <canonskill experience='1' name='microsoft windows'>    <variant>Windows</variant>  </canonskill>  <canonskill experience='1' name='migration'>    <variant>migration</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='packaging'>    <variant>packaging</variant>  </canonskill>  <canonskill experience='1' name='policy analysis'>    <variant>Policies</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='proxy server'>    <variant>Proxy</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='shell scripting'>    <variant>Scripting</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='sql'>    <variant>SQL</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='symantec packages'>    <variant>Symantec</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='technical support'>    <variant>Technical Support</variant>  </canonskill>  <canonskill experience='1' name='technician'>    <variant>Technician</variant>    <variant>Technician II</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='upgrades'>    <variant>Upgrading</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='vbscript'>    <variant>VBScript</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='vpn'>    <variant>VPN</variant>  </canonskill>  <canonskill experience='1' expidrefs='2' idrefs='2' name='wan'>    <variant>WAN</variant>  </canonskill></skillrollup></ResDoc>";
      XMLInputFactory xif = XMLInputFactory.newFactory();
      XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(raw));

      while(xsr.hasNext()){
        xsr.nextTag();
        System.out.println(xsr.getName());
        System.out.println(xsr.getAttributeCount());
      }
    }
    catch(XMLStreamException e)
    {

    }
  }
}

代码示例来源:origin: javaee/glassfish

ConfigModel model = document.getModelByElementName(in.getLocalName());
if(model==null) {
  String localName = in.getLocalName();
  Logger.getAnonymousLogger().severe("Ignoring unrecognized element "+in.getLocalName() + " at " + in.getLocation());
    final int tag = in.nextTag();
    if (tag==START_ELEMENT && in.getLocalName().equals(localName)) {
      if (Logger.getAnonymousLogger().isLoggable(Level.FINE)) {

代码示例来源:origin: stackoverflow.com

reader.nextTag();
return reader.hasNext();

代码示例来源:origin: wildfly/wildfly

int sequenceNumber = 0;
final int attributeCount = streamReader.getAttributeCount();
for (int i = 0; i < attributeCount; i ++) {
  String namespace = streamReader.getAttributeNamespace(i);
  if (namespace != null && !namespace.equals("")) {
    throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);
    name = streamReader.getAttributeValue(i);
  } else if ("algorithm".equals(localName)) {
    algorithm = streamReader.getAttributeValue(i);
  } else if ("hash".equals(localName)) {
    hash = CodePointIterator.ofString(streamReader.getAttributeValue(i)).base64Decode(Base64Alphabet.STANDARD, false).drain();
  } else if ("seed".equals(localName)) {
    seed = new String(CodePointIterator.ofString(streamReader.getAttributeValue(i)).base64Decode(Base64Alphabet.STANDARD, false).drain(), StandardCharsets.US_ASCII);
    sequenceNumber = Integer.parseInt(streamReader.getAttributeValue(i));
  } else {
    throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);
if (streamReader.nextTag() != END_ELEMENT) {
  throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);

代码示例来源:origin: org.jboss.osgi.repository/jbosgi-repository-core

@Override
public XResource nextResource() {
  try {
    while (reader.hasNext() && reader.nextTag() != END_ELEMENT) {
      Element element = Element.forName(reader.getLocalName());
      switch (element) {
        case RESOURCE: {
          return readResourceElement(reader);
        }
      }
    }
  } catch (XMLStreamException ex) {
    throw MESSAGES.cannotReadResourceElement(ex, reader.getLocation());
  }
  return null;
}

代码示例来源:origin: wildfly/wildfly

private Attributes parseAttributes(final XMLStreamReader streamReader) throws RealmUnavailableException, XMLStreamException {
  final int attributeCount = streamReader.getAttributeCount();
  if (attributeCount > 0) {
    throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);
  }
  int tag = streamReader.nextTag();
  if (tag == END_ELEMENT) {
    return Attributes.EMPTY;
  }
  Attributes attributes = new MapAttributes();
  do {
    if (! validNamespace(streamReader.getNamespaceURI())) {
      throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);
    }
    if ("attribute".equals(streamReader.getLocalName())) {
      parseAttribute(streamReader, attributes);
    } else {
      throw ElytronMessages.log.fileSystemRealmInvalidContent(path, streamReader.getLocation().getLineNumber(), name);
    }
  } while (streamReader.nextTag() == START_ELEMENT);
  return attributes;
}

相关文章

微信公众号

最新文章

更多