【问题标题】:java Get the list or name of all attributes in a XML elementjava 获取 XML 元素中所有属性的列表或名称
【发布时间】:2011-10-10 01:02:19
【问题描述】:

我有一个 xml 元素

   <base baseAtt1="aaa" baseAtt2="tt">
        <innerElement att1="one" att2="two" att3="bazinga"/>
   </base>

我想获取属性列表。 基础元素和内部元素。

不知道innerElement的名字 它可以有许多不同的名称。

 NodeList baseElmntLst_gold  = goldAnalysis.getElementsByTagName("base");
 Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);

目标是得到一种字典作为输出,

例如,对于上面的 xml,输出将是具有这些值的字典。

baseAtt1 = "aaa"
baseAtt2 = "tt"
att1 = "one"
att2 = "two"
att3 = "bazinga"

我正在使用 jre 1.5

【问题讨论】:

    标签: java xml dom xpath


    【解决方案1】:

    您可以使用此 XPath 检索第一个 element 节点的所有属性:

    base/element[1]/@*
    

    要获取 XML 中所有节点的所有属性,您可以使用以下表达式:

    //@*
    

    【讨论】:

    【解决方案2】:

    这是基于普通 DOM 的解决方案(但是在 Java 中将 XPath 与 DOM 结合并没有错):

    NodeList baseElmntLst_gold  = goldAnalysis.getElementsByTagName("base");
    Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
    
    NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
    for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i)
    {
        Node attr = baseElmnt_gold_attr.item(i);
        System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
    }
    
    NodeList innerElmntLst_gold = baseElmnt_gold.getChildNodes();
    Element innerElement_gold = null;
    for (int i = 0; i < innerElmntLst_gold.getLength(); ++i)
    {
        if (innerElmntLst_gold.item(i) instanceof Element)
        {
            innerElement_gold = (Element) innerElmntLst_gold.item(i);
            break; // just get first child
        }
    }
    
    NamedNodeMap innerElmnt_gold_attr = innerElement_gold.getAttributes();
    for (int i = 0; i < innerElmnt_gold_attr.getLength(); ++i)
    {
        Node attr = innerElmnt_gold_attr.item(i);
        System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
    }
    

    结果:

    baseAtt1 = "aaa"
    baseAtt2 = "tt"
    att1 = "one"
    att2 = "two"
    att3 = "bazinga"
    

    【讨论】:

    • 为什么只在第二个循环中获取第一个孩子,我总是获取第一行数据而无法获得第二和第三个继续?
    【解决方案3】:

    如果您使用 XPath,您将拥有更少的代码,但对于基于 dom 的解决方案,我有一个建议:

    public void printElementsAndAttributes() throws Exception {
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        org.w3c.dom.Document doc = db.parse(new File("test.xml"));
        NodeList base = doc.getElementsByTagName("base");
        Node basenode = base.item(0);
        System.out.println(basenode.getNodeName() + getAttributesAsString(basenode.getAttributes()));
        NodeList children = basenode.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node item = children.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(item.getNodeName() + getAttributesAsString(item.getAttributes()));
    
            }
        }
    
    
    }
    
    private String getAttributesAsString(NamedNodeMap attributes) {
        StringBuilder sb = new StringBuilder("\n");
        for (int j = 0; j < attributes.getLength(); j++) {
            sb.append("\t- ").append(attributes.item(j).getNodeName()).append(": ").append(attributes.item(j).getNodeValue()).append("\n");
        }
        return sb.toString();
    
    }
    

    【讨论】:

      【解决方案4】:

      使用这个方法..

       public static void listAllAttributes(Element element) {
      
                   System.out.println("List attributes for node: " + element.getNodeName());
      
                   // get a map containing the attributes of this node
                       NamedNodeMap attributes = element.getAttributes();
      
                   // get the number of nodes in this map
      
                   int numAttrs = attributes.getLength();
      
      
                   for (int i = 0; i < numAttrs; i++) {
                  Attr attr = (Attr) attributes.item(i);
                  String attrName = attr.getNodeName();
      
       String attrValue = attr.getNodeValue();
        System.out.println("Found attribute: " + attrName + " with value: " + attrValue);
      
                   }
       }
      

      通过在主方法中使用以下调用来调用此方法

        NodeList entries = doc.getElementsByTagName("NameOfTheNode");
                      int num = entries.getLength();
                       for (int i=0; i<num; i++) {
                                                        Element node = (Element) entries.item(i);
                                                        listAllAttributes(node);
      
                                }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-09
        • 2011-08-23
        • 1970-01-01
        • 2023-02-25
        相关资源
        最近更新 更多