Parse config file java




















Imagine a config file tracking some display properties of a GUI window:. If you're on an operating system that doesn't follow Freedesktop standards, you can still use this location, but you may have to create all the directories yourself. If you're new to Java, start by reading my 7 tips for new Java developers article. Once you're relatively comfortable with Java, open your favorite integrated development environment IDE and create a new project. I call mine myConfigParser.

Without worrying too much about imports and error catching initially, you can instantiate a parser using the standard Java extensions found in the javax and java. If you're using an IDE, you'll be prompted to import the appropriate libraries; otherwise, you can find a full list of libraries in the complete version of this code later in this article.

This example code uses the java. Paths library to locate the user's home directory, adding the default configuration location to the path. Then it defines the configuration file to be parsed as a File object using the java. File library. Next, it uses the javax. DocumentBuilder and javax. DocumentBuilderFactory libraries to create an internal document builder so that the Java program can ingest and parse XML data. Finally, Java builds a document called doc and loads the configFile file into it.

Using org. That's essentially it. Technically, you're done parsing the data. But parsed data isn't of much use to you if you can't access it, so write some queries to extract important values from your configuration.

Getting data from your ingested XML document is a matter of referencing a specific node and then "walking" through the elements it contains.

It's common to use a series of loops to iterate through elements in nodes, but I'll keep that to a minimum here, just to keep the code easy to read:. This sample code creates a NodeList object called nodes using the org. NodeList; library. This object contains any child node with a name that matches the string window , which is the only node in the sample config file created in this article.

Right now, my code looks utterly disgusting. For example,. As you can see, the code is pretty messy, and I have cut it short for the presentation here. I feel there are better ways to do this as well maybe not using BufferedReader. Can someone please provide possibly a better way or approach that is not as convoluted as mine? I would suggest not creating custom code for config files. What you're proposing isn't too far removed from YAML getting started. Use that instead.

The one criticism I could offer is that it's not recursive. Every level requires a new level of code to support. I would probably make a recursive function a function that calls itself with sub-content as parameter and then again if there's sub-sub-content etc. For example. Custom Extremely simple As I mentioned in the comment below, you can just make them all name and value pairs.

A nice way to clean it up would be to use a table, i. You can then invoke you parsing methods through reflection simple or create a few more classes implementing a common interface more work but more robust. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.

What is the best way to parse this configuration file? Ask Question. When the user enters some information, I want the details of the textfields, comboboxes and checkboxes to be saved into a file, and the next time when a user opens this Window, I want the details that have been saved in the file, i. Can anyone please help me in doing this?

I hope you understand the question, if not I will explain in a more detailed manner. Here is a simple example using the java. The simplest way is to use java. Properties that is able to store its content in file properties or xml format. If you want more you can use XML. You can also use configuration package The list can be continued If the config files don't have to be editable using a text editor just serialize your config to a file and deserialize it when you need to read it.

Otherwise have a look a the Properties class that allows you to write key-value pairs as either properties files or XML. You might have to do some conversion from string to the value you need though, i. If you want some structure to your configuration files and dont like the flat java properties format or messy XML tagging then consider using JSON which is a very nice and clean format for storing any kind of data structure.

JSON is also used as a simple data protocol for web based apps if you get into that at some stage. Tags: config , Java. No Comments. Thanks Adil. I will try and improve the quality of the articles. Hey i have a question about the. And here is the Singleton version: import java. Is it possible to load the config file from a file on the file system? Like System.



0コメント

  • 1000 / 1000