org.w3c.dom.Node.getAttributes()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(594)

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

Node.getAttributes介绍

[英]A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
[中]包含此节点属性的NamedNodeMap(如果是Element)或null否则。

代码示例

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

  1. private List<String> extractUsesPermissionNames(NodeList usesPermissionNodes) {
  2. List<String> usesPermissionQualifiedNames = new ArrayList<>();
  3. for (int i = 0; i < usesPermissionNodes.getLength(); i++) {
  4. Node usesPermissionNode = usesPermissionNodes.item(i);
  5. Node nameAttribute = usesPermissionNode.getAttributes().getNamedItem("android:name");
  6. if (nameAttribute == null) {
  7. return null;
  8. }
  9. usesPermissionQualifiedNames.add(nameAttribute.getNodeValue());
  10. }
  11. return usesPermissionQualifiedNames;
  12. }

代码示例来源:origin: Tencent/tinker

  1. private static String extractNameAttribute(Node node) {
  2. return node.getAttributes().getNamedItem("name").getNodeValue();
  3. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. public static Node getAttribute(Node node, String name) {
  2. return node.getAttributes().getNamedItem(name);
  3. }

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

  1. @Override
  2. public int compare(Node o1, Node o2) {
  3. NamedNodeMap attributes1 = o1.getAttributes();
  4. NamedNodeMap attributes2 = o2.getAttributes();
  5. if (attributes1 == null) {
  6. return 1;
  7. } else if (attributes2 == null) {
  8. return -1;
  9. } else if (MO_TAB.equals(o1.getNodeName()) && !MO_TAB.equals(o2.getNodeName())) {
  10. return -1;
  11. } else if (!MO_TAB.equals(o1.getNodeName()) && MO_TAB.equals(o2.getNodeName())) {
  12. return 1;
  13. } else if (MO_GROUP.equals(o1.getNodeName()) && !MO_GROUP.equals(o2.getNodeName())) {
  14. if (MO_TAB.equals(o1.getNodeName()) && MO_TAB.equals(o2.getNodeName())) {
  15. id1 = attributes1.getNamedItem(TAB_NAME);
  16. id2 = attributes2.getNamedItem(TAB_NAME);
  17. } else if (MO_GROUP.equals(o1.getNodeName()) && MO_GROUP.equals(o2.getNodeName())) {
  18. id1 = attributes1.getNamedItem(GROUP_NAME);
  19. id2 = attributes2.getNamedItem(GROUP_NAME);
  20. } else {
  21. return -1;
  22. String idVal1 = id1.getNodeValue();
  23. String idVal2 = id2.getNodeValue();
  24. return idVal1.compareTo(idVal2);

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

  1. protected void processLogLevels(Document doc) {
  2. NodeList nodeList = doc.getElementsByTagName(LEVEL);
  3. Map menuItems = _monitor.getLogLevelMenuItems();
  4. for (int i = 0; i < nodeList.getLength(); i++) {
  5. Node n = nodeList.item(i);
  6. NamedNodeMap map = n.getAttributes();
  7. String name = getValue(map, NAME);
  8. try {
  9. JCheckBoxMenuItem item =
  10. (JCheckBoxMenuItem) menuItems.get(LogLevel.valueOf(name));
  11. item.setSelected(getValue(map, SELECTED).equalsIgnoreCase("true"));
  12. } catch (LogLevelFormatException e) {
  13. // ignore it will be on by default.
  14. }
  15. }
  16. }

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

  1. private static HashMap<String, String> parseNodeAttributes(Node node) {
  2. final NamedNodeMap attributes = node.getAttributes();
  3. final int attrCount = attributes.getLength();
  4. final HashMap<String, String> receiverAttrs = new HashMap<>(attributes.getLength());
  5. for (int i = 0; i < attrCount; i++) {
  6. Node attribute = attributes.item(i);
  7. String value = attribute.getNodeValue();
  8. if (value != null) {
  9. receiverAttrs.put(attribute.getNodeName(), value);
  10. }
  11. }
  12. return receiverAttrs;
  13. }

代码示例来源:origin: iBotPeaches/Apktool

  1. /**
  2. * Removes "debug" tag from file
  3. *
  4. * @param file AndroidManifest file
  5. * @throws AndrolibException
  6. */
  7. public static void removeApplicationDebugTag(File file) throws AndrolibException {
  8. if (file.exists()) {
  9. try {
  10. Document doc = loadDocument(file);
  11. Node application = doc.getElementsByTagName("application").item(0);
  12. // load attr
  13. NamedNodeMap attr = application.getAttributes();
  14. Node debugAttr = attr.getNamedItem("android:debuggable");
  15. // remove application:debuggable
  16. if (debugAttr != null) {
  17. attr.removeNamedItem("android:debuggable");
  18. }
  19. saveDocument(file, doc);
  20. } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
  21. }
  22. }
  23. }

代码示例来源:origin: real-logic/simple-binary-encoding

  1. /**
  2. * Helper function that throws an exception when the attribute is not set.
  3. *
  4. * @param elementNode that should have the attribute
  5. * @param attrName that is to be looked up
  6. * @return value of the attribute
  7. * @throws IllegalArgumentException if the attribute is not present
  8. */
  9. public static String getAttributeValue(final Node elementNode, final String attrName)
  10. {
  11. final Node attrNode = elementNode.getAttributes().getNamedItemNS(null, attrName);
  12. if (attrNode == null || "".equals(attrNode.getNodeValue()))
  13. {
  14. throw new IllegalStateException(
  15. "Element '" + elementNode.getNodeName() + "' has empty or missing attribute: " + attrName);
  16. }
  17. return attrNode.getNodeValue();
  18. }

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

  1. protected void processRecordFilter(Document doc) {
  2. NodeList nodeList = doc.getElementsByTagName(NDCTEXTFILTER);
  3. // there is only one value stored
  4. Node n = nodeList.item(0);
  5. // add check for backwards compatibility as this feature was added in
  6. // version 1.2
  7. if (n == null) {
  8. return;
  9. }
  10. NamedNodeMap map = n.getAttributes();
  11. String text = getValue(map, NAME);
  12. if (text == null || text.equals("")) {
  13. return;
  14. }
  15. _monitor.setNDCLogRecordFilter(text);
  16. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. /**
  2. * Get all the attributes in a certain node (on the root level)
  3. *
  4. * @param node
  5. * The node to examine
  6. * @return an array of strings containing the names of the attributes.
  7. */
  8. public static String[] getNodeAttributes( Node node ) {
  9. NamedNodeMap nnm = node.getAttributes();
  10. if ( nnm != null ) {
  11. String[] attributes = new String[nnm.getLength()];
  12. for ( int i = 0; i < nnm.getLength(); i++ ) {
  13. Node attr = nnm.item( i );
  14. attributes[i] = attr.getNodeName();
  15. }
  16. return attributes;
  17. }
  18. return null;
  19. }

代码示例来源:origin: org.testng/testng

  1. private void populateAttributes(Node node, Object object) throws XPathExpressionException {
  2. for (int j = 0; j < node.getAttributes().getLength(); j++) {
  3. Node item = node.getAttributes().item(j);
  4. setProperty(object, item.getLocalName(), item.getNodeValue());
  5. }
  6. }

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

  1. private void parseUsedPermissions(Document manifestDocument) {
  2. NodeList elementsByTagName = manifestDocument.getElementsByTagName("uses-permission");
  3. int length = elementsByTagName.getLength();
  4. for (int i = 0; i < length; i++) {
  5. Node node = elementsByTagName.item(i).getAttributes().getNamedItem("android:name");
  6. usedPermissions.add(node.getNodeValue());
  7. }
  8. }

代码示例来源:origin: Tencent/tinker

  1. String resourceType = node.getNodeName();
  2. if (resourceType.equals(ITEM_TAG)) {
  3. resourceType = node.getAttributes().getNamedItem("type").getNodeValue();
  4. if (resourceType.equals("id")) {
  5. resourceCollector.addIgnoreId(node.getAttributes().getNamedItem("name").getNodeValue());

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

  1. protected void processCategories(Document doc) {
  2. CategoryExplorerTree tree = _monitor.getCategoryExplorerTree();
  3. CategoryExplorerModel model = tree.getExplorerModel();
  4. NodeList nodeList = doc.getElementsByTagName(CATEGORY);
  5. // determine where the starting node is
  6. NamedNodeMap map = nodeList.item(0).getAttributes();
  7. int j = (getValue(map, NAME).equalsIgnoreCase(FIRST_CATEGORY_NAME)) ? 1 : 0;
  8. // iterate backwards throught the nodeList so that expansion of the
  9. // list can occur
  10. for (int i = nodeList.getLength() - 1; i >= j; i--) {
  11. Node n = nodeList.item(i);
  12. map = n.getAttributes();
  13. CategoryNode chnode = model.addCategory(new CategoryPath(getValue(map, PATH)));
  14. chnode.setSelected((getValue(map, SELECTED).equalsIgnoreCase("true")) ? true : false);
  15. if (getValue(map, EXPANDED).equalsIgnoreCase("true")) ;
  16. tree.expandPath(model.getTreePathToRoot(chnode));
  17. }
  18. }

代码示例来源:origin: stanfordnlp/CoreNLP

  1. /**
  2. * Fetches the value of a given attribute
  3. */
  4. public static String getAttributeValue(Node node, String attributeName) {
  5. try {
  6. return node.getAttributes().getNamedItem(attributeName).getNodeValue();
  7. } catch (Exception e) {
  8. }
  9. return null;
  10. }

代码示例来源:origin: Tencent/tinker

  1. private static String nodeToString(Node node, boolean isNoChild) {
  2. StringBuilder stringBuilder = new StringBuilder();
  3. if (node != null) {
  4. stringBuilder.append(node.getNodeName());
  5. NamedNodeMap namedNodeMap = node.getAttributes();
  6. stringBuilder.append(Constant.Symbol.MIDDLE_BRACKET_LEFT);
  7. int namedNodeMapLength = namedNodeMap.getLength();
  8. for (int j = 0; j < namedNodeMapLength; j++) {
  9. Node attributeNode = namedNodeMap.item(j);
  10. stringBuilder.append(Constant.Symbol.AT + attributeNode.getNodeName() + Constant.Symbol.EQUAL + attributeNode.getNodeValue());
  11. if (j < namedNodeMapLength - 1) {
  12. stringBuilder.append(Constant.Symbol.COMMA);
  13. }
  14. }
  15. stringBuilder.append(Constant.Symbol.MIDDLE_BRACKET_RIGHT);
  16. String value = StringUtil.nullToBlank(isNoChild ? node.getTextContent() : node.getNodeValue()).trim();
  17. if (StringUtil.isNotBlank(value)) {
  18. stringBuilder.append(Constant.Symbol.EQUAL + value);
  19. }
  20. }
  21. return stringBuilder.toString();
  22. }

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

  1. /**
  2. * Replaces the node's attributes with the attributes in the given hashmap
  3. *
  4. * @param node XML node that should be edited
  5. * @param attributes HashMap of strings representing the attributes of a node (key = value)
  6. * @return The given node with ONLY the given attributes
  7. */
  8. private static Node rewriteNodeAttributes(Node node, HashMap<String, String> attributes) {
  9. NamedNodeMap nodeAttrs = node.getAttributes();
  10. // Remove all previous attributes
  11. while (nodeAttrs.getLength() > 0) {
  12. nodeAttrs.removeNamedItem(nodeAttrs.item(0).getNodeName());
  13. }
  14. // Set to new attributes
  15. for (String key : attributes.keySet()) {
  16. ((Element) node).setAttribute(key, attributes.get(key));
  17. }
  18. return node;
  19. }

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

  1. private @Nullable String getAttributeValue(Node parentNode, String attributeName) {
  2. Node attributeNode = parentNode.getAttributes().getNamedItem(attributeName);
  3. return attributeNode == null ? null : attributeNode.getTextContent();
  4. }

代码示例来源:origin: org.testng/testng

  1. private void populateAttributes(Node node, Object object) {
  2. for (int j = 0; j < node.getAttributes().getLength(); j++) {
  3. Node item = node.getAttributes().item(j);
  4. p(node.getAttributes().item(j).toString());
  5. setProperty(object, item.getLocalName(), item.getNodeValue());
  6. }
  7. }

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

  1. private static String getTagAttributeText(final Document doc, final String tag, final String attribute) {
  2. NodeList elementsByTagName = doc.getElementsByTagName(tag);
  3. for (int i = 0; i < elementsByTagName.getLength(); ++i) {
  4. Node item = elementsByTagName.item(i);
  5. Node namedItem = item.getAttributes().getNamedItem(attribute);
  6. if (namedItem != null) {
  7. return namedItem.getTextContent();
  8. }
  9. }
  10. return null;
  11. }

相关文章