org.xml.sax.Attributes类的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(13.7k)|赞(0)|评价(0)|浏览(213)

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

Attributes介绍

[英]Interface for a list of XML attributes. This module, both source code and documentation, is in the Public Domain, and comes with NO WARRANTY. See http://www.saxproject.org for further information.

This interface allows access to a list of attributes in three different ways:

  1. by attribute index;
  2. by Namespace-qualified name; or
  3. by qualified (prefixed) name.

The list will not contain attributes that were declared #IMPLIED but not specified in the start tag. It will also not contain attributes used as Namespace declarations (xmlns*) unless the http://xml.org/sax/features/namespace-prefixes feature is set to true (it is false by default). Because SAX2 conforms to the original "Namespaces in XML" recommendation, it normally does not give namespace declaration attributes a namespace URI.

Some SAX2 parsers may support using an optional feature flag (http://xml.org/sax/features/xmlns-uris) to request that those attributes be given URIs, conforming to a later backwards-incompatible revision of that recommendation. (The attribute's "local name" will be the prefix, or "xmlns" when defining a default element namespace.) For portability, handler code should always resolve that conflict, rather than requiring parsers that can change the setting of that feature flag.

If the namespace-prefixes feature (see above) is false, access by qualified name may not be available; if the http://xml.org/sax/features/namespaces feature is false, access by Namespace-qualified names may not be available.

This interface replaces the now-deprecated SAX1 org.xml.sax.AttributeList interface, which does not contain Namespace support. In addition to Namespace support, it adds the getIndex methods (below).

The order of attributes in the list is unspecified, and will vary from implementation to implementation.
[中]用于XML属性列表的接口*此模块(包括源代码和文档)属于公共领域,不提供任何保修。详见http://www.saxproject.org
此界面允许以三种不同的方式访问属性列表:
1.按属性指数;
1.名称空间限定名称;或
1.通过限定(前缀)名称。
列表将不包含在开始标记中声明#隐含但未指定的属性。它也不包含用作命名空间声明(xmlns
)的属性,除非http://xml.org/sax/features/namespace-prefixes特性设置为true(默认为false)。因为SAX2符合最初的“XML中的名称空间”建议,所以它通常不为名称空间声明属性提供名称空间URI。
一些SAX2解析器可能支持使用可选的特性标志(http://xml.org/sax/features/xmlns-uris)来请求为这些属性提供URI,以符合该建议稍后的向后不兼容版本。(定义默认元素名称空间时,属性的“本地名称”将是前缀或“xmlns”。)为了可移植性,处理程序代码应该始终解决该冲突,而不是需要可以更改该特性标志设置的解析器。
如果名称空间前缀功能(见上文)为false,则通过限定名称进行访问可能不可用;如果http://xml.org/sax/features/namespaces功能为false,则按命名空间限定名进行访问可能不可用。
此接口将替换现在已弃用的SAX1组织。xml。萨克斯。AttributeList接口,不包含命名空间支持。除了名称空间支持之外,它还添加了getIndex方法(如下)。
列表中属性的顺序未指定,并且会因实现而异。

代码示例

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

public void startElement(String namespace, String localName, String qName,
Attributes attrs) 
final Element tmp = (Element)_document.createElementNS(namespace, qName);
  final int nDecls = _namespaceDecls.size();
  for (int i = 0; i < nDecls; i++) {
  final String prefix = (String) _namespaceDecls.elementAt(i++);
    tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX,
    (String) _namespaceDecls.elementAt(i));
    tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, 
    (String) _namespaceDecls.elementAt(i));
final int nattrs = attrs.getLength();
for (int i = 0; i < nattrs; i++) {
  if (attrs.getLocalName(i) == null) {
  tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
  tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i), 
    attrs.getValue(i));
