complex XML to Java-JSON transformation needed

49 views
Skip to first unread message

elemerz

unread,
Apr 19, 2010, 8:08:36 AM4/19/10
to xmappr
Hi,
I posted a question today before this one, and thanks for your quick
answer Peter Knego
I tried all the 4 points you advised me, but it still get errors. So I
have no other way than to present my problem:
I have a Javascript validation framework which needs to be configured
using JSON objects.
I must write an XML-based configuration utility for this client-side
JavaScript validation framework in Java.
After a little of googling I came across Xmappr and I started to have
a good feeling 
But finally I could not manage  Pls help me if you can! Thanks in
advance

Elemér.

I need to convert the following XML

<?xml version="1.0" encoding="UTF-8"?>
<validations xmlns="http://www.isdc.ro/vld"
xmlns:xsi="http://www.w3.org/
2001/XMLSchema-instance"
xsi:schemaLocation="http://
www.isdc.ro/vld vld.xsd">
<validation forElements="#txtPassword,#txtPasswordVerify"
group="pass" groupToValidate="pass" onBefore="beforeValidateHandler">
<onEvents>
<onEvent name="blur"/>
<onEvent target="form" name="submit"/>
</onEvents>
<handlers>
<handler function="defaultVisualFeedbackHandlerJS"/>
</handlers>
<rules>
<rule msg="Password must be at least 6 chars long!"
negate="false" rule="/\w+/"/>
<rule msg="Password must contain lowercase
characters uppercase characters and numbers as well!"
rule="passValidatorJS"/>
</rules>
</validation>
<!—another validations…
<validation></validation>

</validations>

Into Java objects, do some checking and finally convert it into JSON
like follows:

var vlds = {
validations: [{
feedbackElement: null,
group: 'default',
validatable: null, //the group to be validated instead of the
control
onBefore: null,
onAfter: null,
errorClass: 'invalid', //class applied on an invalid control.
onEvent: 'blur', //string:this object's specified standard event
or JSON:{target:a jquery object,name:event name}
handlers: null,//should be overridden in constructor
rules: [{
rule: /\w+/, //can be a regex, a boolean or a function returning
any value
negate: false,//inverses the validation logic:useful if it's
easier to specify when a control is invalid than the case when is
valid.
msg: 'This field is Required!'
}], /*required. A non-empty array of validation rules*/
validateOnCreate: false
}, {/*validation-2*/}, {/*validation-3*/}]
};

Can you please help me, how can I do this as simply as possible?



--
Subscription settings: http://groups.google.com/group/xmappr/subscribe?hl=en

Peter Knego

unread,
Apr 19, 2010, 10:22:33 AM4/19/10
to xmappr
I can only help with regard to Xmappr questions - i.e. XML to Java
mapping. For Java to JSON try some JSON library. Jackson is
recommended.

This code works for me:

public class Foo {

public static String xml = "" +
"<validations xmlns='http://www.isdc.ro/vld' " +
"xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " +
"xsi:schemaLocation='http://www.isdc.ro/vld vld.xsd'>" +
"<validation forElements='#txtPassword,#txtPasswordVerify'
" +
"group='pass' groupToValidate='pass'
onBefore='beforeValidateHandler' >\n" +
" <onEvents>" +
" <onEvent name='blur'/>" +
" <onEvent target='form' name='submit'/>" +
" </onEvents>\n" +
" <handlers>" +
" <handler function='defaultVisualFeedbackHandlerJS'/>"
+
" </handlers>\n" +
" <rules>" +
" <rule msg='Password must be at least 6 chars long!' "
+
"negate='false' rule='/\\w+/'/>" +
" <rule msg='Password must contain lowercase characters
uppercase characters and numbers as well!' " +
"rule='passValidatorJS' />" +
" </rules>\n" +
"</validation>" +
"</validations>";

public static void main(String[] args) {
StringReader reader = new StringReader(xml);
Xmappr xmappr = new Xmappr(Validations.class);
xmappr.setPrettyPrint(true);
xmappr.addNamespace("http://www.isdc.ro/vld");

Validations root = (Validations) xmappr.fromXML(reader);

StringWriter writer = new StringWriter();
xmappr.toXML(root, writer);
System.out.println(writer);
}

@RootElement
public static class Validations {
@Element
public List<Validation> validation;
}

public static class Validation {
@Element
public OnEvents onEvents;
@Element
public Handlers handlers;
@Element
public Rules rules;
}

public static class OnEvents {
@Element
public List<OnEvent> onEvent;
}

public static class Handlers {
@Element
public List<Handler> handler;
}

public static class Rules {
@Element
public List<Rule> rule;
}

public static class OnEvent {
@Attribute
public String name;
@Attribute
public String target;
}

public static class Handler {
@Attribute
public String function;
}

public static class Rule {
@Attribute
public String msg;
@Attribute
public boolean negate;
@Attribute
public String rule;

Peter Knego

unread,
Apr 19, 2010, 10:28:56 AM4/19/10
to xmappr
Xmappr does not yet support element wrapping (it's on to-do list): so
Java class structure must correspond one-to-one to XML document
structure.

If you control the structure of this XML you can simplify it: just
remove <onEvents>, <handlers> and <rules> elements, as they are pure
wrappers and bring no additional information. In this case you can
also get rid of OnEvents, Handlers, Rules classes.

In this ase your Validation class would look like this:
public static class Validation {
@Element
public List<OnEvent> onEvent;
@Element
public List<Handler> handler;
@Element
public List<Rule> rule;
}

Peter

On Apr 19, 4:22 pm, Peter Knego <pe...@knego.net> wrote:
> I can only help with regard to Xmappr questions - i.e. XML to Java
> mapping. For Java to JSON try some JSON library. Jackson is
> recommended.
>
> This code works for me:
>
> public class Foo {
>
>     public static String xml = "" +
>             "<validations xmlns='http://www.isdc.ro/vld'" +
>             "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" +
>             "xsi:schemaLocation='http://www.isdc.ro/vldvld.xsd'>" +
Reply all
Reply to author
Forward
0 new messages