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.
For example: (XML File)
<RootName>
<SubNode>
<Attribute1>Value 1</Attribute1>
<Attribute2>Value 2</Attribute2>
</SubNode>
</RootName>
Here is my code so far: (Java Code)
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:
Also see my comment below your question on how to properly design XML and when to use elements, and when to use attributes.