Node last = (Node)_nodeStk.peek();
_nodeStk.push(tmp);
_lastSibling = null;

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

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
  Node parent = getParent();
  Element element = this.document.createElementNS(uri, qName);
  for (int i = 0; i < attributes.getLength(); i++) {
    String attrUri = attributes.getURI(i);
    String attrQname = attributes.getQName(i);
    String value = attributes.getValue(i);
    if (!attrQname.startsWith("xmlns")) {
      element.setAttributeNS(attrUri, attrQname, value);
    }
  }
  element = (Element) parent.appendChild(element);
  this.elements.add(element);
}

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

private List<Attribute> getAttributes(Attributes attributes) {
  int attrLength = attributes.getLength();
  List<Attribute> result = new ArrayList<>(attrLength);
  for (int i = 0; i < attrLength; i++) {
    QName attrName = toQName(attributes.getURI(i), attributes.getQName(i));
    if (!isNamespaceDeclaration(attrName)) {
      result.add(this.eventFactory.createAttribute(attrName, attributes.getValue(i)));
    }
  }
  return result;
}

代码示例来源:origin: MovingBlocks/Terasology

public static String findAttribute(Attributes attributes, String name) {
  int size = attributes.getLength();
  for (int i = 0; i < size; i++) {
    if (attributes.getQName(i).equalsIgnoreCase(name)) {
      return attributes.getValue(i);
    }
  }
  return null;
}

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

