This discussion is archived
5 Replies Latest reply: Mar 20, 2012 11:13 AM by DrClap RSS

Class to support XML Config.

920854 Newbie
Currently Being Moderated
Hi ,

I have a system that loads your properties trough a class that reads an xml file. For example ans sysconfig.xml that holds all of the system properties. Now, i need to load xml properties to another class, for example an class that read files from another application, and i need to store password, login, path, etc..

What is the best approach to create an generic class that receives an string that represents an xml and load it?

Is too ugly receives an String with the name of xml to load??

Thanks for suggestions..
  • 1. Re: Class to support XML Config.
    EJP Guru
    Currently Being Moderated
    java.util.Properties?
  • 2. Re: Class to support XML Config.
    920854 Newbie
    Currently Being Moderated
    This is my method that loads the xml configuration:

         private synchronized void loadPropriedades() throws FileException {
              if (propriedades != null) {
                   return;
              }
              try {
                   DocumentBuilder db = JAXPFactory.getInstance().getFactory();
                   Document xml = db.parse(getClass().getResourceAsStream(
                             "/.xml"));
                   NodeList nodeListPrincipal = xml.getElementsByTagName("Config");
                   NamedNodeMap atributos = nodeListPrincipal.item(0).getAttributes();
                   Node nodeAtual = null;
                   propriedades = new HashMap();
                   for (int f = 0; f < atributos.getLength(); f++) {
                        nodeAtual = atributos.item(f);
                        propriedades.put(nodeAtual.getNodeName(), nodeAtual
                                  .getNodeValue());
                   }
              } catch (SAXException sae) {
                   Log.error("Erro na leitura do XML de configurações do sistema", se);
              } catch (ParserConfigurationException pace) {
                   Log.error("Erro ao traduzir XML de configurações do sistema", pce);
              } catch (IOException ioe) {
                   Log.error("Erro na abertura do XML de configurações do sistema",
                             ioe);
              } catch (Exception e) {
                   Log.error(e);
              }
         }

    My idea is transform the method above to something like private synchronized void loadPropriedades(String XmlReceived) <----------------------------------- To avoid create one class to each xml, make a class to load, but i dont know if receives a String with xml name is good...

    Understand?

    Thanks !
  • 3. Re: Class to support XML Config.
    DrClap Expert
    Currently Being Moderated
    If the method doesn't know the name of the file it is supposed to load, then passing the name of the file as a parameter seems like a perfectly ordinary thing to do. Was that really your question?
  • 4. Re: Class to support XML Config.
    920854 Newbie
    Currently Being Moderated
    So, i need to use this class like an utility class, so any new xml config can be passed by to this class, to load the properties.... So i will add some parameters or maibe create a class that represents the config... something like XmlConfig that holds the name of file, nodes, etc... to avoid the String.... understand me?

    What you think about?

    thanks

    Edited by: PaulH on 20/03/2012 14:09

    Edited by: PaulH on 20/03/2012 14:10
  • 5. Re: Class to support XML Config.
    DrClap Expert
    Currently Being Moderated
    Like EJP, I would just use an XML-formatted Properties file. That way I wouldn't have to write any XML parsing code. Then I don't think a "utility class" would be necessary, given that the built-in Properties class would be doing all of the work.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points