What are the advantages and disadvantages of using XML to transfer data in this Java program?

I was asked to write a GUI of an existing shell / CmdLine program written in Java. I want to create an abstraction layer between the GUI and the original program, so that different GUIs can be easily added (web interfaces, for example, rather than desktop applications) At present, the structure of the programme is as follows:

Command line options will be immediately converted to Java hash (hash with - f option) "- F = > opt" and other options, and then used to set the environment object used by the program

The structure I think of is:

Following the solid line means that the GUI generates an XML file that holds the same information as the shell / CmdLine option, which is then used to set the environment options

The thing is, I'm not sure if this is the best way for things to work (dotted lines represent alternative structures), and I don't know whether XML is the right choice

In this way, the program part that sets the environment object from the option is in the same way as the part that uses the environment to get the results Therefore, it is easier to implement the content that provides shell / CmdLine parameters directly to the program than to implement the structure that passes information as an XML file I certainly don't want to create an XML file and then convert it into shell options and pass it to the program, but the GUI can more meaningfully generate the shell options themselves than create XML

The main advantage of using XML files is that I can see that it makes it easier for future developers to create XML files using existing libraries without worrying about obtaining - a opt1 - B OPT2 – C opt3 [...] syntax permissions

On the other hand, I've heard that trying to create your own XML language is not to be taken lightly (although the program stores data in XML files without DTDs or even schemas, as far as I know)

Is my approach more or less correct? Or am I using totally inappropriate tools to do what I want to do?

Solution

In the XML method, you will use user options to create XML, pass it to the main program, parse the XML and extract values to set the environment Unless you pass options from one process to another or through threads (JSON can be used even in this case), these efforts will be too much

If we talk about a single process that passes these options through the GUI or command line, we can simply encapsulate these parameters into Java objects and pass them to the main program using the command line / GUI For example, the command line or GUI – > populates the environmentoptions object – > the main program

You can create an interface that describes the ienenvironmentoption and use it to set the required properties

interface IEnvironmentOption {
    public static final String OPTION_NAME = "-t";

    public void setOption(String name,String value);

    public String getOption(String name);
}

class EnvironmentOptions implements IEnvironmentOption {
    private Properties envProperties;

    @Override
    public void setOption(String name,String value) {
        envProperties.setProperty(name,value);
    }

    @Override
    public String getOption(String name) {
        return envProperties.getProperty(name);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>