mukta
mukta

Reputation: 11

How can we get all the nodes name from unknown XML throught java

Suppose My XML Looks like This

<?xml version="1.0"?>
<Employees>
<Employee>
    <name>Pankaj</name>
    <age>29</age>
    <role>Java Developer</role>
    <gender>Male</gender>
    <address>
       <flat>f 09</flat>
       <housenum>naksha atlantis</housenum>
       <area>E city</area>
    </address>
    <ContactDetails>
        <mobile>4676543565</mobile>
        <landline>0120-223312</landline>
    </ContactDetails>
</Employee>
</Employee>
    <Employee>
    <name>Lisa</name>
    <age>35</age>
    <role>CSS Developer</role>
    <gender>Female</gender>
    <addressLine>
       <local>f 03</local>
       <gate>149</gate>
       <area>domlur</area>
    </addressLine>
 </Employee>
</Employees>


**I want output in this format**

[Employees[Employee[name,age,role,gender,address[flat,housenum,area],ContactDetails[mobile,landline]]]] [Employees[Employee[name,age,role,gender,,addressLine2[local2,gate2,area2]]]]##

Upvotes: 1

Views: 62

Answers (3)

Fenirine
Fenirine

Reputation: 11

You could use org.w3c.dom which is included in JDK1.4 and above and do something like this:

private static String parse(Node n) {
    if(n.getChildNodes().getLength() <= 2) return n.getNodeName();
    String res = n.getNodeName() + "[";
    NodeList children = n.getChildNodes();
    for(int i = 1 ; i < children.getLength() ; i += 2) {
        res += parse(children.item(i)) + ",";
    }
    return res.substring(0, res.length() - 1) + "]";
}

public static void main(String[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("data.xml"));
    doc.getDocumentElement().normalize();
    System.out.println("[" + parse(doc.getFirstChild()) + "]");
}

which gives the output:

Employees[Employee[name,age,role,gender,address[flat,housenum,area],ContactDetails[mobile,landline]],Employee[name,age,role,gender,addressLine[local,gate,area]]]]

Upvotes: 0

Karol Dowbecki
Karol Dowbecki

Reputation: 44952

Look out, your example XML is not properly formatted due to duplicated </Employee> tag after first employee.


Assuming you don't know the XML schema and the XML can be loaded into the memory you can use Dom4j and create a recursive function to inspect the elements:

public static void main(String[] args) throws Exception {
    SAXReader reader = new SAXReader();
    Document doc = reader.read(Main.class.getResourceAsStream("/input.xml"));
    iterate(doc.getRootElement(), 0);
}

private static void iterate(Element el, int level) {
    System.out.print(" ".repeat(level));
    System.out.println(el.getName());
    el.elements().forEach(e -> iterate(e, level + 1));
}

will output:

Employees
 Employee
  name
  age
  role
  gender
  address
   flat
   housenum
   area
  ContactDetails
   mobile
   landline
 Employee
  name
  age
  role
  gender
  addressLine
   local
   gate
   area

Upvotes: 0

kidney
kidney

Reputation: 3083

I would use Java SAX and log element name in every startElement event.

A nice example can be found here.

Upvotes: 0

Related Questions