Question

in the post located here

Andrew Finnell's reply was by far the most helpful to a point. I do however need to know how to expand that into allowing other sections other than just

<properties>
</properties>

If the answer is already on that page please let me know, if not can someone help me out.

Basically I want to have

<properties>
</properties>
<blah1>
</blah1>
<blah2>
</blah2>

etc... as my sections. All the normal xml libs and such I have found are far to bulky and confusing so I am hoping to be able to use the afore referenced method with much more ease. Thanks in advance.

Was it helpful?

Solution

An xml file needs excatly one root element. Otherwise it is not valid and can't be processed by xml parsers.

A valid xml with sections could look like this:

<properties>  <!-- root element -->
  <section1>  <!-- section 1 as a child of root -->
  </section1>
  <section2>  <!-- section 2 as a child of root -->
  </section2>
</properties>

OTHER TIPS

If you put sections into an XML properties file, it is no longer an XML properties file, and you can't expect to use java.utils.Properties to process it.

If your requirement is for properties (name/value pairs) within named sections, then you could use INI file syntax. There's a Java library for handling this.

If you want something more general, then take a look at JSON. There are a numer of Java libraries that handle JSON. (I like Jackson because it allows you to map regular Java beans to JSON.)

Ok thanks for quick responses. I have restructured things a bit and here is my xml. Is this 100% compliant or are minor changes needed?

<?xml version="1.0"?>
<configuration>
<settings name="connection">
    <server>test.com</server>
    <name>blah</name>
    <password>blah</password>
    <owner>blah</owner>
    <staff>blah,blH,BLAH,BLah</staff>
    <timercount>5</timercount>
</settings>

<settings name="prvtmsg">
    <hello>Hello %person. How are you today?</hello>
    <commands>I have the following commands: About, Help, and Join</commands>
</setting>

<settings name="timers">
    <timer1>interval,action,delay</timer1>
    <timer2>interval,action,delay</timer2>
    <timer3>interval,action,delay</timer3>
    <timer4>interval,action,delay</timer4>
    <timer5>interval,action,delay</timer5>
</settings>
</configuration>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top