XML : How can I generically get an XML attribute in Java without specifying its name?

I am trying to go through a XML-document in a generic way, as in knowing nothing about it, except how many levels it has:

  <nodelevel1>      <nodelevel2 attribute="xyz">      </nodelevel2>  </nodelevel1>    

Therefore I took this XML-document and extracted all the information in it in a generic way (so no XPath, no .getElementsByTagName("carname").item(0).getTextContent(), etc.). I do this in order to understand working with XML better, not in order to have a perfect solution, I'm aware that there are simpler / better solutions. This is for learning purposes only.

I was able to get all the information out in a generic way except for the attributes company="Ferrari", company="Lamborgini", etc. I had to use "Company: " + eElement.getAttribute("company").

So how can I get the attributes of the nodes (here the companies) without specifying them?

sportscars.xml

       <?xml version="1.0"?>       <cars>          <supercars company="Ferrari">             <carname type="formula one">Ferarri 101</carname>             <carname type="sports car">Ferarri 201</carname>             <carname type="sports car">Ferarri 301</carname>          </supercars>          <supercars company="Lamborgini">             <carname>Lamborgini 001</carname>             <carname>Lamborgini 002</carname>             <carname>Lamborgini 003</carname>          </supercars>          <luxurycars company="Benteley">             <carname>Benteley 1</carname>             <carname>Benteley 2</carname>             <carname>Benteley 3</carname>          </luxurycars>       </cars>    

My java-class QueryXMLFileDemo.java:

      public class QueryXmlFileDemo {            public static void main(String[] args) {              try {                  File inputFile = new File("sportcars.xml");                  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();                  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();                  Document inputDocument = dBuilder.parse(inputFile);                  inputDocument.getDocumentElement().normalize();                  Node carsNode = inputDocument.getFirstChild();                  NodeList carsNodeList = carsNode.getChildNodes();                  for (int i = 0; i < carsNodeList.getLength(); i++) {                      Node carTypes = carsNodeList.item(i);                        String attributeName = carsNodeList.item(i).getNodeName();                      System.out.println("Attribute Name: " + attributeName);                             // hides the #text-entries                      if (Node.ELEMENT_NODE != carTypes.getNodeType()) {                          continue;                      }                      if (carTypes.getNodeType() == Node.ELEMENT_NODE) {                          Element eElement = (Element) carTypes;                          // Line I want to do generically without specifying the attributes name                          System.out.println("Company: " + eElement.getAttribute("company"));                      }                      System.out.println("CarType: " + carTypes.getNodeName());                      NodeList carNamesList = carTypes.getChildNodes();                      for (int j = 0; j < carNamesList.getLength(); j++) {                          Node carNameNode = carNamesList.item(j);                          if (Node.ELEMENT_NODE != carNameNode.getNodeType()) {                              continue;                          }                          System.out.println("Car: " + carNameNode.getTextContent());                      }                      System.out.println("");                  }              } catch (Exception e) {              }          }      }    

Output:

  Company: Ferrari  CarType: supercars  Car: Ferarri 101  Car: Ferarri 201  Car: Ferarri 301    Company: Lamborgini  CarType: supercars  Car: Lamborgini 001  Car: Lamborgini 002  Car: Lamborgini 003    Company: Benteley  CarType: luxurycars  Car: Benteley 1  Car: Benteley 2  Car: Benteley 3    

No comments:

Post a Comment