final Stack<Element> elementStack = new Stack<Element>();
final StringBuilder textBuffer = new StringBuilder();
final DefaultHandler handler = new DefaultHandler() {
      throws SAXException {
    addTextIfNeeded();
    final Element el = doc.createElement(qName);
    for (int i = 0; i < attributes.getLength(); i++) {
      el.setAttribute(attributes.getQName(i), attributes.getValue(i));
    elementStack.push(el);
  public void endElement(final String uri, final String localName, final String qName) {
    addTextIfNeeded();
    final Element closedEl = elementStack.pop();
    if (elementStack.isEmpty()) { // Is this the root element?
      doc.appendChild(closedEl);
    } else {
    if (textBuffer.length() > 0) {
      final Element el = elementStack.peek();
      final Node textNode = doc.createTextNode(textBuffer.toString());
      el.appendChild(textNode);
      textBuffer.delete(0, textBuffer.length());

代码示例来源:origin: org.eclipse/org.eclipse.pde.core

private void createElement(String tagName, Attributes attributes) {
  Element element = fParent.getOwnerDocument().createElement(tagName);
  for (int i = 0; i < attributes.getLength(); i++) {
    element.setAttribute(attributes.getQName(i), attributes.getValue(i));
  }
  ((Element)fOpenElements.peek()).appendChild(element);
  fOpenElements.push(element);
}

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

elem = m_doc.createElementNS(null,name);
else
 elem = m_doc.createElementNS(ns, name);
 int nAtts = atts.getLength();
   if (atts.getType(i).equalsIgnoreCase("ID"))
    setIDAttribute(atts.getValue(i), elem);
   String attrNS = atts.getURI(i);
   String attrQName = atts.getQName(i);
   elem.setAttributeNS(attrNS,attrQName, atts.getValue(i));
 int nDecls = m_prefixMappings.size();
  prefix = (String) m_prefixMappings.elementAt(i);
  declURL = (String) m_prefixMappings.elementAt(i + 1);
  elem.setAttributeNS("http://www.w3.org/2000/xmlns/", prefix, declURL);
 m_elemStack.push(elem);
 throw new org.xml.sax.SAXException(de);

代码示例来源:origin: scouter-project/scouter

@Override
public final void startElement(
  final String ns, final String localName, final String qName, final Attributes atts)
  throws SAXException {
 try {
  closeElement();
  writeIdent();
  w.write('<' + qName);
  if (atts != null && atts.getLength() > 0) {
   writeAttributes(atts);
  }
  if (optimizeEmptyElements) {
   openElement = true;
  } else {
   w.write(">\n");
  }
  ident += 2;
 } catch (IOException ex) {
  throw new SAXException(ex);
 }
}

代码示例来源:origin: org.codehaus.tycho/tycho-osgi-components

public void handleLibraryState(String elementName, Attributes attributes) {
  if (elementName.equals(LIBRARY_EXPORT)) {
    stateStack.push(new Integer(LIBRARY_EXPORT_STATE));
    String currentLib = (String) objectStack.peek();
    if (attributes == null)
      return;
    String maskValue = attributes.getValue("", LIBRARY_EXPORT_MASK); //$NON-NLS-1$
    objectStack.pop();
    Vector exportMask = (Vector) objectStack.peek();
      while (tok.hasMoreTokens()) {
        String value = tok.nextToken();
        if (!exportMask.contains(maskValue))
          exportMask.addElement(value.trim());

代码示例来源:origin: protegeproject/protege

@Override
  public void startElement(String namespaceURI,
               String localName,
               String qName,
               Attributes atts)
  throws SAXException {
    for (int i = 0; i < atts.getLength(); i++) {                	
      if (atts.getQName(i).equals("xml:base")) {
        try {
          xmlBase = new URI(atts.getValue(i));
          throw new SAXParseCompletedException();
        }
        catch (URISyntaxException e) {
          logger.error("URI syntax error", e);
        }
      }
    }
    throw new SAXException("No xml base");
  }
}

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

logger.fine("ADDING NAMESPACES: " + schemaProxy.size());
  String t = atts.getValue("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
    t = atts.getValue("", "schemaLocation");
        throw new SAXException(
            "Bad Schema location attribute: you must have an even number of terms");
  XMLElementHandler parent = ((XMLElementHandler) handlers.peek());
  logger.finest(
      "Parent Node = "
  URI uri = new URI(namespaceURI);
  XMLElementHandler eh = parent.getHandler(uri, localName, hints);
  handlers.push(eh);
  eh.startElement(new URI(namespaceURI), localName, atts);
} catch (Exception e) {
      new SAXException(
          e.getMessage()
              + " at Line "
              + qName,
          e);
  exception.initCause(e);
  throw exception;

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

String sl = atts.getValue("", "schemaLocation");
  sl = atts.getValue(namespaceURI, "schemaLocation");
  schemaLocation = sl != null ? new URI(sl) : null;
} catch (URISyntaxException e) {
  logger.warning(e.toString());
  throw new SAXException(e);
String namespace1 = atts.getValue("", "namespace");
  namespace1 = atts.getValue(namespaceURI, "namespace");
  this.namespace = new URI(namespace1);
} catch (URISyntaxException e) {
  logger.warning(e.toString());
  throw new SAXException(e);
  throw new SAXException(
      "You may not import a namespace with the same name as the current namespace");

代码示例来源:origin: com.sun.xml.bind/jaxb-impl

public void startElement(String namespace, String localName, String qName, Attributes attrs) {
  Node parent = nodeStack.peek();
  Element element = document.createElementNS(namespace, qName);
    int length = attrs.getLength();
    for (int i = 0; i < length; i++) {
      String namespaceuri = attrs.getURI(i);
      String value = attrs.getValue(i);
      String qname = attrs.getQName(i);
      element.setAttributeNS(namespaceuri, qname, value);
  nodeStack.push(element);

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

throws SAXException {
if (value.length != 1) {
  throw new SAXException(
      "Internal error, ElementValues require an associated Element.");
String cs;
String ts;
dec = attrs.getValue("", "decimal");
  dec = attrs.getValue(GMLSchema.NAMESPACE.toString(), "decimal");
cs = attrs.getValue("", "cs");
  cs = attrs.getValue(GMLSchema.NAMESPACE.toString(), "cs");
ts = attrs.getValue("", "ts");
  ts = attrs.getValue(GMLSchema.NAMESPACE.toString(), "ts");

代码示例来源:origin: apache/geode

/**
 * When a <code>required-role</code> element is encountered, we push a string for creation of
 * MembershipAttributes.
 */
private void startRequiredRole(Attributes atts) {
 stack.push(atts.getValue(NAME));
}

代码示例来源:origin: org.codehaus.tycho/tycho-osgi-components

public void parseLibraryAttributes(Attributes attributes) {
  // Push a vector to hold the export mask
  objectStack.push(new Vector());
  String current = attributes.getValue("", LIBRARY_NAME); //$NON-NLS-1$ 
  objectStack.push(current);
}

代码示例来源:origin: apache/tika

String indicesString = null;
for (int i = 0; i < atts.getLength(); i++) {
  String lName = atts.getLocalName(i);
  String value = atts.getValue(i);
  value = (value == null) ? "" : value.trim();
      originX = Float.parseFloat(atts.getValue(i));
    } catch (NumberFormatException e) {
      throw new SAXException(e);
      originY = Float.parseFloat(atts.getValue(i));
    } catch (NumberFormatException e) {
      throw new SAXException(e);
    unicodeString = atts.getValue(i);
  } else if (BIDI_LEVEL.equals(lName) && value.length() > 0) {
    try {
      bidilevel = Integer.parseInt(atts.getValue(i));
    } catch (NumberFormatException e) {
      throw new SAXException(e);
    indicesString = atts.getValue(i);
  } else if (NAME.equals(lName)) {
    name = value;

代码示例来源:origin: derry/delion

@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
    throws SAXException {
  if (mTagStack.empty()) throw new SAXException("Tag stack is empty when it shouldn't be.");
  Node currentNode = new Node(qName);
  mTagStack.peek().children.add(currentNode);
  mTagStack.push(currentNode);
  for (int i = 0; i < attributes.getLength(); ++i) {
    String attributeName = attributes.getLocalName(i);
    String attributeValue = attributes.getValue(attributeName);
    currentNode.attributes.put(attributeName, attributeValue);
  }
}

代码示例来源:origin: jMonkeyEngine/jmonkeyengine

@Override
public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
  if (qName.equals("scene")) {
    if (elementStack.size() != 0) {
      throw new SAXException("dotScene parse error: 'scene' element must be the root XML element");
    String version = attribs.getValue("formatVersion");
    if (version == null || (!version.equals("1.0.0") && !version.equals("1.0.1"))) {
      logger.log(Level.WARNING, "Unrecognized version number"
      throw new SAXException("dotScene parse error: nodes element was specified twice");
    String curElement = elementStack.peek();
    if (!curElement.equals("node") && !curElement.equals("nodes")) {
      throw new SAXException("dotScene parse error: "
          + "node element can only appear under 'node' or 'nodes'");
  } else if (qName.equals("property")) {
    if (node != null) {
      String type = attribs.getValue("type");
      String name = attribs.getValue("name");
      String data = attribs.getValue("data");
      if (type.equals("BOOL")) {
        node.setUserData(name, Boolean.parseBoolean(data) || data.equals("1"));

代码示例来源:origin: wbaumann/SmartReceiptsLibrary

void startElement(Attributes attributes, String localName) throws SAXException {
  FlexViews.Element element = null;
  for (int i=0; i<elementCache.length; i++) {
    if (elementCache[i].isElement(localName)) {
      element = elementCache[i];
      break;
    }
  }
  if (element == null) 
    throw new SAXException("Undefined FlexViews Element: " + localName);
  elements.push(element);
  String id = attributes.getValue(FlexViews.Attribute.ID.tagName());
  if (element.isViewGroup()) {
    if (id == null) 
      throw new SAXException("All ViewGroups require that a \"" + FlexViews.Attribute.ID.tagName() + "\" attribute is defined.");
    ids.push(id);
    views.push(new ArrayList<FlexView>());
  }
  else {
    if (!views.isEmpty()) 
      views.peek().add(new FlexView(attributes, element));
  }
  if (id != null) {
    if (!viewMap.containsKey(id))
      viewMap.put(id, new FlexView(attributes, element));
    else
      throw new SAXException("Views cannot have the same ID: " + id);
  }
}

相关文章