title: Java: Read XML files with dynamic structure
body: <p>In my project I have several repository <code>xml</code> files with different structure. For example:</p>
  <p><code>common.xml</code></p>
  <pre><code>&lt;general&gt;    
      &lt;youTube&gt;https://www.youtube.com/&lt;/youTube&gt;     
      &lt;eBay&gt;https://www.ebay.com/&lt;/eBay&gt;  
      &lt;trollPic&gt;https://mallofnorway.com/content/uploads/2020/07/008840118.jpg&lt;/trollPic&gt;
      &lt;ui&gt;
          &lt;multiValueAtt&gt;Gender&lt;/multiValueAtt&gt;
      &lt;/ui&gt;
      &lt;api&gt;
          &lt;ownerId&gt;1e1b3a81-8f9a-4126-890a-109694e9fd7c&lt;/ownerId&gt;
          &lt;schemaId&gt;4cbacf2e-8f3f-4c0b-9764-d88d302d279f&lt;/schemaId&gt;
      &lt;/api&gt;
  &lt;/general&gt;  
  </code></pre>
  <p><code>partner.xml</code></p>
  <pre><code>&lt;general&gt;
      &lt;ui&gt;
          &lt;partner&gt;
              &lt;schema&gt;
                  &lt;multiValueAttr&gt;Gender&lt;/multiValueAttr&gt;
              &lt;/schema&gt;            
          &lt;/partner&gt;
          &lt;member&gt;
              &lt;schema&gt;
                  &lt;multiValueAttr&gt;Department&lt;/multiValueAttr&gt;
              &lt;/schema&gt;            
          &lt;/member&gt;
      &lt;/ui&gt;
  &lt;/general&gt;  
  </code></pre>
  <p>I need to make a function to get values from all these files. So I understand that I need to pass to this my function the xml file location and the specified tag name I'm looking for inside that file and maybe the xml path to that tag / string. So I guess the method call should look like <code>getConfigParam('./some-file.xml', 'ui.partner.schema.multiValueAttr');</code><br />
  I know how to read simple <code>xml</code> file:</p>
  <pre><code>static public String objRepository(String fileName, String area, String key){
      try{
          String rootPath = Paths.get(&quot;.&quot;).toAbsolutePath().normalize().toString();
          String path = rootPath + &quot;\\src\\main\\resources\\repository\\&quot; + fileName + &quot;.xml&quot;;
          File file=new File(path);
          DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
          DocumentBuilder db=dbf.newDocumentBuilder();
          Document doc=db.parse(file);
          doc.getDocumentElement().normalize();
          //System.out.println(&quot;The node name is: &quot;+doc.getDocumentElement().getNodeName());
  
          NodeList nList=doc.getElementsByTagName(area);
          //System.out.println(&quot;The length is: &quot;+nList.getLength());
          for(int i=0; i&lt;nList.getLength(); i++){
              Node nNode=nList.item(i);
              if(nNode.getNodeType()==Node.ELEMENT_NODE){
                  Element ele=(Element) nNode;
                  return ele.getElementsByTagName(key).item(i).getTextContent();
              }
          }
  
      }catch(Exception e){
          e.printStackTrace();
      }
      return &quot;&quot;;
  }
  </code></pre>
  <p>I also know <a href="https://stackoverflow.com/a/22237761/3485434">how to read</a> nested <code>xml</code> files with constant structure but I do not know how to read nested <code>xml</code> files with different structures with the same method.</p>