简体   繁体   English

获取XML文件中所有属性的名称

[英]Get the name of all attributes in a XML-File

Hey I have an XML file and I would like to navigate to a given node and grab the name of all Attributes to that node. 嘿,我有一个XML文件,我想导航到一个给定的节点,并获取该节点的所有属性的名称。

For example: (XML File) 例如:(XML文件)

<RootName>
    <SubNode>
    <Attribute1>Value 1</Attribute1>
    <Attribute2>Value 2</Attribute2>  
</SubNode> 
</RootName>

Here is my code so far: (Java Code) 到目前为止,这是我的代码:(Java代码)

File file = new File("data.xml");
try
    {    
        /* Parse File */
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(file);

        /* Find Starting Tag */
        NodeList nodes = doc.getElementsByTagName(StartTag);
        for (int i = 0; i < nodes.getLength(); i++)
        {
            Element element = (Element) nodes.item(i);
            System.out.println(element);

        }

Now I know you can find a specific attribute given the name 现在我知道您可以找到一个给定名称的特定属性

String name = element.getAttribute("Attribute1");    

But I would like to find all these names dynamically. 但是我想动态地找到所有这些名称。

Thanks in advance 提前致谢

-Scott 斯科特

What you are looking for are the Elements. 您正在寻找的是元素。 Here is a sample on how to get the Elements in an XML: 这是有关如何在XML中获取元素的示例:

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMElements{
   static public void main(String[] arg){
     try {
       BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter XML File name: ");
       String xmlFile = bf.readLine();
       File file = new File(xmlFile);
       if(file.exists()){
         // Create a factory
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         // Use the factory to create a builder
         DocumentBuilder builder = factory.newDocumentBuilder();
         Document doc = builder.parse(xmlFile);
         // Get a list of all elements in the document
         NodeList list = doc.getElementsByTagName("*");
         System.out.println("XML Elements: ");
         for (int i=0; i<list.getLength(); i++) {
           // Get element
           Element element = (Element)list.item(i);
           System.out.println(element.getNodeName());
         }
       }
       else{
         System.out.print("File not found!");
       }
     }
     catch (Exception e) {
       System.exit(1);
     }
   }
}

Also see my comment below your question on how to properly design XML and when to use elements, and when to use attributes. 另请参阅您的问题下方的我的评论,该问题涉及如何正确设计XML,何时使用元素以及何时使用属性。

element.getAttributes(); gets you a org.w3c.dom.NamedNodeMap . 为您提供org.w3c.dom.NamedNodeMap You can loop through this using the item(int index) method to get org.w3c.dom.Attr nodes, and get the names of those from the getName() method. 您可以使用item(int index)方法遍历此过程,以获取org.w3c.dom.Attr节点,并从getName()方法getName()这些节点的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM