-1

Hi I am completely new in XML in Java.In my recent project I need to create validation rules in XML,but the the problem is that different user group may have different rule For example

<root>
 <user-group type="sale">
    <parameter-name ="loginName">
       <max-length>10</max-length>
       <min-length>4</min-length>
    </parameter-name>
    <parameter-name ="password">
    <max-length>10</max-length>
    <min-length>4</min-length>
    </parameter-name>
 </user-group>   
 <user-group type="clerk">
   <parameter-name ="loginName">
      <max-length>16</max-length>
      <min-length>4</min-length>
      </parameter-name>
      <parameter-name ="password">
      <max-length>12</max-length>
      <min-length>8</min-length>
      </parameter-name>
 </user-group>` 
</root>

So how to write a Java stuff to implements the above rule. Thanks in advance.

SBM
  • 1
  • Sorry to say i have no idea how to implement the logic in java – SBM Nov 13 '14 at 19:00
  • Well, welcome to StackOverflow! please refer to this page to help us help you: http://stackoverflow.com/help/how-to-ask. Also, your XML is invalid (`` is not a valid node). Is that the real XML you are parsing or a typo? – blurfus Nov 13 '14 at 20:34

2 Answers2

0

Read the XML using one of the known XML parsers. Refer XML Parsing for Java

As you read through the XML, you can create a data structure to store the rules. This is explained below. Loop through each of the "user-group" XML nodes in your Java program, create a map implementation, you can use a HashMap, with key - "clerk" value will be a POJO bean defining a "rule"

For example here is your "Rules" class -

 public class Rules {

    private String ruleName;
    private int maxLength;
    private int minLength;

    public String getRuleName() {
        return ruleName;
    }

    public void setRuleName(String ruleName) {
        this.ruleName = ruleName;
    }

    public int getMinLength() {
        return minLength;
    }

    public void setMinLength(int minLength) {
        this.minLength = minLength;
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    public int getMaxLength() {
        return maxLength;
    }
}

Now you can use this HashMap anywhere in your program, to implement the rules. Seems like you would need to implement rules on the UI. In that case, I would recommend using established frameworks like Struts, Spring or an equivalent framework.

Hope this gives you a headstart ;)

blurfus
  • 13,485
  • 8
  • 55
  • 61
Nick
  • 31
  • 11
0

The simple answer: use XML schemas with define namespaces. This way each user-group type can define what the structure of that node is. Setting this as an attribute is not really the most effective way to do this. I can elaborate later tonight on how to use XSD with namespaces so that you could create a document with "different" user-group nodes, specified in different namespaces, that each entity could validate and use without any problems. I don't have time to show an example, but I found this: Creating an XML document using namespaces in Java

The most simplistic explanation I can come up with is the definition of "table". For a furniture store, a "table" entity has maybe a round or square surface with most likely 4 legs, etc. But a "table" could mean something completely different for some other group. Using your XML as an example, it would be something like this:

<root>
 <sale:user-group xmlns:sale="SOME_URL">
    <some structure and rules>
 </sale:user-group>   
 <clerk:user-group xmlns:clerk="SOME_OTHER_URL">
    <different structure and rules>
 </clerk:user-group>
</root>

The link I provided should answer your question. If not, I will come back tonight and show you a simple XSD that might fit your case.

Community
  • 1
  • 1
hfontanez
  • 5,774
  • 2
  • 25
  • 37